|
1 | 1 | """ |
2 | 2 | Build requests to send to the API. |
3 | 3 | """ |
| 4 | +import json |
4 | 5 | import warnings |
5 | 6 |
|
6 | 7 | import requests |
7 | 8 |
|
8 | | -from .result import Parser |
| 9 | +from exonetapi.structures import ApiResourceSet |
9 | 10 | from exonetapi.exceptions.ValidationException import ValidationException |
| 11 | +from .result import Parser |
10 | 12 |
|
11 | 13 |
|
12 | 14 | class RequestBuilder(object): |
13 | 15 | """Create and make requests to the API. |
14 | 16 | """ |
15 | 17 |
|
16 | | - def __init__(self, resource, client=None): |
17 | | - if not resource.startswith('/'): |
| 18 | + def __init__(self, resource=None, client=None): |
| 19 | + if resource is not None and not resource.startswith('/'): |
18 | 20 | resource = '/' + resource |
19 | 21 |
|
20 | 22 | self.__resource = resource |
@@ -98,9 +100,8 @@ def get(self, identifier=None): |
98 | 100 |
|
99 | 101 | return Parser(response.content).parse() |
100 | 102 |
|
101 | | - def store(self, resource): |
102 | | - warnings.warn("store() is deprecated; use post().", DeprecationWarning) |
103 | | - self.post(resource) |
| 103 | + def get_recursive(self): |
| 104 | + return self.__get_recursive() |
104 | 105 |
|
105 | 106 | def post(self, resource): |
106 | 107 | """Make a POST request to the API with the provided resource as data. |
@@ -176,7 +177,10 @@ def __build_url(self, identifier=None): |
176 | 177 |
|
177 | 178 | :return: A URL. |
178 | 179 | """ |
179 | | - url = self.__client.get_host() + self.__resource |
| 180 | + url = self.__client.get_host() |
| 181 | + |
| 182 | + if self.__resource is not None: |
| 183 | + url += self.__resource |
180 | 184 |
|
181 | 185 | if identifier: |
182 | 186 | url += '/' + identifier |
@@ -211,3 +215,32 @@ def __make_call(self, method, url, json_data=None, params=None): |
211 | 215 | response.raise_for_status() |
212 | 216 |
|
213 | 217 | return response |
| 218 | + |
| 219 | + def __get_recursive(self, data=None, url=None): |
| 220 | + """ |
| 221 | + Get the URL and call this method recursivly as long as there is an URL in the 'next' field |
| 222 | + of the 'links' data. |
| 223 | +
|
| 224 | + :param data: The ApiResourceSet to append the resources to. |
| 225 | + :param url: The URL to call. |
| 226 | + :return: The ApiResourceSet containing all requested resources. |
| 227 | + """ |
| 228 | + response = self.__make_call( |
| 229 | + 'GET', |
| 230 | + url or self.__build_url(), |
| 231 | + params=self.__query_params if not url else None |
| 232 | + ) |
| 233 | + |
| 234 | + content = Parser(response.content).parse() |
| 235 | + |
| 236 | + if data is None: |
| 237 | + data = ApiResourceSet() |
| 238 | + data.set_meta(content.meta().copy()) |
| 239 | + |
| 240 | + data.add_resource(content.resources()) |
| 241 | + |
| 242 | + next_link = content.links().get('next') |
| 243 | + if next_link is not None: |
| 244 | + return self.__get_recursive(data, next_link) |
| 245 | + |
| 246 | + return data |
0 commit comments