I am trying to implement this API from blockonomics. It is called a HTTP callback, where it seends data to my HTTP or a link on my website I give it. For example, I give it website.com/api it will send website.com/api?info=value&info2=value2 and u get the point. So I want to be able to take this data and implement it into my flask program. The sample code they give is in PHP and I get the gist of it but I do not know how to make this python.
This is the callback it would send
/api/callback_url?status=2&addr=1C3FrYaGgUJ8R21jJcwzryQQUFCWFpwcrL&value=10000&txid=4cb3 0849ffcaf61c0e97e8351cca2a32722ceb6ad5f34e630b4acb7c6dc1e73b
This is the PHP code it gives me.
<?php
$secret = 'Mabcdas122olkdd';
$txid = $_GET['txid'];
$value = $_GET['value'];
$status = $_GET['status'];
$addr = $_GET['addr'];
//Match secret for security
if ($_GET['secret'] != $secret) {
return;
}
if ($status != 2) {
//Only accept confirmed transactions
return;
}
$db = new SQLite3('payments_db.sqlite', SQLITE3_OPEN_READWRITE);
//Mark address in database as paid
$stmt = $db->prepare("UPDATE payments set addr=:addr,txid=:txid,".
"value=:value where addr=:addr");
$stmt->bindParam(":addr", $addr);
$stmt->bindParam(":txid", $txid);
$stmt->bindParam(":value", $value);
$stmt->execute();
?>
I tried doing the line
requests.get("website.com/api", params=['addr':'value', 'value':'value', ETC...)
and then recording this as a json but apparently this is not the correct way because it does not work. Can someone basically just explain how to take the data from the URL that the API is sending me and convert it to data that I can work with? I just need to atleast record what is being sent and I understand how to do the rest.
CodePudding user response:
You can do this
from flask import request
@app.route("api/callbackurl")
def getData():
# get query string
status = request.args.get('status')
addr = request.args.get('addr')
CodePudding user response:
You need a route that supports the GET method.
@app.route('/api/callback_url')
def callback_url():
# your code here!
You can query the url parameters based on the name via the request object.
from flask import request
secret = request.args.get('secret')
If the value does not exist, either None or the specified default value is returned.
If a type argument is passed, the value is automatically converted.
status = request.args.get('status', 0, type=int)