Skip to content

Commit ad5f82a

Browse files
committed
add shelterluv adoptions to 360 response
move shelterluv token to a separate file which is in gitignore
1 parent 8e3f9e5 commit ad5f82a

File tree

6 files changed

+14218
-14
lines changed

6 files changed

+14218
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ start_env.sh
1818

1919
/src/server/venv/
2020
/src/local_files/
21+
/src/server/secrets.py

src/client/.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@
2020

2121
npm-debug.log*
2222
yarn-debug.log*
23-
yarn-error.log*
24-
package-lock.json
23+
yarn-error.log*

src/client/package-lock.json

Lines changed: 14174 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/server/api/common_api.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
from config import engine
33
from flask import jsonify
44
from sqlalchemy.sql import text
5+
import requests
6+
import json
7+
from secrets import SHELTERLUV_SECRET_TOKEN
58

69

710
@common_api.route('/api/contacts/<search_text>', methods=['GET'])
@@ -41,17 +44,41 @@ def get_360(matching_id):
4144
for row in result["contact_details"]:
4245
if row["source_type"] == "salesforcecontacts":
4346
donations_query = text("select * from salesforcedonations where contact_id like :salesforcecontacts_id")
44-
query_result = connection.execute(donations_query, salesforcecontacts_id=row["source_id"] + "%")
45-
salesforcedonations_results = [dict(row) for row in query_result]
46-
result['donations'] = salesforcedonations_results
47+
salesforce_contacts_query_result = connection.execute(donations_query,
48+
salesforcecontacts_id=row["source_id"] + "%")
49+
salesforce_donations_results = [dict(row) for row in salesforce_contacts_query_result]
50+
result['donations'] = salesforce_donations_results
4751

4852
if row["source_type"] == "volgistics":
4953
shifts_query = text("select * from volgisticsshifts where number = :volgistics_id")
50-
query_result = connection.execute(shifts_query, volgistics_id=row["source_id"])
51-
volgisticsshifts_results = [dict(row) for row in query_result]
54+
volgistics_shifts_query_result = connection.execute(shifts_query, volgistics_id=row["source_id"])
55+
volgisticsshifts_results = [dict(row) for row in volgistics_shifts_query_result]
5256
result['shifts'] = volgisticsshifts_results
5357

54-
#todo: add adoptions
58+
if row["source_type"] == "shelterluvpeople":
59+
adoptions = []
60+
person = requests.get("http://shelterluv.com/api/v1/people/{}".format(row["source_id"]),
61+
headers={"x-api-key": SHELTERLUV_SECRET_TOKEN})
62+
person_json = person.json()
63+
animal_ids = person_json["Animal_ids"]
5564

65+
for animal_id in animal_ids:
66+
animal_events = requests.get("http://shelterluv.com/api/v1/animals/{}/events".format(animal_id),
67+
headers={"x-api-key": SHELTERLUV_SECRET_TOKEN})
68+
animal_events_json = animal_events.json()
69+
70+
for event in animal_events_json["events"]:
71+
for adoption in event["AssociatedRecords"]:
72+
if adoption["Type"] == "Person" and adoption["Id"] == row["source_id"]:
73+
del event["AssociatedRecords"]
74+
animal_details = requests.get(
75+
"http://shelterluv.com/api/v1/animals/{}".format(animal_id),
76+
headers={"x-api-key": SHELTERLUV_SECRET_TOKEN})
77+
78+
animal_details_json = animal_details.json()
79+
event["animal_details"] = animal_details_json
80+
adoptions.append(event)
81+
82+
result['adoptions'] = adoptions
5683

5784
return jsonify({'result': result})

src/server/datasource_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def volgistics_address(index, street):
105105
}
106106
},
107107
"shelterluvpeople": {
108-
"source_id": "id",
108+
"source_id": "internal-id",
109109
"first_name": "firstname",
110110
"last_name": "lastname",
111111
"email": "email",

src/server/pipeline/shelterluv_api_handler.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import requests
22
import json
33
import config
4+
from secrets import SHELTERLUV_SECRET_TOKEN
45

56
#################################
67
# This script is used to fetch data from shelterluv API.
@@ -14,24 +15,26 @@
1415
# /people has all the data. it seems that /person/:id isn't used
1516
#################################
1617

17-
shelterluv_token = "replace_with_secret_token" #dropbox
18-
19-
2018
''' Iterate over all shelterlove people and store in json file in the raw data folder
2119
We fetch 100 items in each request, since that is the limit based on our research '''
22-
def save_shelterluv_people_all():
20+
def store_shelterluv_people_all():
2321
offset = 0
2422
LIMIT = 100
2523
has_more = True
2624
shelterluv_people = []
2725

2826
while has_more:
2927
r = requests.get("http://shelterluv.com/api/v1/people?limit={}&offset={}".format(LIMIT, offset),
30-
headers={"x-api-key": shelterluv_token})
28+
headers={"x-api-key": SHELTERLUV_SECRET_TOKEN})
3129
response = r.json()
3230
shelterluv_people += response["people"]
3331
has_more = response["has_more"]
3432
offset += 100
3533

3634
with open(config.RAW_DATA_PATH + "shelterLuv_people.json", "w") as outfile:
3735
json.dump(shelterluv_people, outfile, indent=4)
36+
37+
38+
if __name__ == "__main__":
39+
# execute only if run as a script
40+
store_shelterluv_people_all()

0 commit comments

Comments
 (0)