Home > Software design >  API Call using request module in python
API Call using request module in python

Time:05-31

I am not very familiar with API calls or the requests module. I am trying to get the about information (details) for each DAO. I correctly get the names of the DAOs but I get KeyError when I try to do the details. Any help would be greatly appreciated.

import pandas as pd
import requests

payload = {"requests": [{"indexName": "governance_production", "params": "highlightPreTag=&highlightPostTag=&hitsPerPage=855&attributesToRetrieve=["id"]&maxValuesPerFacet=100&query=&page=0&facets=["types","tags"]&tagFilters="}]}
url = 'https://3b439zgym3-2.algolianet.com/1/indexes/*/queries?x-algolia-agent=Algolia for JavaScript (3.35.1); Browser (lite)&x-algolia-application-id=3B439ZGYM3&x-algolia-api-key=14a0c8d17665d52e61167cc1b2ae9ff1'
headers = {"content-type": "application/x-www-form-urlencoded"}
req = requests.post(url, headers=headers, json=payload).json()

data = []

for item in req['results'][0]['hits']:
    data.append({
        "name": item['_highlightResult']['name']['value'],
        "details": item['_highlightResult']['details']['value'],
    })

print(data)
df = pd.DataFrame(data)
print(df)

CodePudding user response:

Because there is no key named details exists in the resulted JSON, that's why it returns an error.

Here is a sample from the request you made above -

Either it includes tags key along with name and types

  {
    "_highlightResult": {
      "assetSlug": {
        "matchLevel": "none",
        "matchedWords": [],
        "value": "tribe"
      },
      "name": {
        "matchLevel": "none",
        "matchedWords": [],
        "value": "Fei"
      },
      "tags": [
        {
          "matchLevel": "none",
          "matchedWords": [],
          "value": "DeFi"
        }
      ],
      "types": [
        {
          "matchLevel": "none",
          "matchedWords": [],
          "value": "Protocol"
        }
      ]
    },
    "id": "f9779bc3-4eb4-4830-982b-fc981762dbd8",
    "objectID": "f9779bc3-4eb4-4830-982b-fc981762dbd8"
  }

or not including tags key

  {
    "_highlightResult": {
      "assetSlug": {
        "matchLevel": "none",
        "matchedWords": [],
        "value": "aave"
      },
      "name": {
        "matchLevel": "none",
        "matchedWords": [],
        "value": "Aave Grants DAO"
      },
      "types": [
        {
          "matchLevel": "none",
          "matchedWords": [],
          "value": "Grants"
        }
      ]
    },
    "id": "b3a88880-b343-4eba-955e-dd0c4970291a",
    "objectID": "b3a88880-b343-4eba-955e-dd0c4970291a"
  }

Here is the full body of JSON data - JSON data

  • Related