Get json api data from another api flask - python

i created this rest api with flask and sql alchemy and i want to not enter the json data manually but get it from another json and add it to my database :
https://www.habitat.fr/api/qDbBye4V7vtMu8qL97vvHTAnLQuEhC/product/911095/sku
my add product route in flask
#add product
#app.route('/product', methods=['POST'])
def add_product():
name = request.json['name']
description = request.json['description']
price = request.json['price']
qty = request.json['qty']
new_product = Product(name,description,price,qty)
db.session.add(new_product)
db.session.commit()
return product_schema.jsonify(new_product)

All you need do is write a function using requests and json modules to fetch the json in the url like this:
import requests
import json
def get_data():
r = requests.get('https://www.habitat.fr/api/qDbBye4V7vtMu8qL97vvHTAnLQuEhC/product/911095/sku')
return json.loads(r.content) #convert content to dict
You may now call the function in your flask app:
#app.route('/product', methods=['POST'])
def add_product():
my_data = get_data()
name = my_data['name']
description = my_data['description']
price = my_data['price']
qty = my_data['qty']

Related

Python API script

I am making a python script using API of a free test automation website called TestProject.
Link to their API: https://api.testproject.io/docs/v2/
Basically what i want to do is grab pdf of reports of all tests and save them somewhere.
But to make the GET request to do that i first need projectID and jobID which i already wrote functions getting them and saving them in the array.
But now i have a problem where its looping through both lists and not using correct projectID and jobID and its throwing errors because it does not exist.
So what i need is something to check if jobID is in projectID so that way i can make a GET request to get all the executionID's to get the PDF of the report.
I am kinda new to programming so i would love any help i can get. If anyone has any better solutions please feel free to let me know.
My script:
import requests
import json
import csv
from datetime import datetime
from jsonpath_ng import jsonpath, parse
API_key = 'api_key'
headers = {'Authorization':'{}'.format(API_key)}
list_projectId = []
list_jobId = []
list_executionId = []
ParseData_projectId = parse('$..id')
ParseData_jobId = parse('$..id')
ParseData_executionId = parse('$..id')
def parsing (response,ParseData,list_data):
# parses data and appends it to the list
Data = json.loads(response)
Parsaj = ParseData
Podatki = Parsaj.find(Data)
for i in range(0, len(Podatki)):
vrednost = Podatki[i].value
list_data.append(vrednost)
def projectId():
# gets all projectId's and saves them in list_projectId
url = 'https://api.testproject.io/v2/projects?_start=0'
response = requests.get(url,headers=headers)
response_json = response.json()
converted = json.dumps(response_json)
parsing(converted,ParseData_projectId,list_projectId)
def jobId():
# gets all jobId's and saves them in list_jobId
for i in range(0, len(list_projectId)):
id = list_projectId[i]
url = 'https://api.testproject.io/v2/projects/{}'.format(id) + '/jobs?onlyScheduled=false&_start=0'
response = requests.get(url,headers=headers)
response_json = response.json()
converted = json.dumps(response_json)
parsing(converted,ParseData_jobId,list_jobId)
def executionId():
# Their API link:
# https://api.testproject.io/v2/projects/{projectId}/jobs/{jobId}/reports?_start=0
# the for loop below does not work here is where i need the help:
for i in range(0, len(list_projectId)):
project_id = list_projectId[i]
job_id = list_jobId[i]
url = 'https://api.testproject.io/v2/projects/{}'.format(project_id) + '/jobs/{}'.format(job_id) + '/reports?_start=0'
response = requests.get(url,headers=headers)
response_json = response.json()
converted = json.dumps(response_json)
parsing(converted,ParseData_executionId,list_executionId)
projectId()
print("----------LIST PROJECT ID: ----------")
print(list_projectId)
print("")
jobId()
print("----------LIST JOB ID: ----------")
print(list_jobId)
executionId()
print("----------LIST EXECUTION ID: ----------")
print(list_executionId)
you have to use 'in' operator to check the value exist in the list data structure.

Problem with POST Method on my Flask APP on Python

i have a problem with my flask app thaht i'm trying to create,
i wrote a POST Method to take arguments from HTTP and write them to a table.
here's my code:
from flask import Flask
from flask_restful import Resource, Api, reqparse
import pandas as pd
import ast
import sqlalchemy
# SQLAlchemy connectable
engine = sqlalchemy.create_engine("mssql+pymssql://my_connection_string")
conn = engine.connect()
app = Flask(__name__)
api = Api(app)
class Samples(Resource):
def get(self):
data = pd.read_sql_table('DBLIST_DATA', engine)
data = data.to_dict() # convert dataframe to dict
return {'data': data}, 200 # return data and 200 OK
def post(self):
parser = reqparse.RequestParser() # initialize
parser.add_argument('NODE_NO_data', required=True) # add args
parser.add_argument('BLOCK_NAME_data', required=True)
parser.add_argument('BLOCK_TYPE_data', required=True)
args = parser.parse_args() # parse arguments to dictionary
# read our SQL Table
data = pd.read_sql_table('DBLIST_DATA', engine)
#Check if Value Exists
if args['NODE_NO_data'] in list(data['NODE_NO_data']):
return {
'message': f"'{args['NODE_NO_data']}' already exists."
}, 409
else:
# create new dataframe containing new values
new_data = pd.DataFrame({
'NODE_NO_data': [args['NODE_NO_data']],
'BLOCK_NAME_data': [args['BLOCK_NAME_data']],
'BLOCK_TYPE_data': [args['BLOCK_TYPE_data']],
'locations': [[]]
})
# add the newly provided values
data = data.append(new_data, ignore_index=True)
data.to_sql('DBLIST_DATA', index=False) # save back to SQL Table
return {'data': data.to_dict()}, 200 # return data with 200 OK
api.add_resource(Samples, '/samples') # add endpoints
if __name__ == '__main__':
app.run() # run app
when i try to POST via Postman the following values:
127.0.0.1:5000/samples/?NODE_NO_data=1&BLOCK_NAME_data=Machine&BLOCK_TYPE_data=Vertical
i'm getting a 404 not found error.
can someone please tell me what i'm doing wrong?
Thanks!
You could just remove the last / of your request URL http://127.0.0.1:5000/samples.

How to append/remove from a list in Python over a Flask API? [duplicate]

This question already has answers here:
Are global variables thread-safe in Flask? How do I share data between requests?
(4 answers)
Closed 2 years ago.
I am trying to learn how to add/delete stocks in my Python API from a Flutter app I am making. I finally figured out how to retrieve the data, but I do not know how to add/delete from my stock list.
Here is the Yahoo Finance scraper and the stock list:
import pandas as pd
import yfinance as yf
import json
from flask import Flask, json
stock_list = stock_list = ['AAPL', 'AMD', 'TSLA']
tnx = yf.Ticker('^TNX')
tenYr = round(tnx.info['previousClose'], 2)
# print('10yr Note:', str(tenYr) + '%')
myJson = {}
for stock in stock_list:
info = yf.Ticker(stock).info
symbol = stock
price = info.get('previousClose')
tEps = info.get('trailingEps')
fEps = info.get('forwardEps')
tRatio = (tEps / (tenYr * 0.01)) / (price * 4)
fRatio = (fEps / (tenYr * 0.01)) / (price * 4)
myJson[symbol] = {
"price": price,
"tEps": tEps,
"fEps": fEps,
"tRatio": tRatio,
"fRatio": fRatio
}
# print(json.dumps(myJson))
Here is the Flask API:
from flask import Flask, request, jsonify
import stock_list as sl
app = Flask(__name__)
#app.route('/stocks', methods=['GET'])
def get_stocks():
# d = {}
# d['Query'] = str(request.args['Query'])
return jsonify(sl.myJson)
#app.route('/stocks/add', methods=['POST'])
def add_stocks():
return null
#app.route('/stocks/remove', methods=['POST'])
def remove_stocks():
return null
if __name__ == '__main__':
app.run()
How can I pass a new stock, say GOOGL, into the stock list, and also remove it? Thank you for the help!
If you want to add data through url, then you have to pass that data in url like :
#app.route('/stocks/add/<string:stock>', methods=['POST'])
def add_stocks(stock):
sl.stock_list.append(stock)
#app.route('/stocks/remove/<string:stock>', methods=['POST'])
def remove_stocks(stock):
sl.stock_list.pop(stock)
If you want to add data through any form then use form.validate_on_submit() method like shown here
or something similar

How to describe and visualize continued changed data from mongoDB using pandas

I'm setting up an API using Flask, and add some JSON post using postman to be transferred to mongoDB database.
Then I had to visualize statistics of the real time data from the database into lets say data_analysis_script.py, which means if I post some JSON from postman, the statistics should be changed since the data had been added.
Any suggestion about function or library I could use further in script for showing the data statistics ?
I had tried using manager which could run both app.run() and the code, but not print the code
API code
from flask import Flask, jsonify, request
from flask_pymongo import PyMongo
import pandas as pd
app = Flask(__name__)
app.config['MONGO_DBNAME'] = 'db'
app.config['MONGO_URI'] = 'mongodb://localhost:8000/db'
mongo = PyMongo(app)
#app.route('/stocks', methods=['GET'])
def get_all_stocks():
stocks = mongo.db.stocks
output = []
for i in stocks.find():
output.append({'name' : i['name'], 'item' : i['item']})
return jsonify({'Here yours' : output})
#app.route('/add', methods=['POST'])
def add_stocks():
stocks = mongo.db.stocks
name = request.json['name']
item = request.json['item']
item_id = stocks.insert({'name': name, 'item': item})
new_stocks = stocks.find_one({'_id': stocks_id })
output = {'name' : new_stocks['name'], 'item' : new_stocks['item']}
return jsonify({'Here yours' : output})
#app.route('/stocks/', methods=['GET'])
def get_one_stocks(name):
stocks = mongo.db.stocks
c = stocks.find_one({'name' : name})
if s:
output = {'name' : c['name'], 'item' : c['item']}
else:
output = "Nothing"
return jsonify({'Here yours' : output})
if __name__ == '__main__':
app.run(debug=True)
I expect the data visualization and statistics changed when there is a JSON entry

Python flask_restful arguments

I'm creating the server end of a test API using flask_restful that will capture a request coming into the API with a ID, and 3 parameters. If the Id does not exists, then it is created and populated with the 3 data arguments. The Asset is created, however only the 3rd parameter is added to the dict.
Code I have:
from flask import Flask, request
from flask_restful import reqparse, abort, Api, Resource
app = Flask(__name__)
api = Api(app)
assets = {}
def abort_if_asset_doesnt_exist(asset_id):
if asset_id not in assets:
abort(404, message="Asset {} doesn't exist".format(asset_id))
parser = reqparse.RequestParser()
parser.add_argument('data1', type=str)
parser.add_argument('data2', type=str)
parser.add_argument('data3', type=str)
class Asset(Resource):
def get(self, asset_id):
return {asset_id: assets[asset_id]}
def put(self, asset_id):
assets[asset_id] = request.form['data1']
assets[asset_id] = request.form['data2']
assets[asset_id] = request.form['data3']
return {asset_id: assets[asset_id]}
def delete(self, asset_id):
abort_if_todo_doesnt_exist(asset_id)
del assets[asset_id]
return '', 204
api.add_resource(Asset, '/api-v1.0/add/<string:asset_id>', methods=['PUT', 'GET'])
if __name__ == '__main__':
app.run(debug=True)
I want to take the id and create a 'instance' in the dictionary, with it's corresponding data fields attached to it. like below
What I want:
{
"123456" {
"data1":"Mydata1"
"data2":"Mydata2"
"data3":"Mydata3"
}
}
With this I can call the asset and receive it's associated data fields.
The curl (PUT) command I use looks like this.
$ curl http://localhost:5000/api-v1.0/add/123456 -d "data1=Mydata1" -d "data2=Mydata2" -d "data3=Mydata3" -X PUT
This is because you're just assigning a value one after the other to the location specified by asset_id in your assets dict. Since the last value assigned to assets is the 3rd parameter this is the value that is contained at that key.
To fix this you need to change that code to create a dict for the asset_id that you want to store the form data against:
assets[asset_id] = dict()
assets[asset_id]['data1'] = request.form['data1']
assets[asset_id]['data2'] = request.form['data2']
assets[asset_id]['data3'] = request.form['data3']
Or even better use a defaultdict:
assets = defaultdict(dict)
...
assets[asset_id]['data1'] = request.form['data1']

Categories