Home > Mobile >  Convert terminal input to python dictionary for use with API
Convert terminal input to python dictionary for use with API

Time:03-29

$ curl https://api.goclimate.com/v1/flight_footprint \
  -u YOUR_API_KEY: \
  -d 'segments[0][origin]=ARN' \
  -d 'segments[0][destination]=BCN' \
  -d 'segments[1][origin]=BCN' \
  -d 'segments[1][destination]=ARN' \
  -d 'cabin_class=economy' \
  -d 'currencies[]=SEK' \
  -d 'currencies[]=USD' \
  -G

I have the following input, provided as an example by the creators of the API. This input is meant to be used in the terminal and give output in form of a dictionary. How would it be possible to write the input above in a list or dictionary to use it as part of an Python script? I tried it like below but the response from the API is solely b' '

payload = {
    "segments" : [
        { 
            "origin" : "ARN",
            "destination" : "BCN"
        },
        { 
            "origin" : "BCN",
            "destination" : "ARN"
        }
    ],
    "cabin_class" : "economy",
    "currencies" : [
        "SEK", "USD"
    ]
}

r = requests.get('https://api.goclimate.com/v1/flight_footprint', auth=('my_API_key', ''), data=payload)
print(r.content)

CodePudding user response:

You are making a GET request with requests, but you are trying to pass data, which would be appropriate for making a POST request. Here you want to use params instead:

response = requests.get(
    "https://api.goclimate.com/v1/flight_footprint",
    auth=("my_API_key", ""),
    params=payload, 
)

print(response.content)

Now, what should payload be? It can be a dictionary, but it can't be nested in the way you had it, since it needs to be encoded into the URL as parameters (N.B. this is what your -G option was doing in the curl request).

Looking at the docs and your curl example, I think it should be:

payload = {
    "segments[0][origin]": "ARN",
    "segments[0][destination]": "BCN",
    "segments[1][origin]": "BCN",
    "segments[1][destination]": "ARN",
    "cabin_class": "economy",
    "currencies[]": "SEK",  # this will actually be overwritten
    "currencies[]": "USD",  # since this key is a duplicate (see below)
}

response = requests.get(
    "https://api.goclimate.com/v1/flight_footprint",
    auth=("my_API_key", ""),
    params=payload, 
)

print(response.content)

Thinking of how we might parse your original dictionary into this structure:

data = {
    "segments" : [
        { 
            "origin" : "ARN",
            "destination" : "BCN"
        },
        { 
            "origin" : "BCN",
            "destination" : "ARN"
        }
    ],
    "cabin_class" : "economy",
    "currencies" : [
        "SEK", "USD"
    ]
}

payload = {}

for index, segment in enumerate(data["segments"]):
    origin = segment["origin"]
    destination = segment["destination"]
    # python 3.6  needed:
    payload[f"segments[{index}][origin]"] = origin
    payload[f"segments[{index}][destination]"] = destination

payload["cabin_class"] = data["cabin_class"]

# requests can handle repeated parameters with the same name this way:
payload["currencies[]"] = data["currencies"]

... should do it.

  • Related