Skip to content

Commit 4272c06

Browse files
corke2013Keegan Cordeiro
andauthored
Beam Support in SDK (#96)
* fix bug in to_json function * Beam analysis endpoint (#98) * add beam schema * add beam analysis endpoint * add BeamEndpoint import * update schema for python 3.8 * update schema for python 3.8 * update endpoints for python 3.8 * fix analysis endpoints and schema * add analysis group endpoints (#99) * add analysis group endpoints * add analysis group import * python 3.8 compat * fix signature * use optional type hint --------- Co-authored-by: Keegan Cordeiro <[email protected]> * Beam sink endpoint (#100) * beam sink endpoint - allow json, csv and ndjson upload * expose beam endpoint in sdk * allow extra fields so that new fields are visible without an update to the SDK * remove undocumented endpoints * add features-api to_csv mixin * remove accidental print statement * fix typing for python 3.8 * update build url test * fix bug in features api endpoint when using iter_all * update feature api tests * update readme, add examples and missing param to beam update function * add beam endpoint tests (#102) * add beam endpoint tests * address comments - use correct fixtures --------- Co-authored-by: Keegan Cordeiro <[email protected]> * add comment regarding features-api * Pagination (#103) * add ArgKwargResult set for custom pagination * use custom pagination on all supported beam endpoints --------- Co-authored-by: Keegan Cordeiro <[email protected]> --------- Co-authored-by: Keegan Cordeiro <[email protected]> --------- Co-authored-by: Keegan Cordeiro <[email protected]> * fix bug in upload demand endpoint * use config dict on each model --------- Co-authored-by: Keegan Cordeiro <[email protected]>
1 parent 90486e4 commit 4272c06

29 files changed

+3917
-12
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,36 @@ suggested_radius = phq.radius.search(location__origin="45.5051,-122.6750")
217217
print(suggested_radius.radius, suggested_radius.radius_unit, suggested_radius.location.model_dump(exclude_none=True))
218218
```
219219

220+
### Beam endpoints
221+
222+
Get Analysis.
223+
224+
Additional examples are available in [usecases/beam/analysis](https://github.com/predicthq/sdk-py/tree/master/usecases/beam/analysis) folder.
225+
226+
```Python
227+
from predicthq import Client
228+
229+
phq = Client(access_token="abc123")
230+
231+
232+
analysis = phq.beam.analysis.get(analysis_id="abc123")
233+
print(analysis.model_dump(exclude_none=True))
234+
```
235+
236+
Get Analysis Group.
237+
238+
Additional examples are available in [usecases/beam/analysis_group](https://github.com/predicthq/sdk-py/tree/master/usecases/beam/analysis_group) folder.
239+
240+
```Python
241+
from predicthq import Client
242+
243+
phq = Client(access_token="abc123")
244+
245+
246+
analysis_group = phq.beam.analysis_group.get(group_id="abc123")
247+
print(analysis_group.model_dump(exlcude_none=True))
248+
```
249+
220250
### Serializing search results into a dictionary
221251

222252
All search results can be serialized into a dictionary using the `.model_dump()` method call.

predicthq/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Client(object):
1616
@classmethod
1717
def build_url(cls, path):
1818
result = list(urlparse(path))
19-
result[2] = f"/{result[2].strip('/')}/"
19+
result[2] = f"/{result[2].strip('/')}"
2020
return urljoin(config.ENDPOINT_URL, urlunparse(result))
2121

2222
def __init__(self, access_token=None):
@@ -35,6 +35,7 @@ def initialize_endpoints(self):
3535
self.accounts = endpoints.AccountsEndpoint(proxy(self))
3636
self.places = endpoints.PlacesEndpoint(proxy(self))
3737
self.radius = endpoints.SuggestedRadiusEndpoint(proxy(self))
38+
self.beam = endpoints.BeamEndpoint(proxy(self))
3839

3940
def get_headers(self, headers):
4041
_headers = {

predicthq/endpoints/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .v1.features import FeaturesEndpoint
66
from .v1.places import PlacesEndpoint
77
from .v1.radius import SuggestedRadiusEndpoint
8+
from .v1.beam import BeamEndpoint
89

910

1011
__all__ = [
@@ -15,4 +16,5 @@
1516
"FeaturesEndpoint",
1617
"PlacesEndpoint",
1718
"SuggestedRadiusEndpoint",
19+
"BeamEndpoint",
1820
]

predicthq/endpoints/decorators.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from predicthq.exceptions import ValidationError
66

7+
from predicthq.endpoints.schemas import ArgKwargResultSet
8+
79

810
def _kwargs_to_key_list_mapping(kwargs, separator="__"):
911
"""
@@ -41,16 +43,17 @@ def _to_url_params(key_list_mapping, glue=".", separator=",", parent_key=""):
4143
return params
4244

4345

44-
def _to_json(key_list_mapping):
46+
def _to_json(key_list_mapping, json=None):
4547
"""
4648
Converts key_list_mapping to json
4749
"""
48-
json = {}
50+
if json is None:
51+
json = {}
4952
for key, value in key_list_mapping.items():
5053
for v in value:
5154
json[key] = dict() if not json.get(key) else json[key]
5255
if isinstance(v, dict):
53-
json[key].update(_to_json(v))
56+
_to_json(v, json[key])
5457
else:
5558
json[key] = v
5659
return json
@@ -80,14 +83,16 @@ def returns(model_class):
8083
def decorator(f):
8184
@functools.wraps(f)
8285
def wrapper(endpoint, *args, **kwargs):
83-
8486
model = getattr(endpoint.Meta, f.__name__, {}).get("returns", model_class)
8587

8688
data = f(endpoint, *args, **kwargs)
8789
try:
8890
loaded_model = model(**data)
8991
loaded_model._more = functools.partial(wrapper, endpoint)
9092
loaded_model._endpoint = endpoint
93+
if isinstance(loaded_model, ArgKwargResultSet):
94+
loaded_model._args = args
95+
loaded_model._kwargs = kwargs
9196
return loaded_model
9297
except PydanticValidationError as e:
9398
raise ValidationError(e)

predicthq/endpoints/schemas.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from urllib.parse import parse_qsl, urlparse
21
from typing import Callable, Optional
32

43
from pydantic import BaseModel, HttpUrl
@@ -59,3 +58,8 @@ def iter_all(self):
5958

6059
def __iter__(self):
6160
return self.iter_items()
61+
62+
63+
class ArgKwargResultSet(ResultSet):
64+
_args: Optional[dict] = None
65+
_kwargs: Optional[dict] = None
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .endpoint import BeamEndpoint
2+
from .schemas import Analysis, AnalysisGroup
3+
4+
5+
__all__ = ["BeamEndpoint", "Analysis", "AnalysisGroup"]

0 commit comments

Comments
 (0)