How can I convert an API endpoint into dataframe - python

I have found this following API endpoint from binance which unfortunately doesn't have a documentation on their page. They are using it directly on their website UI.
I have found the POST Request to the https://www.binance.com/gateway-api/v1/public/future/leverage/token/basket/list actually works. The problem is I would like to convert this info into a dataframe.
I have tried the following, unfortunately, I cannot find a way to convert the following into dataframe as the type is not yet json.
Any help would be appreciated. Thanks in advance
My attempt:
import requests
from requests.structures import CaseInsensitiveDict
url = "https://www.binance.com/gateway-api/v1/public/future/leverage/token/basket/list"
headers = CaseInsensitiveDict()
headers["Content-Type"] = "application/json"
data = """
{"asset": "BTCUP", "current": 1, "size": 20}
"""
resp = requests.post(url, headers=headers, data=data)
print(resp.content) #shows the data as class 'byte'

Related

Is there a way to get access to QuickBase tables using python API?

I'm trying to read the table contents in Quick Base. As of now I'm downloading the files from Quickbase in CSV format and reading them from the python pandas package. Is there a way to get access to QuickBase tables using python API? So that we can avoid the process of downloading the files and directly reading the table contents directly from QuickBase
Yes, can you get table data via the Quickbase REST API: https://developer.quickbase.com
There isn't a directly supported SDK, but it's fairly simple to interact with this API using the Python json and request libraries to make a POST call.
import json
import requests
headers = {
'QB-Realm-Hostname': '{QB-Realm-Hostname}',
'User-Agent': '{User-Agent}',
'Authorization': '{Authorization}'
}
body = {}
r = requests.post(
'https://api.quickbase.com/v1/records/query',
headers = headers,
json = body
)
print(json.dumps(r.json(),indent=4))
Note: Please only comment if you have helpful things to say. I am fully aware that this should be a comment but I don't have enough reputation and I need help.
To Erich Wehrmann's answer:
Hello, thank you for your help.
I have gotten here but I don't know how to actually get the data out of my table instead of just the field names in json format.
Do you know how I can actually get the data from my table?
import json
import requests
headers = {
'QB-Realm-Hostname': 'my-realm.quickbase.com',
'User-Agent': '{User-Agent}',
'Authorization': 'QB-USER-TOKEN my-token',
'Content-Type': 'application/json',
}
body = {
'from': 'my-table-id', 'select': '[1,2]'
}
r = requests.get(
'https://api.quickbase.com/v1/fields?tableId=my-table-Id',
headers = headers,
json = body
)
print(json.dumps(r.json(),indent=4))

Error while posting data in CustomVision.ai API

I have trained the model on customvision.ai and obtained the prediction key and endpoint.
Here what I have written
import requests
import json
url="https://centralindia.api.cognitive.microsoft.com/customvision/v3.0/Prediction/xxxxxxxxxx/classify/iterations/Iteration1/url"
headers={'content-type':'application/json','Prediction-Key':'24xxxxxxxxxxxxxxxxxxxxxx'}
body={"Url": "https://i.imgur.com/cYzaOkV.jpg"}
r =requests.post(url,data=body,headers=headers)
print(r.content)
But when I tried to run this
I get this error:
b'{"code":"BadRequestImageUrl","message":"Invalid image url"}'
However,
I tried to call my model using a local image and in this case, it works fine
here code for written for local img:
import requests
import json
url="https://centralindia.api.cognitive.microsoft.com/customvision/v3.0/Prediction/82xxxxxxxxxxxxxxxxxxxx/classify/iterations/Iteration1/image"
headers={'content-type':'application/octet-stream','Prediction-Key':'24xxxxxxxxxxxxxxxxxx'}
r =requests.post(url,data=open("lentigo-adults-1.jpg","rb"),headers=headers)
print(r.content)
I get my desired output:
b'{"id":"43a1d33b-0ea9-490a-9ae4-d24f8395931a","project":"82462bcc-5616-4f82-98ea-8d4fda55a9e6","iteration":"d0e48a13-e906-4af2-9f4c-15d79e3b4576","created":"2020-10-14T13:48:41.671Z","predictions":[{"probability":0.999876738,"tagId":"b5bd1d3a-dbcd-4b0d-ad0b-3f8d7b12fbea","tagName":"Lentigo"},{"probability":0.0001100349,"tagId":"c69433ef-4630-4e0f-8077-2de485483323","tagName":"Acne Cystic"},{"probability":1.29300688E-05,"tagId":"2102157b-b250-4c3d-bf2a-588cda8e93eb","tagName":"Rosacea"},{"probability":1.10303489E-07,"tagId":"ea68f8e7-b50f-4ca0-88dc-c2c8f3bb6823","tagName":"Herpes"},{"probability":9.26677259E-08,"tagId":"29c5c735-90f7-48ff-b23c-545be3325e9e","tagName":"Acne Pustular"},{"probability":7.811148E-08,"tagId":"579b080c-4ab1-405f-a8bd-5bf32efd9b70","tagName":"Melasma"},{"probability":3.90258634E-08,"tagId":"8340afdd-b758-46b4-9aee-754480437f0e","tagName":"Acne Comedo"},{"probability":1.49268364E-09,"tagId":"b89f964a-1a72-41ec-b8b7-8e0d7e9ef8ff","tagName":"Eczema"}]}'
can anyone please help me??
I want to call this endpoint using img url
Thank you for giving me your important time towards my problem.
According to my test, when we call the image URL endpoint, the body should be json string. Please update code r =requests.post(url,data=body,headers=headers) to response = requests.post( url, headers=headers, data=json.dumps(body))
for example
import requests
import json
url = "https://testvision03.cognitiveservices.azure.com/customvision/v3.0/Prediction/<>/detect/iterations/Iteration1/url"
body = {"Url": "https://i.imgur.com/cYzaOkV.jpg"}
headers = {
'Prediction-Key': '',
'Content-Type': 'application/json'
}
response = requests.post(url, headers=headers, data=json.dumps(body))
print(response.text.encode('utf8'))

How do you update DynamoDB from Jupyter Notebook via API Gateway?

I'm building a personal webscraper and I'm still in dev phase, but I want to start saving data. The problem is, I cannot PUT or POST from the notebook level, which also means I cannot iterate through a big list of dictionaries/json objects.
I can however to it manually via Postman by just pasting the body and sending it.
Below is my code, which currently returns:
The pastebin URL is:{"message": "Could not parse request body into json: Unrecognized token 'ID': was expecting 'null', 'true', 'false' or NaN\n at [Source: (byte[])"ID=0&District=London"; line: 1, column: 4]"}
import requests
# defining the api-endpoint
API_ENDPOINT = ##############
# data to be sent to api
body = {
"ID": 0,
"District": "London"
}
# sending post request and saving response as response object
r = requests.post(url = API_ENDPOINT, data = body)
# extracting response text
pastebin_url = r.text
print("The pastebin URL is:%s"%pastebin_url)
Related question - can I use urllib instead of requests here?
You can try the following:
r = requests.post(url = API_ENDPOINT, json = body)
or
import json
r = requests.post(url = API_ENDPOINT, headers={"Content-Type":"application/json"}, data = json.dumps(body))

Python requests response - cant unnest the data object

I am querying an API and receiving back a response of type text
url = "https://tripadvisor1.p.rapidapi.com/reviews/list"
querystring = {"limit":"20","currency":"USD","lang":"en_US","location_id":"2269364"}
headers = {
'x-rapidapi-host': "xxx",
'x-rapidapi-key': "xxx"
}
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)
I then want to retrieve a dataframe object. Various online sources suggest this approach:
d = json.loads(response.text)
e = json_normalize(d)
However, what I get back is this:
I would like to obtain a dataframe object from the 'data' column. Please advise if I can make this question clearer.
First off requests already has a json() method on the response object to decode JSON responses. Second, the top level object contains three things: data, paging_results, and paging_total_results which is why you see what you do.
If you want to extract the inner data contents, just pull that out before passing it to normalize:
response = ...
df = json_normalize(response.json()['data'])

Sending a JSON string as a post request

rocksteady's solution worked
He did originally refer to dictionaries. But the following code to send the JSON string also worked wonders using requests:
import requests
headers = {
'Authorization': app_token
}
url = api_url + "/b2api/v1/b2_get_upload_url"
content = json.dumps({'bucketId': bucket_id})
r = requests.post(url, data = content, headers = headers)
I'm working with an API that requires me to send JSON as a POST request to get results. Problem is that Python 3 won't allow me to do this.
The following Python 2 code works fine, in fact it's the official sample:
request = urllib2.Request(
api_url +'/b2api/v1/b2_get_upload_url',
json.dumps({ 'bucketId' : bucket_id }),
headers = { 'Authorization': account_authorization_token }
)
response = urllib2.urlopen(request)
However, using this code in Python 3 only makes it complain about data being invalid:
import json
from urllib.request import Request, urlopen
from urllib.parse import urlencode
# -! Irrelevant code has been cut out !-
headers = {
'Authorization': app_token
}
url = api_url + "/b2api/v1/b2_get_upload_url"
# Tested both with encode and without
content = json.dumps({'bucketId': bucket_id}).encode('utf-8')
request = Request(
url=url,
data=content,
headers=headers
)
response = urlopen(req)
I've tried doing urlencode(), like you're supposed to. But this returns a 400 status code from the web server, because it's expecting pure JSON. Even if the pure JSON data is invalid, I need to somehow force Python into sending it.
EDIT: As requested, here are the errors I get. Since this is a flask application, here's a screenshot of the debugger:
Screenshot
Adding .encode('utf-8') gives me an "Expected string or buffer" error
EDIT 2: Screenshot of the debugger with .encode('utf-8') added
Since I have a similar application running, but the client still was missing, I tried it myself.
The server which is running is from the following exercise:
Miguel Grinberg - designing a restful API using Flask
That's why it uses authentication.
But the interesting part: Using requests you can leave the dictionary as it is.
Look at this:
username = 'miguel'
password = 'python'
import requests
content = {"title":"Read a book"}
request = requests.get("http://127.0.0.1:5000/api/v1.0/projects", auth=(username, password), params=content)
print request.text
It seems to work :)
Update 1:
POST requests are done using requests.post(...)
This here describes it well : python requests
Update 2:
In order to complete the answer:
requests.post("http://127.0.0.1:5000/api/v1.0/projects", json=content)
sends the json-string.
json is a valid parameter of the request and internally uses json.dumps()...

Categories