pygeobox 1.0.0__py3-none-any.whl → 1.0.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.
pygeobox/apikey.py CHANGED
@@ -1,233 +1,233 @@
1
- from typing import List, Dict, Optional, TYPE_CHECKING
2
- from urllib.parse import urljoin
3
-
4
- from .base import Base
5
- from .utils import clean_data
6
-
7
- if TYPE_CHECKING:
8
- from . import GeoboxClient
9
-
10
- class ApiKey(Base):
11
-
12
- BASE_ENDPOINT = 'apikeys/'
13
-
14
- def __init__(self,
15
- api: 'GeoboxClient',
16
- key_id: int,
17
- data: Optional[Dict] = {}):
18
- """
19
- Initialize an apikey instance.
20
-
21
- Args:
22
- api (GeoboxClient): The GeoboxClient instance for making requests.
23
- key_id (int): The unique identifier for the apikey.
24
- data (Dict, optional): The data of the apikey.
25
- """
26
- super().__init__(api, data=data)
27
- self.key_id = key_id
28
- self.endpoint = urljoin(self.BASE_ENDPOINT, str(self.id))
29
-
30
-
31
- def __repr__(self) -> str:
32
- """
33
- Return a string representation of the attachment.
34
-
35
- Returns:
36
- str: The string representation of the attachment.
37
- """
38
- return f'ApiKey(name={self.name}, revoked={self.revoked})'
39
-
40
-
41
- @classmethod
42
- def get_apikeys(cls, api: 'GeoboxClient', **kwargs) -> List['ApiKey']:
43
- """
44
- Get a list of apikeys
45
-
46
- Args:
47
- api (GeoboxClient): The GeoboxClient instance for making requests.
48
-
49
- Keyword Args:
50
- search (str): search term for keyword-based searching among all textual fields.
51
- order_by (str): comma separated list of fields for sorting results [field1 A|D, field2 A|D, …]. e.g. name A, type D. NOTE: "A" denotes ascending order and "D" denotes descending order.
52
- skip (int): Number of layers to skip. default is 0.
53
- limit (int): Maximum number of layers to return. default is 10.
54
- user_id (int): Specific user. privileges required.
55
-
56
- Example:
57
- >>> from geobox import GeoboxClient
58
- >>> from geobox.apikey import ApiKey
59
- >>> client = GeoboxClient()
60
- >>> apikeys = ApiKey.get_apikeys(client)
61
- or
62
- >>> apikeys = client.get_apikeys()
63
- """
64
- params = {
65
- 'search': kwargs.get('search'),
66
- 'order_by': kwargs.get('order_by'),
67
- 'skip': kwargs.get('skip'),
68
- 'limit': kwargs.get('limit'),
69
- 'user_id': kwargs.get('user_id')
70
- }
71
- return super()._get_list(api, cls.BASE_ENDPOINT, params, factory_func=lambda api, item: ApiKey(api, item['id'], item))
72
-
73
-
74
- @classmethod
75
- def create_apikey(cls, api: 'GeoboxClient', name: str, user_id: int = None) -> 'ApiKey':
76
- """
77
- Create an ApiKey
78
-
79
- Args:
80
- api (GeoboxClient): The GeoboxClient instance for making requests.
81
- name (str): name of the key.
82
- user_id (int, optional): Specific user. privileges required.
83
-
84
- Returns:
85
- ApiKey: the apikey object
86
-
87
- Example:
88
- >>> from geobox import GeoboxClient
89
- >>> from geobox.apikey import ApiKey
90
- >>> client = GeoboxClient()
91
- >>> apikey = ApiKey.create_apikey(client, name='test')
92
- or
93
- >>> apikey = client.create_apikey(name='test')
94
- """
95
- data = clean_data({
96
- 'name': name,
97
- 'user_id': user_id
98
- })
99
- response = api.post(cls.BASE_ENDPOINT, payload=data, is_json=False)
100
- return ApiKey(api, response['id'], response)
101
-
102
-
103
- @classmethod
104
- def get_apikey(cls, api: 'GeoboxClient', key_id: int) -> 'ApiKey':
105
- """
106
- Get an ApiKey
107
-
108
- Args:
109
- api (GeoboxClient): The GeoboxClient instance for making requests.
110
- key_id (str): the id of the apikey.
111
-
112
- Returns:
113
- ApiKey: the ApiKey object
114
-
115
- Example:
116
- >>> from geobox import GeoboxClient
117
- >>> from geobox.apikey import ApiKey
118
- >>> client = GeoboxClient()
119
- >>> apikey = ApiKey.get_apikey(client, key_id=1)
120
- or
121
- >>> apikey = client.get_apikey(key_id=1)
122
- """
123
- params = {
124
- 'f': 'json'
125
- }
126
- return super()._get_detail(api=api,
127
- endpoint=cls.BASE_ENDPOINT,
128
- uuid=key_id,
129
- params=params,
130
- factory_func=lambda api, item: ApiKey(api, item['id'], item))
131
-
132
-
133
- @classmethod
134
- def get_apikey_by_name(cls, api: 'GeoboxClient', name: str, user_id: int = None) -> 'ApiKey':
135
- """
136
- Get an ApiKey by name
137
-
138
- Args:
139
- api (GeoboxClient): The GeoboxClient instance for making requests.
140
- name (str): the name of the key to get
141
- user_id (int, optional): specific user. privileges required.
142
-
143
- Returns:
144
- ApiKey | None: returns the key if a key matches the given name, else None
145
-
146
- Example:
147
- >>> from geobox import GeoboxClient
148
- >>> from geobox.apikey import ApiKey
149
- >>> client = GeoboxClient()
150
- >>> apikey = ApiKey.get_apikey_by_name(client, name='test')
151
- or
152
- >>> apikey = client.get_apikey_by_name(name='test')
153
- """
154
- apikeys = cls.get_apikeys(api, search=name, user_id=user_id)
155
- if apikeys and apikeys[0].name == name:
156
- return apikeys[0]
157
- else:
158
- return None
159
-
160
-
161
- def update(self, name: str, user_id: int = None) -> Dict:
162
- """
163
- Update an ApiKey
164
-
165
- Args:
166
- name (str): the name of the key
167
- user_id (int, optional): Specific user. privileges required.
168
-
169
- Returns:
170
- Dict: Updated ApiKey data
171
-
172
- Example:
173
- >>> from geobox import GeoboxClient
174
- >>> from geobox.apikey import ApiKey
175
- >>> client = GeoboxClient()
176
- >>> apikey = ApiKey.get_apikey(client, key_id=1)
177
- >>> apikey.update(name="updated_name")
178
- """
179
- data = {
180
- "name": name,
181
- "user_id": user_id
182
- }
183
- return super()._update(self.endpoint, data)
184
-
185
-
186
- def delete(self) -> None:
187
- """
188
- Delete the ApiKey.
189
-
190
- Returns:
191
- None
192
-
193
- Example:
194
- >>> from geobox import GeoboxClient
195
- >>> from geobox.apikey import ApiKey
196
- >>> client = GeoboxClient()
197
- >>> apikey = ApiKey.get_apikey(client, key_id=1)
198
- >>> apikey.delete()
199
- """
200
- super().delete(self.endpoint)
201
- self.key_id = None
202
-
203
-
204
- def revoke(self) -> None:
205
- """
206
- Revoke an ApiKey
207
-
208
- Example:
209
- >>> from geobox import GeoboxClient
210
- >>> from geobox.apikey import ApiKey
211
- >>> client = GeoboxClient()
212
- >>> apikey = ApiKey.get_apikey(client, key_id=1)
213
- >>> apikey.revoke()
214
- """
215
- endpoint = f"{self.endpoint}/revoke"
216
- self.api.post(endpoint)
217
- self.data['revoked'] = True
218
-
219
-
220
- def grant(self) -> None:
221
- """
222
- Grant an ApiKey
223
-
224
- Example:
225
- >>> from geobox import GeoboxClient
226
- >>> from geobox.apikey import ApiKey
227
- >>> client = GeoboxClient()
228
- >>> apikey = ApiKey.get_apikey(client, key_id=1)
229
- >>> apikey.grant()
230
- """
231
- endpoint = f"{self.endpoint}/grant"
232
- self.api.post(endpoint)
1
+ from typing import List, Dict, Optional, TYPE_CHECKING
2
+ from urllib.parse import urljoin
3
+
4
+ from .base import Base
5
+ from .utils import clean_data
6
+
7
+ if TYPE_CHECKING:
8
+ from . import GeoboxClient
9
+
10
+ class ApiKey(Base):
11
+
12
+ BASE_ENDPOINT = 'apikeys/'
13
+
14
+ def __init__(self,
15
+ api: 'GeoboxClient',
16
+ key_id: int,
17
+ data: Optional[Dict] = {}):
18
+ """
19
+ Initialize an apikey instance.
20
+
21
+ Args:
22
+ api (GeoboxClient): The GeoboxClient instance for making requests.
23
+ key_id (int): The unique identifier for the apikey.
24
+ data (Dict, optional): The data of the apikey.
25
+ """
26
+ super().__init__(api, data=data)
27
+ self.key_id = key_id
28
+ self.endpoint = urljoin(self.BASE_ENDPOINT, str(self.id))
29
+
30
+
31
+ def __repr__(self) -> str:
32
+ """
33
+ Return a string representation of the attachment.
34
+
35
+ Returns:
36
+ str: The string representation of the attachment.
37
+ """
38
+ return f'ApiKey(name={self.name}, revoked={self.revoked})'
39
+
40
+
41
+ @classmethod
42
+ def get_apikeys(cls, api: 'GeoboxClient', **kwargs) -> List['ApiKey']:
43
+ """
44
+ Get a list of apikeys
45
+
46
+ Args:
47
+ api (GeoboxClient): The GeoboxClient instance for making requests.
48
+
49
+ Keyword Args:
50
+ search (str): search term for keyword-based searching among all textual fields.
51
+ order_by (str): comma separated list of fields for sorting results [field1 A|D, field2 A|D, …]. e.g. name A, type D. NOTE: "A" denotes ascending order and "D" denotes descending order.
52
+ skip (int): Number of layers to skip. default is 0.
53
+ limit (int): Maximum number of layers to return. default is 10.
54
+ user_id (int): Specific user. privileges required.
55
+
56
+ Example:
57
+ >>> from geobox import GeoboxClient
58
+ >>> from geobox.apikey import ApiKey
59
+ >>> client = GeoboxClient()
60
+ >>> apikeys = ApiKey.get_apikeys(client)
61
+ or
62
+ >>> apikeys = client.get_apikeys()
63
+ """
64
+ params = {
65
+ 'search': kwargs.get('search'),
66
+ 'order_by': kwargs.get('order_by'),
67
+ 'skip': kwargs.get('skip'),
68
+ 'limit': kwargs.get('limit'),
69
+ 'user_id': kwargs.get('user_id')
70
+ }
71
+ return super()._get_list(api, cls.BASE_ENDPOINT, params, factory_func=lambda api, item: ApiKey(api, item['id'], item))
72
+
73
+
74
+ @classmethod
75
+ def create_apikey(cls, api: 'GeoboxClient', name: str, user_id: int = None) -> 'ApiKey':
76
+ """
77
+ Create an ApiKey
78
+
79
+ Args:
80
+ api (GeoboxClient): The GeoboxClient instance for making requests.
81
+ name (str): name of the key.
82
+ user_id (int, optional): Specific user. privileges required.
83
+
84
+ Returns:
85
+ ApiKey: the apikey object
86
+
87
+ Example:
88
+ >>> from geobox import GeoboxClient
89
+ >>> from geobox.apikey import ApiKey
90
+ >>> client = GeoboxClient()
91
+ >>> apikey = ApiKey.create_apikey(client, name='test')
92
+ or
93
+ >>> apikey = client.create_apikey(name='test')
94
+ """
95
+ data = clean_data({
96
+ 'name': name,
97
+ 'user_id': user_id
98
+ })
99
+ response = api.post(cls.BASE_ENDPOINT, payload=data, is_json=False)
100
+ return ApiKey(api, response['id'], response)
101
+
102
+
103
+ @classmethod
104
+ def get_apikey(cls, api: 'GeoboxClient', key_id: int) -> 'ApiKey':
105
+ """
106
+ Get an ApiKey
107
+
108
+ Args:
109
+ api (GeoboxClient): The GeoboxClient instance for making requests.
110
+ key_id (str): the id of the apikey.
111
+
112
+ Returns:
113
+ ApiKey: the ApiKey object
114
+
115
+ Example:
116
+ >>> from geobox import GeoboxClient
117
+ >>> from geobox.apikey import ApiKey
118
+ >>> client = GeoboxClient()
119
+ >>> apikey = ApiKey.get_apikey(client, key_id=1)
120
+ or
121
+ >>> apikey = client.get_apikey(key_id=1)
122
+ """
123
+ params = {
124
+ 'f': 'json'
125
+ }
126
+ return super()._get_detail(api=api,
127
+ endpoint=cls.BASE_ENDPOINT,
128
+ uuid=key_id,
129
+ params=params,
130
+ factory_func=lambda api, item: ApiKey(api, item['id'], item))
131
+
132
+
133
+ @classmethod
134
+ def get_apikey_by_name(cls, api: 'GeoboxClient', name: str, user_id: int = None) -> 'ApiKey':
135
+ """
136
+ Get an ApiKey by name
137
+
138
+ Args:
139
+ api (GeoboxClient): The GeoboxClient instance for making requests.
140
+ name (str): the name of the key to get
141
+ user_id (int, optional): specific user. privileges required.
142
+
143
+ Returns:
144
+ ApiKey | None: returns the key if a key matches the given name, else None
145
+
146
+ Example:
147
+ >>> from geobox import GeoboxClient
148
+ >>> from geobox.apikey import ApiKey
149
+ >>> client = GeoboxClient()
150
+ >>> apikey = ApiKey.get_apikey_by_name(client, name='test')
151
+ or
152
+ >>> apikey = client.get_apikey_by_name(name='test')
153
+ """
154
+ apikeys = cls.get_apikeys(api, search=name, user_id=user_id)
155
+ if apikeys and apikeys[0].name == name:
156
+ return apikeys[0]
157
+ else:
158
+ return None
159
+
160
+
161
+ def update(self, name: str, user_id: int = None) -> Dict:
162
+ """
163
+ Update an ApiKey
164
+
165
+ Args:
166
+ name (str): the name of the key
167
+ user_id (int, optional): Specific user. privileges required.
168
+
169
+ Returns:
170
+ Dict: Updated ApiKey data
171
+
172
+ Example:
173
+ >>> from geobox import GeoboxClient
174
+ >>> from geobox.apikey import ApiKey
175
+ >>> client = GeoboxClient()
176
+ >>> apikey = ApiKey.get_apikey(client, key_id=1)
177
+ >>> apikey.update(name="updated_name")
178
+ """
179
+ data = {
180
+ "name": name,
181
+ "user_id": user_id
182
+ }
183
+ return super()._update(self.endpoint, data)
184
+
185
+
186
+ def delete(self) -> None:
187
+ """
188
+ Delete the ApiKey.
189
+
190
+ Returns:
191
+ None
192
+
193
+ Example:
194
+ >>> from geobox import GeoboxClient
195
+ >>> from geobox.apikey import ApiKey
196
+ >>> client = GeoboxClient()
197
+ >>> apikey = ApiKey.get_apikey(client, key_id=1)
198
+ >>> apikey.delete()
199
+ """
200
+ super().delete(self.endpoint)
201
+ self.key_id = None
202
+
203
+
204
+ def revoke(self) -> None:
205
+ """
206
+ Revoke an ApiKey
207
+
208
+ Example:
209
+ >>> from geobox import GeoboxClient
210
+ >>> from geobox.apikey import ApiKey
211
+ >>> client = GeoboxClient()
212
+ >>> apikey = ApiKey.get_apikey(client, key_id=1)
213
+ >>> apikey.revoke()
214
+ """
215
+ endpoint = f"{self.endpoint}/revoke"
216
+ self.api.post(endpoint)
217
+ self.data['revoked'] = True
218
+
219
+
220
+ def grant(self) -> None:
221
+ """
222
+ Grant an ApiKey
223
+
224
+ Example:
225
+ >>> from geobox import GeoboxClient
226
+ >>> from geobox.apikey import ApiKey
227
+ >>> client = GeoboxClient()
228
+ >>> apikey = ApiKey.get_apikey(client, key_id=1)
229
+ >>> apikey.grant()
230
+ """
231
+ endpoint = f"{self.endpoint}/grant"
232
+ self.api.post(endpoint)
233
233
  self.data['revoked'] = False