hakai_api 0.2.2__py3-none-any.whl

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/Client.py ADDED
@@ -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)
hakai_api/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+ from hakai_api.Client import Client
2
+ all = [Client]
@@ -0,0 +1,18 @@
1
+ Metadata-Version: 2.1
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
+ Platform: UNKNOWN
10
+ Requires-Dist: future
11
+ Requires-Dist: pytz
12
+ Requires-Dist: requests
13
+ Requires-Dist: requests-oauthlib
14
+ Requires-Dist: six
15
+
16
+ UNKNOWN
17
+
18
+
@@ -0,0 +1,6 @@
1
+ hakai_api/Client.py,sha256=bsh4OJ5IsRheuisC81EtqJIjEUbV07Xm8qPId3R9VCU,4084
2
+ hakai_api/__init__.py,sha256=8NCyUrIEBlBEe5sNx0nnGhM6t8XOgHmPV96WrdtEYVw,51
3
+ hakai_api-0.2.2.dist-info/METADATA,sha256=XEeg6f7o21hOKb1yhhUEs2-Kqe1jBNlCvJbMQfWlcC4,393
4
+ hakai_api-0.2.2.dist-info/WHEEL,sha256=OqRkF0eY5GHssMorFjlbTIq072vpHpF60fIQA6lS9xA,92
5
+ hakai_api-0.2.2.dist-info/top_level.txt,sha256=GZUG1cyzQhmb7pm-yZ1LDwKc1YFGTIMIAyTt1lE4ULk,10
6
+ hakai_api-0.2.2.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.36.2)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ hakai_api