1

I am new to python and want to fetch data from an API via Google Cloud Functions so that I can store the data in Google Cloud Storage afterwards.

The data is available in JSON format and I want to transform it in a table via pandas.

Since I am really unsure about the correct syntax I'd like to know how I have to call the function test_topic - The following code doesn't work for me. I get no error message but also I get no result.

What do I have to do that I get the table as a result?

import requests
import pandas as pd

def test_topic(df):
    url = "https://api.domain.com/v1/"
    payload={}
    headers = {}
    parameters = {
        "api_key": "1234567890",
        "start_date": "2020-01",
        "end_date": "2021-01",
        "format": "json" 
    }

    response = requests.request("GET", url, headers=headers, data=payload, params=parameters)

    df = response.json()['visits']  
    pd.DataFrame(df)
4
  • You forgot to include the error messages. Commented Jan 6, 2022 at 20:08
  • You're right, sorry. I don't get no error message, it's just that i don't get no result. Commented Jan 6, 2022 at 21:01
  • What's the response status in your code? Commented Jan 7, 2022 at 7:47
  • repsonse status is 200 Commented Jan 7, 2022 at 20:24

1 Answer 1

1

Your first issue is that you probably need to authenticate your request against the Cloud Function. Unless the Cloud Function was deployed --allow-unauthenticated (permitting anyone), you're going to have to authenticate requests using an access (?) token (TOKEN):

token = os.getenv("TOKEN")
headers=["Authorization"] = "Bearer: {token}".format(token=token)}

For development purposes, you can grab a token using gcloud and export this to your code:

export TOKEN=$(gcloud auth print-access-token)
python3 main.py

You should also:

headers["Accept"] = "application/json"

For debugging, you should consider:

json_response = response.json()
print(json_response)

before you try to access visits, perhaps:

if "visits" in json_response:
  # JSON response contains "visits"
else:
  # JSON response does not contain "visits"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.