-
Notifications
You must be signed in to change notification settings - Fork 2
/
01_person.py
97 lines (61 loc) · 3.61 KB
/
01_person.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
"""01 Person.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1SLMWV_mCWuT82pZtsPpLHRmAF_HArWvR
# 1 - Introducing interacting with the API, the JSON format and structure
Import the libraries so that they can be used within the notebook
* **requests** is used to make HTTP calls
* **json** is used to encode and decode strings into JSON
* **string** is used to perform text manipulation and checking
* **pandas** helps format the JSON data in a more readable format
"""
import requests
import json
import string
import pandas as pd
from pandas.io.json import json_normalize
"""Since we are just 'reading', we will use the FAIRDOMHub directly for these examples"""
base_url = 'https://www.fairdomhub.org'
"""A helper method for receiving the JSON response for a give resource type and id.
We request using the standard URL (in the example https://fairdomhub.org/people/134), but instead of receiving the default HTML response, we request to get JSON. The is acheived by the Accept header: *"Accept": "application/vnd.api+json"* . This is known as [Content Negotiation](https://en.wikipedia.org/wiki/Content_negotiation) ("application/json" would also work).
You can also see the JSON in the browser by adding the **.json** extension, https://fairdomhub.org/people/134.json .
We use the Python **requests** module to make the request:
* **Accept: application/vnd.api+json** - indicates that the notebook expects any data returned to be in JSON API format
* **Accept-Charset: ISO-8859-1** - indicates that the notebook expects any text returned to be in ISO-8859-1 character set
"""
def json_for_resource(type, id):
headers = {
"Accept": "application/vnd.api+json",
"Accept-Charset": "ISO-8859-1"
}
r = requests.get(base_url + "/" + type + "/" + str(id), headers=headers)
r.raise_for_status()
return r.json()
"""Using the previous function, we request the JSON for a person in the FAIRDOMHub, and display the resulting JSON.
The JSON structure follows the [JSONAPI Specification](https://jsonapi.org/), which is shared convention. It is designed to be flexible, application indepentant, and makes interoperability and general tooling easier.
The main elements are:
* **data** - the resources primary data
* **attributes** - metadata attributes association with the resource
* **links** - links related to the resource, usually the link to itself
* **meta** - non-standard meta-information about a resource that can not be represented as an attribute or relationship, this is generally created and updated timestamps
* **relationships** - other resources related to this resource
"""
person_id = 134
result = json_for_resource('people',person_id)
result
"""Here we delve into the attributes:"""
data = json_normalize(result['data']['attributes'])
data
"""Listing the **tools** this person has described themself as having knowledge about"""
result['data']['attributes']['tools']
"""Here is the persons **avatar** displayed, which is also accessible through the API"""
avatar_url = base_url + result['data']['attributes']['avatar']
from IPython.display import Image
from IPython.core.display import HTML
Image(url= avatar_url)
"""# Exercise 1
* Run the notebook and familiarise yourself with each step. Have a short glance at the JSON API spec
* Find yourself, or a collegue on the [FAIRDOMHub](https://fairdomhub.org). Ideally somebody with an avatar and some details filled out about themself
* Update the notebook to display details about the person, and also update it to list their *expertise* instead of *tools*
"""