hakai_api 0.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hakai_api-0.2.2/PKG-INFO +10 -0
- hakai_api-0.2.2/README.md +51 -0
- hakai_api-0.2.2/hakai_api/Client.py +113 -0
- hakai_api-0.2.2/hakai_api/__init__.py +2 -0
- hakai_api-0.2.2/hakai_api.egg-info/PKG-INFO +10 -0
- hakai_api-0.2.2/hakai_api.egg-info/SOURCES.txt +10 -0
- hakai_api-0.2.2/hakai_api.egg-info/dependency_links.txt +1 -0
- hakai_api-0.2.2/hakai_api.egg-info/not-zip-safe +1 -0
- hakai_api-0.2.2/hakai_api.egg-info/requires.txt +5 -0
- hakai_api-0.2.2/hakai_api.egg-info/top_level.txt +1 -0
- hakai_api-0.2.2/setup.cfg +4 -0
- hakai_api-0.2.2/setup.py +20 -0
hakai_api-0.2.2/PKG-INFO
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Metadata-Version: 1.0
|
|
2
|
+
Name: hakai_api
|
|
3
|
+
Version: 0.2.2
|
|
4
|
+
Summary: Get Hakai database resources using http calls
|
|
5
|
+
Home-page: https://github.com/tayden/hakai-api-client-python
|
|
6
|
+
Author: Taylor Denouden
|
|
7
|
+
Author-email: taylor.denouden@hakai.org
|
|
8
|
+
License: MIT
|
|
9
|
+
Description: UNKNOWN
|
|
10
|
+
Platform: UNKNOWN
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Hakai Api Python Client
|
|
2
|
+
|
|
3
|
+
This project exports a single Python class that can be used to make HTTP requests to the Hakai API resource server.
|
|
4
|
+
The exported `Client` class extends the functionality of the Python [requests library](https://docs.python-requests.org/en/master/) to supply Hakai OAuth2 credentials with url requests.
|
|
5
|
+
|
|
6
|
+
# Installation
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pip install hakai-api
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
# Quickstart
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
from hakai_api import Client
|
|
16
|
+
|
|
17
|
+
# Get the api request client
|
|
18
|
+
client = Client() # Follow stdout prompts to get an API token
|
|
19
|
+
|
|
20
|
+
# Make a data request for chlorophyll data
|
|
21
|
+
url = '%s/%s' % (client.api_root, 'eims/views/output/chlorophyll?limit=50')
|
|
22
|
+
response = client.get(url)
|
|
23
|
+
|
|
24
|
+
print(url) # https://hecate.hakai.org/api/eims/views/output/chlorophyll...
|
|
25
|
+
print(response.json()) # [{'action': '', 'event_pk': 7064, 'rn': '1', 'date': '2012-05-17', 'work_area': 'CALVERT'...
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
# Methods
|
|
29
|
+
|
|
30
|
+
This library exports a single client name `Client`. Instantiating this class produces a `requests.Session` client from the Python requests library. The Hakai API Python Client inherits directly from `requests.Session` thus all methods available on that parent class are available. For details see the [requests documentation](http://docs.python-requests.org/).
|
|
31
|
+
|
|
32
|
+
The hakai_api `Client` class also contains a property `api_root` which is useful for constructing urls to access data from the API. The above [Quickstart example](#quickstart) demonstrates using this property to construct a url to access project names.
|
|
33
|
+
|
|
34
|
+
# API endpoints
|
|
35
|
+
|
|
36
|
+
For details about the API, including available endpoints where data can be requested from, see the [Hakai API documentation](https://github.com/HakaiInstitute/hakai-api).
|
|
37
|
+
|
|
38
|
+
# Advanced usage
|
|
39
|
+
|
|
40
|
+
You can specify which API to access when instantiating the Client. By default, the API uses `https://hecate.hakai.org/api` as the API root. It may be useful to use this library to access a locally running API instance or to access the Goose API for testing purposes. If you are always going to be accessing data from a locally running API instance, you are better off using the requests.py library directly since Authorization is not required for local requests.
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
# Get a client for a locally running API instance
|
|
44
|
+
client = Client("http://localhost:8666")
|
|
45
|
+
print(client.api_root) # http://localhost:8666
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
# Development
|
|
49
|
+
The business code for this package is in the file [`hakai_api/Client.py`](hakai_api/Client.py).
|
|
50
|
+
The `./setup.py` file is important for giving details about how to install the package when users do `pip install hakai-api`.
|
|
51
|
+
You must update the semantic version info in setup.py and then push a tag to the Github origin in order for PyPi to build and release a new version of the package.
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""This allows authorized requests to the Hakai API using the requests library.
|
|
2
|
+
|
|
3
|
+
Written by: Taylor Denouden, Chris Davis, and Nate Rosenstock
|
|
4
|
+
Last updated: April 2021
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import print_function
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
import pickle
|
|
11
|
+
from datetime import datetime
|
|
12
|
+
from time import mktime
|
|
13
|
+
|
|
14
|
+
from pytz import utc
|
|
15
|
+
from requests_oauthlib import OAuth2Session
|
|
16
|
+
from six.moves import input
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Client(OAuth2Session):
|
|
20
|
+
# noinspection SpellCheckingInspection
|
|
21
|
+
_client_id = '289782143400-1f4r7l823cqg8fthd31ch4ug0thpejme.apps.googleusercontent.com'
|
|
22
|
+
_credentials_file = os.path.expanduser('~/.hakai-api-credentials')
|
|
23
|
+
|
|
24
|
+
def __init__(self, api_root='https://hecate.hakai.org/api'):
|
|
25
|
+
"""Create a new Client class with credentials.
|
|
26
|
+
|
|
27
|
+
Params:
|
|
28
|
+
api_root: The base url of the hakai api you want to call.
|
|
29
|
+
Defaults to the production server.
|
|
30
|
+
"""
|
|
31
|
+
self._api_root = api_root
|
|
32
|
+
self._authorization_base_url = '{}/auth/oauth2'.format(api_root)
|
|
33
|
+
self._token_url = '{}/auth/oauth2/token'.format(api_root)
|
|
34
|
+
|
|
35
|
+
# Try to get cached credentials
|
|
36
|
+
credentials = self._try_to_load_credentials()
|
|
37
|
+
if credentials:
|
|
38
|
+
self._credentials = credentials
|
|
39
|
+
else:
|
|
40
|
+
# Acquire and store credentials from web sign-in.
|
|
41
|
+
credentials = self._get_credentials_from_web()
|
|
42
|
+
# Cache the credentials
|
|
43
|
+
self._save_credentials(credentials)
|
|
44
|
+
self._credentials = credentials
|
|
45
|
+
|
|
46
|
+
# Init the OAuth2Session parent class with credentials
|
|
47
|
+
super(Client, self).__init__(token=self._credentials)
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
def api_root(self):
|
|
51
|
+
"""Return the api base url."""
|
|
52
|
+
return self._api_root
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def credentials(self):
|
|
56
|
+
"""Return the credentials object."""
|
|
57
|
+
return self._credentials
|
|
58
|
+
|
|
59
|
+
@classmethod
|
|
60
|
+
def reset_credentials(cls):
|
|
61
|
+
if os.path.isfile(cls._credentials_file):
|
|
62
|
+
os.remove(cls._credentials_file)
|
|
63
|
+
|
|
64
|
+
def _save_credentials(self, credentials):
|
|
65
|
+
"""Save the credentials object to a file."""
|
|
66
|
+
with open(self._credentials_file, 'wb') as outfile:
|
|
67
|
+
cache = {
|
|
68
|
+
"api_root": self.api_root,
|
|
69
|
+
"credentials": credentials
|
|
70
|
+
}
|
|
71
|
+
pickle.dump(cache, outfile)
|
|
72
|
+
|
|
73
|
+
def _try_to_load_credentials(self):
|
|
74
|
+
"""Try to load the cached credentials file.
|
|
75
|
+
|
|
76
|
+
Returns false if unsuccessful and returns credentials if success.
|
|
77
|
+
"""
|
|
78
|
+
if not os.path.isfile(self._credentials_file):
|
|
79
|
+
return False
|
|
80
|
+
|
|
81
|
+
with open(self._credentials_file, 'rb') as infile:
|
|
82
|
+
try:
|
|
83
|
+
cache = pickle.load(infile)
|
|
84
|
+
api_root = cache["api_root"]
|
|
85
|
+
credentials = cache["credentials"]
|
|
86
|
+
except (KeyError, ValueError):
|
|
87
|
+
os.remove(self._credentials_file)
|
|
88
|
+
return False
|
|
89
|
+
|
|
90
|
+
now = int((mktime(datetime.now(tz=utc).timetuple()) + datetime.now(
|
|
91
|
+
tz=utc).microsecond / 1000000.0)) # utc timestamp
|
|
92
|
+
credentials_expired = now > credentials['expires_at']
|
|
93
|
+
same_api = api_root == self._api_root
|
|
94
|
+
|
|
95
|
+
if not same_api or credentials_expired:
|
|
96
|
+
os.remove(self._credentials_file)
|
|
97
|
+
return False
|
|
98
|
+
return credentials
|
|
99
|
+
|
|
100
|
+
def _get_credentials_from_web(self):
|
|
101
|
+
"""Get user credentials from a web sign-in.
|
|
102
|
+
|
|
103
|
+
Returns false if unsuccessful and returns credentials if success.
|
|
104
|
+
"""
|
|
105
|
+
# Acquire and store credentials from web sign-in.
|
|
106
|
+
oauth2_session = OAuth2Session(self._client_id)
|
|
107
|
+
authorization_url, state = oauth2_session.authorization_url(self._authorization_base_url)
|
|
108
|
+
print('Please go here and authorize:')
|
|
109
|
+
print(authorization_url)
|
|
110
|
+
|
|
111
|
+
redirect_response = input('\nPaste the full redirect URL here:\n')
|
|
112
|
+
assert redirect_response[:5] == "https", "Make sure you paste only the https url"
|
|
113
|
+
return oauth2_session.fetch_token(self._token_url, authorization_response=redirect_response)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Metadata-Version: 1.0
|
|
2
|
+
Name: hakai-api
|
|
3
|
+
Version: 0.2.2
|
|
4
|
+
Summary: Get Hakai database resources using http calls
|
|
5
|
+
Home-page: https://github.com/tayden/hakai-api-client-python
|
|
6
|
+
Author: Taylor Denouden
|
|
7
|
+
Author-email: taylor.denouden@hakai.org
|
|
8
|
+
License: MIT
|
|
9
|
+
Description: UNKNOWN
|
|
10
|
+
Platform: UNKNOWN
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
setup.py
|
|
3
|
+
hakai_api/Client.py
|
|
4
|
+
hakai_api/__init__.py
|
|
5
|
+
hakai_api.egg-info/PKG-INFO
|
|
6
|
+
hakai_api.egg-info/SOURCES.txt
|
|
7
|
+
hakai_api.egg-info/dependency_links.txt
|
|
8
|
+
hakai_api.egg-info/not-zip-safe
|
|
9
|
+
hakai_api.egg-info/requires.txt
|
|
10
|
+
hakai_api.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hakai_api
|
hakai_api-0.2.2/setup.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from setuptools import setup
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name='hakai_api',
|
|
5
|
+
packages=['hakai_api'],
|
|
6
|
+
version='0.2.2',
|
|
7
|
+
description='Get Hakai database resources using http calls',
|
|
8
|
+
url='https://github.com/tayden/hakai-api-client-python',
|
|
9
|
+
author='Taylor Denouden',
|
|
10
|
+
author_email='taylor.denouden@hakai.org',
|
|
11
|
+
license='MIT',
|
|
12
|
+
install_requires=[
|
|
13
|
+
'future',
|
|
14
|
+
'pytz',
|
|
15
|
+
'requests',
|
|
16
|
+
'requests-oauthlib',
|
|
17
|
+
'six',
|
|
18
|
+
],
|
|
19
|
+
zip_safe=False,
|
|
20
|
+
)
|