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

Add EndpointPayload ABC and Specific Payload Dataclasses for API Requests #208

Open
wants to merge 24 commits into
base: master
Choose a base branch
from

Conversation

jm-rivera
Copy link

Note:
First PR in the stack: #202
Second PR in the stack: #203
Third PR in the stack: #204
Fourth PR in the stack #205
Previous PR in the stack #206

Will manually rebase as PRs in the stack are merged to make it easier to review the changes introduced in this PR.


This PR adds 'payload' dataclasses for the API endpoints.

The EndpointPayload class is an Abstract Base Class, which defines the structure for all other payload classes. Payload classes are in charge of normalizing and validating the payload before it is used for a POST request. They also implement a .to_dict property for easy access to the correctly structured payload data as a python dictionary.

Details:

  • datacommons_client/endpoints/payloads.py: Added abstract base class EndpointPayload and specific payload classes (NodePayload, ObservationPayload, ResolvePayload, SparqlPayload) to structure, normalize, and validate payloads for different API requests.

Tests

Adds minimal structure within a new client folder
Adds:
- A Node dataclass which models an individual node (with dcid, name, etc)
- A NodeGroup dataclass which represents a list of Node objects
- An Arcs dataclass, which represents arcs (with a label and containing NodeGroups)
- A Properties dataclass which represents a list of Properties
Adds:
- An Observation dataclass which models an individual observation (date, value)
- An OrderedFacets dataclass which models the 'ordered facets' of observations
- A Variable dataclass which represents a variable data (grouped by entity)
- A Facet class which represents the metadata for a facet
Adds:
- A candidate dataclass to model candidates in the response (with dcid and dominanType)
- An Entity dataclass to model entities with their resolution candidates
Adds:
- A Cell dataclass to model a single cell in a row
- A Row modelling a row with cells.
Adds tests for node, observation, resolve and sparql models.
DCResponse just wraps around the api response
isort imports with google profile
Validates instance.
Builds header.
Manages post requests, including multi-page responses
Fixes a typo on the return type of the post_request function
- EndpointPayload as an ABC
- NodePayload to model the required payload for node calls
- Observation Payload to model the required payload for observation calls (additionally enums for valid observation select fields and valid date strings)
-ResolvePayload to model the required payload for resolve calls
- SparqlPayload to model the required payload for Sparql calls
@jm-rivera jm-rivera requested a review from beets December 9, 2024 09:35
@jm-rivera jm-rivera self-assigned this Dec 9, 2024
Copy link

google-cla bot commented Dec 9, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link

@dwnoble dwnoble left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jm-rivera ! To make things a little simpler with this stack of PRs, it may be easier to just address all comments in this PR (208). what do you think?

@dataclass
class NodePayload(EndpointPayload):
"""
A dataclass to structure, normalize, and validate the payload for a node API request.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you note that this is specifically for the /v2/node endpoint?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for the other payload below

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants