99class API :
1010 """Represents a configured API interface to the Data Commons API.
1111
12- This class handles environment setup, resolving the base URL, building headers,
13- or optionally using a fully qualified URL directly. It can be used standalone
14- to interact with the API or in combination with Endpoint classes.
15- """
12+ This class handles environment setup, resolving the base URL, building headers,
13+ or optionally using a fully qualified URL directly. It can be used standalone
14+ to interact with the API or in combination with Endpoint classes.
15+ """
1616
1717 def __init__ (
1818 self ,
@@ -21,19 +21,19 @@ def __init__(
2121 url : Optional [str ] = None ,
2222 ):
2323 """
24- Initializes the API instance.
25-
26- Args:
27- api_key: The API key for authentication. Defaults to None.
28- dc_instance: The Data Commons instance domain. Ignored if `url` is provided.
29- Defaults to 'datacommons.org' if both `url` and `dc_instance` are None.
30- url: A fully qualified URL for the base API. This may be useful if more granular control
31- of the API is required (for local development, for example). If provided, dc_instance`
32- should not be provided.
33-
34- Raises:
35- ValueError: If both `dc_instance` and `url` are provided.
36- """
24+ Initializes the API instance.
25+
26+ Args:
27+ api_key: The API key for authentication. Defaults to None.
28+ dc_instance: The Data Commons instance domain. Ignored if `url` is provided.
29+ Defaults to 'datacommons.org' if both `url` and `dc_instance` are None.
30+ url: A fully qualified URL for the base API. This may be useful if more granular control
31+ of the API is required (for local development, for example). If provided, dc_instance`
32+ should not be provided.
33+
34+ Raises:
35+ ValueError: If both `dc_instance` and `url` are provided.
36+ """
3737 if dc_instance and url :
3838 raise ValueError ("Cannot provide both `dc_instance` and `url`." )
3939
@@ -52,81 +52,105 @@ def __init__(
5252 def __repr__ (self ) -> str :
5353 """Returns a readable representation of the API object.
5454
55- Indicates the base URL and if it's authenticated.
55+ Indicates the base URL and if it's authenticated.
5656
57- Returns:
58- str: A string representation of the API object.
59- """
57+ Returns:
58+ str: A string representation of the API object.
59+ """
6060 has_auth = " (Authenticated)" if "X-API-Key" in self .headers else ""
6161 return f"<API at { self .base_url } { has_auth } >"
6262
63- def post (self ,
64- payload : dict [str , Any ],
65- endpoint : Optional [str ] = None ) -> Dict [str , Any ]:
63+ def post (
64+ self ,
65+ payload : dict [str , Any ],
66+ endpoint : Optional [str ] = None ,
67+ * ,
68+ all_pages : bool = True ,
69+ next_token : Optional [str ] = None ,
70+ ) -> Dict [str , Any ]:
6671 """Makes a POST request using the configured API environment.
6772
68- If `endpoint` is provided, it will be appended to the base_url. Otherwise,
69- it will just POST to the base URL.
73+ If `endpoint` is provided, it will be appended to the base_url. Otherwise,
74+ it will just POST to the base URL.
7075
71- Args:
72- payload: The JSON payload for the POST request.
73- endpoint: An optional endpoint path to append to the base URL.
76+ Args:
77+ payload: The JSON payload for the POST request.
78+ endpoint: An optional endpoint path to append to the base URL.
79+ all_pages: If True, fetch all pages of the response. If False, fetch only the first page.
80+ Defaults to True. Set to False to only fetch the first page. In that case, a
81+ `next_token` key in the response will indicate if more pages are available.
82+ That token can be used to fetch the next page.
7483
75- Returns:
76- A dictionary containing the merged response data.
84+ Returns:
85+ A dictionary containing the merged response data.
7786
78- Raises:
79- ValueError: If the payload is not a valid dictionary.
80- """
87+ Raises:
88+ ValueError: If the payload is not a valid dictionary.
89+ """
8190 if not isinstance (payload , dict ):
8291 raise ValueError ("Payload must be a dictionary." )
8392
8493 url = (self .base_url if endpoint is None else f"{ self .base_url } /{ endpoint } " )
85- return post_request (url = url , payload = payload , headers = self .headers )
94+ return post_request (url = url ,
95+ payload = payload ,
96+ headers = self .headers ,
97+ all_pages = all_pages ,
98+ next_token = next_token )
8699
87100
88101class Endpoint :
89102 """Represents a specific endpoint within the Data Commons API.
90103
91- This class leverages an API instance to make requests. It does not
92- handle instance resolution or headers directly; that is delegated to the API instance.
104+ This class leverages an API instance to make requests. It does not
105+ handle instance resolution or headers directly; that is delegated to the API instance.
93106
94- Attributes:
95- endpoint (str): The endpoint path (e.g., 'node').
96- api (API): The API instance providing configuration and the `post` method.
97- """
107+ Attributes:
108+ endpoint (str): The endpoint path (e.g., 'node').
109+ api (API): The API instance providing configuration and the `post` method.
110+ """
98111
99112 def __init__ (self , endpoint : str , api : API ):
100113 """
101- Initializes the Endpoint instance.
114+ Initializes the Endpoint instance.
102115
103- Args:
104- endpoint: The endpoint path (e.g., 'node').
105- api: An API instance that provides the environment configuration.
106- """
116+ Args:
117+ endpoint: The endpoint path (e.g., 'node').
118+ api: An API instance that provides the environment configuration.
119+ """
107120 self .endpoint = endpoint
108121 self .api = api
109122
110123 def __repr__ (self ) -> str :
111124 """Returns a readable representation of the Endpoint object.
112125
113- Shows the endpoint and underlying API configuration.
126+ Shows the endpoint and underlying API configuration.
114127
115- Returns:
116- str: A string representation of the Endpoint object.
117- """
128+ Returns:
129+ str: A string representation of the Endpoint object.
130+ """
118131 return f"<{ self .endpoint .title ()} Endpoint using { repr (self .api )} >"
119132
120- def post (self , payload : dict [str , Any ]) -> Dict [str , Any ]:
133+ def post (self ,
134+ payload : dict [str , Any ],
135+ all_pages : bool = True ,
136+ next_token : Optional [str ] = None ) -> Dict [str , Any ]:
121137 """Makes a POST request to the specified endpoint using the API instance.
122138
123- Args:
124- payload: The JSON payload for the POST request.
139+ Args:
140+ payload: The JSON payload for the POST request.
141+ all_pages: If True, fetch all pages of the response. If False, fetch only the first page.
142+ Defaults to True. Set to False to only fetch the first page. In that case, a
143+ `next_token` key in the response will indicate if more pages are available.
144+ That token can be used to fetch the next page.
145+ next_token: Optionally, the token to fetch the next page of results. Defaults to None.
125146
126- Returns:
127- A dictionary with the merged API response data.
147+ Returns:
148+ A dictionary with the merged API response data.
128149
129- Raises:
130- ValueError: If the payload is not a valid dictionary.
131- """
132- return self .api .post (payload = payload , endpoint = self .endpoint )
150+ Raises:
151+ ValueError: If the payload is not a valid dictionary.
152+ """
153+ return self .api .post (payload = payload ,
154+ endpoint = self .endpoint ,
155+ all_pages = all_pages ,
156+ next_token = next_token )
0 commit comments