ThunderSpark
ThunderSpark

Reputation: 109

API Request within another API request (Same API) in Python

I currently have made a python program, request JSON data from an API. Now here is the thing though this JSON actually contains other request Urls to get extra data from that object.

import requests

import json

import sys

import os

import geojson



response = requests.get("http://api.gipod.vlaanderen.be/ws/v1/workassignment", params = {"CRS": "Lambert72"})

print(response.status_code)
text = json.dumps(response.json(),sort_keys=True, indent=4)
print(text)

f = open("text.json", "wt")
f.write(text)

print(os.getcwd())

JSON from request, the other request URLs including parameters is in the detail column.

[
{
"gipodId": 103246,
"owner": "Eandis Leuven",
"description": ", , ZAVELSTRAAT: E Nieuw distributienet (1214m)",
"startDateTime": "2007-12-03T06:00:00",
"endDateTime": "2014-01-06T19:00:00",
"importantHindrance": false,
"coordinate": {
"coordinates": [
4.697028256276443,
50.896894135898485
],
"type": "Point",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
}
},
**"detail": http://api.gipod.vlaanderen.be/ws/v1/workassignment/103246?crs=4326,
"cities": ["Leuven"]**
}
],
"latestUpdate": "2016-11-16T11:32:39.253"
}

The first request just gets the points (each unique with a certain id), while the second request gets the "details data" which also has polygon data and multiline.

Get Url: http://api.gipod.vlaanderen.be/ws/v1/workassignment/[id]

{ "comment" : null,
  "contactDetails" : { "city" : "Leuven",
      "country" : "België",
      "email" : null,
      "extraAddressInfo" : null,
      "firstName" : null,
      "lastName" : null,
      "number" : "58",
      "organisation" : "Eandis Leuven",
      "phoneNumber1" : "078/35.35.34",
      "phoneNumber2" : null,
      "postalCode" : "3012",
      "street" : "Aarschotsesteenweg"
    },
  "contractor" : null,
  "mainContractor" : null,
  "description" : ", , ZAVELSTRAAT: E Nieuw distributienet (1214m)",
  "diversions" : [
    {
    "gipodId": 1348152,
    "reference": "IOW-TERRAS-2013-01-Z",
    "description": "Horecaterras op parkeerstrook (Lierbaan 12)",
    "comment": null,
    "geometry": {
    "geometries": [
    {
        "coordinates": [[[3.212947654779088, 51.175784679668915],
        [3.2151308569159482, 51.17366647833133],
        [3.216112818368467,  51.17328051591839],
        [3.2186926906668876, 51.173044950954456],
        [3.2204789191276944, 51.173098278776514],
        [3.221602856602255,  51.173333934695286]]],
        "type": "MultiLineString",
        "crs": null
        }
        ],
        "type": "GeometryCollection",
        "crs": {
        "type": "name",
        "properties": {
        "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
        }
    },
    "periods": [{"startDateTime": "2013-04-09T00:00:00","endDateTime": "2013-10-31T00:00:00"}],
    "recurrencePattern": null,
    "latestUpdate": "2014-01-24T10:23:08.917",
    "streets": null,
    "diversionTypes": null,
    "diversionDirection": 
        {
            "type": 0,
            "description": "Beide"
        },
    "status": "Vergund",
    "contactDetails": {
        "organisation": "Café Real",
        "lastName": "Vets",
        "firstName": "Peggy",
        "phoneNumber1": null,
        "phoneNumber2": null,
        "email": "[email protected]",
        "street": "Lierbaan",
        "number": "12",
        "postalCode": "2580",
        "city": "Putte",
        "country": "België",
        "extraAddressInfo": null
        }
   "url": null,
    }
    ],
  "endDateTime" : "2014-01-06T19:00:00",
  "gipodId" : 103246,
  "hindrance" : { "description" : null,
      "direction" : null,
      "effects" : [ "Fietsers hebben doorgang",
          "Handelaars bereikbaar",
          "Verminderde doorstroming in 1 richting",
          "Voetgangers op de rijweg",
          "Voetgangers hebben doorgang"
        ],
      "important" : false,
      "locations" : [ "Voetpad" ]
    },
  "latestUpdate" : "2013-06-18T03:43:28.17",
  "location" : { "cities" : [ "Leuven" ],
      "coordinate" : { "coordinates" : [ 4.697028256276443,
              50.896894135898485
            ],
          "crs" : { "properties" : { "name" : "urn:ogc:def:crs:OGC:1.3:CRS84" },
              "type" : "name"
            },
          "type" : "Point"
        },
      "geometry" : { "coordinates" : [ [ [ [ 4.699934331336474,
                    50.90431808607037
                  ],
                  [ 4.699948535632464,
                    50.90431829749237
                  ],
                  [ 4.699938837004092,
                    50.90458139231922
                  ],
                  [ 4.6999246328435396,
                    50.90458118062111
                  ],
                  [ 4.699934331336474,
                    50.90431808607037
                  ]
                ] ]
            ],
          "crs" : { "properties" : { "name" : "urn:ogc:def:crs:OGC:1.3:CRS84" },
              "type" : "name"
            },
          "type" : "MultiPolygon"
        }
    },
  "owner" : "Eandis Leuven",
  "reference" : "171577",
  "startDateTime" : "2007-12-03T06:00:00",
  "state" : "In uitvoering",
  "type" : "Werken aan nutsleiding",
  "url" : "http://www.eandis.be"
}

Now here is the deal, this request has to be repeated for each object I get from the First API Request. And this can be over one hundred objects. So logic dictates this has to happen in a loop, though how to start is bit..troublesome.

Upvotes: 0

Views: 1086

Answers (1)

VJ Magar
VJ Magar

Reputation: 1052

You can make you of functions in this case.

Your first function can simply fetch the list of the points. Your second function can simply fetch the data of details.


def fetch_details(url: str):
      """ Makes request call to get the data of detail """
        response = requests.get(url)
        # any other processe


def fetch_points(url: str):
       
       response = requests.get(url)
       for obj in response.json():
              fetch_details(obj.get("detail"))

api_url = "api.gipod.vlaanderen.be/ws/v1/workassignment"
fetch_points(api_url)

Upvotes: 1

Related Questions