C. Example code – Retrieve page 2 of Change Requests
Appendix C. Example code – Retrieve page 2 of Change Requests
This example retrieves the second trench of Change requests returned by the Change endpoint. The page parameter is 0-based and the page size is set to 10. As no search parameters are included, the endpoint simply returns all change requests in an unspecified order.
This program will use the following endpoints: Change/.
Change/
All parameters for this endpoint are optional as can be seen in Figure 14.
However, for the purposes here, we are going to set the pageSize to 10 and the requested page to 1. This will retrieve change records 11-20 from the database.
The returned information from the /v1/Change/ API endpoint is, according to Swagger:
{ "Results": [ { "Code": 0, "Links": [ { "rel": "string", "href": "string" } ], "Name": "string", "Remark": "string", "DataSource": "string", "InformationSource": "string", "RevisionDate": "2021-05-26T11:24:54.029Z", "Deprecations": [ { "Id": 0, "Date": "2021-05-26T11:24:54.029Z", "ChangeId": 0, "ReplacedBy": { "Code": 0, "Name": "string", "href": "string" }, "Reason": "string" } ], "Supersessions": [ { "Id": 0, "SupersededBy": { "Code": 0, "Name": "string", "href": "string" }, "Year": 0, "Remarks": "string" } ] } ], "Count": 0, "Page": 0, "PageSize": 0, "TotalResults": 0, "Links": [ { "rel": "string", "href": "string" } ] } |
This indicates that a collection is returned with one element “Results” being an array of collections. Each one of the elements (collections) in the “Results” array is a found database entry (change request). The strategy is therefore to iterate over the collections in “Results” and print some information from each entry. The information retrieved from each found Change Record is the Code, the Name, the Remarks and the revision date.
Example code in Python (download )
# Python version 3.X import requests from requests.auth import HTTPDigestAuth import json
# The URL from Swagger interface, the parameters from experimenting with the Swagger interface url = "https://apps.epsg.org/api/v1/Change/" parameters = "?page=1&pageSize=10"
myResponse = requests.get(url+parameters)
# For successful API call, response code will be 200 (OK) if(myResponse.ok): # Loading the response data into a dictionary variable jData = json.loads(myResponse.content) for changeRequest in jData["Results"] : print("Code : "+str(changeRequest["Code"])) print("Name : "+changeRequest["Name"]) print("Remarks : "+changeRequest["Remark"]) print("Rev Date : "+changeRequest["RevisionDate"]) print("\n") else: # If response code is not ok (200), print the resulting http error code with description myResponse.raise_for_status() |