Skip to content
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",
]
8 changes: 6 additions & 2 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,7 +43,7 @@ def _to_url_params(key_list_mapping, glue=".", separator=",", parent_key=""):
return params


def _to_json(key_list_mapping, json = None):
def _to_json(key_list_mapping, json=None):
"""
Converts key_list_mapping to json
"""
Expand Down Expand Up @@ -81,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