Skip to content

Commit

Permalink
Beam Support in SDK (#96)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
corke2013 and Keegan Cordeiro authored Jan 20, 2025
1 parent 90486e4 commit 4272c06
Show file tree
Hide file tree
Showing 29 changed files with 3,917 additions and 12 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,36 @@ suggested_radius = phq.radius.search(location__origin="45.5051,-122.6750")
print(suggested_radius.radius, suggested_radius.radius_unit, suggested_radius.location.model_dump(exclude_none=True))
```

### Beam endpoints

Get Analysis.

Additional examples are available in [usecases/beam/analysis](https://github.com/predicthq/sdk-py/tree/master/usecases/beam/analysis) folder.

```Python
from predicthq import Client

phq = Client(access_token="abc123")


analysis = phq.beam.analysis.get(analysis_id="abc123")
print(analysis.model_dump(exclude_none=True))
```

Get Analysis Group.

Additional examples are available in [usecases/beam/analysis_group](https://github.com/predicthq/sdk-py/tree/master/usecases/beam/analysis_group) folder.

```Python
from predicthq import Client

phq = Client(access_token="abc123")


analysis_group = phq.beam.analysis_group.get(group_id="abc123")
print(analysis_group.model_dump(exlcude_none=True))
```

### Serializing search results into a dictionary

All search results can be serialized into a dictionary using the `.model_dump()` method call.
Expand Down
3 changes: 2 additions & 1 deletion predicthq/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Client(object):
@classmethod
def build_url(cls, path):
result = list(urlparse(path))
result[2] = f"/{result[2].strip('/')}/"
result[2] = f"/{result[2].strip('/')}"
return urljoin(config.ENDPOINT_URL, urlunparse(result))

def __init__(self, access_token=None):
Expand All @@ -35,6 +35,7 @@ def initialize_endpoints(self):
self.accounts = endpoints.AccountsEndpoint(proxy(self))
self.places = endpoints.PlacesEndpoint(proxy(self))
self.radius = endpoints.SuggestedRadiusEndpoint(proxy(self))
self.beam = endpoints.BeamEndpoint(proxy(self))

def get_headers(self, headers):
_headers = {
Expand Down
2 changes: 2 additions & 0 deletions predicthq/endpoints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .v1.features import FeaturesEndpoint
from .v1.places import PlacesEndpoint
from .v1.radius import SuggestedRadiusEndpoint
from .v1.beam import BeamEndpoint


__all__ = [
Expand All @@ -15,4 +16,5 @@
"FeaturesEndpoint",
"PlacesEndpoint",
"SuggestedRadiusEndpoint",
"BeamEndpoint",
]
13 changes: 9 additions & 4 deletions predicthq/endpoints/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from predicthq.exceptions import ValidationError

from predicthq.endpoints.schemas import ArgKwargResultSet


def _kwargs_to_key_list_mapping(kwargs, separator="__"):
"""
Expand Down Expand Up @@ -41,16 +43,17 @@ def _to_url_params(key_list_mapping, glue=".", separator=",", parent_key=""):
return params


def _to_json(key_list_mapping):
def _to_json(key_list_mapping, json=None):
"""
Converts key_list_mapping to json
"""
json = {}
if json is None:
json = {}
for key, value in key_list_mapping.items():
for v in value:
json[key] = dict() if not json.get(key) else json[key]
if isinstance(v, dict):
json[key].update(_to_json(v))
_to_json(v, json[key])
else:
json[key] = v
return json
Expand Down Expand Up @@ -80,14 +83,16 @@ def returns(model_class):
def decorator(f):
@functools.wraps(f)
def wrapper(endpoint, *args, **kwargs):

model = getattr(endpoint.Meta, f.__name__, {}).get("returns", model_class)

data = f(endpoint, *args, **kwargs)
try:
loaded_model = model(**data)
loaded_model._more = functools.partial(wrapper, endpoint)
loaded_model._endpoint = endpoint
if isinstance(loaded_model, ArgKwargResultSet):
loaded_model._args = args
loaded_model._kwargs = kwargs
return loaded_model
except PydanticValidationError as e:
raise ValidationError(e)
Expand Down
6 changes: 5 additions & 1 deletion predicthq/endpoints/schemas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from urllib.parse import parse_qsl, urlparse
from typing import Callable, Optional

from pydantic import BaseModel, HttpUrl
Expand Down Expand Up @@ -59,3 +58,8 @@ def iter_all(self):

def __iter__(self):
return self.iter_items()


class ArgKwargResultSet(ResultSet):
_args: Optional[dict] = None
_kwargs: Optional[dict] = None
5 changes: 5 additions & 0 deletions predicthq/endpoints/v1/beam/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .endpoint import BeamEndpoint
from .schemas import Analysis, AnalysisGroup


__all__ = ["BeamEndpoint", "Analysis", "AnalysisGroup"]
Loading

0 comments on commit 4272c06

Please sign in to comment.