Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beam Support in SDK #96

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading