ce détachement au cas où quelqu'un a besoin d'un exemple complet de la façon de faire un appel de base à l'API GoodRx:
En Python:
import requests
import hashlib
import hmac
import base64
# python --version returns:
# Python 3.5.1 :: Anaconda 2.4.1 (32-bit)
# my_api_key is the API key for your account, provided by GoodRx
my_api_key = "YOUR_API_KEY";
# s_skey is the secret key for your account, provided by GoodRx
my_secret_key=str.encode("YOUR_SECRET_KEY", 'utf-8')
# Create the base URL and the URL parameters
# The url_params start as text and then have to be encoded into bytes
url_base = "https://api.goodrx.com/fair-price?"
url_params = str.encode("name=lipitor&api_key=" + my_api_key, 'utf-8')
# This does the hash of url_params with my_secret_key
tmpsig = hmac.new(my_secret_key, msg=url_params, digestmod=hashlib.sha256).digest()
# Base64 encoding gets rid of characters that can't go in to URLs.
# GoodRx specifically asks for these to be replaced with "_"
sig = base64.b64encode(tmpsig, str.encode("__", 'utf-8'))
# Convert the sig back to ascii
z = sig.decode('ascii')
# add the sig to the URL base
url_base += url_params.decode('ascii') + "&sig=" + z
# request the URL base
r = requests.get(url_base)
# print the response
print(r.content)
Vous devriez obtenir quelque chose comme ça pour la sortie:
{ "erreurs": [], "data": { "mobile_url": "http://m.goodrx.com/?grx_ref=api#/drug/atorvastatin/tablet", "forme": "tablette", "url": "http://www.goodrx.com/atorvastatin?grx_ref=api", "marque": ["lipitor"], "dosage": "20 mg", "prix": 12,0, "génériques": ["atorvastatine"], "quantité": 30, "affichage": "Lipitor (atorvastatine) », "fabricant": "générique"}, "succès": true}
Voici un code similaire en PHP, en regardant le médicament Apidra SoloStar qui a NDC 00088250205:
<?php
function base64url_encode($data) {
return strtr(base64_encode($data), '+/', '__');
}
$my_api_key = "YOUR_API_KEY";
$s_key="YOUR_SECRET_KEY";
$ndc = "00088250205";
// Initialize the CURL package. This is the thing that sends HTTP requests
$ch = curl_init();
// Create the URL and the hash
$url = "https://api.goodrx.com/fair-price?";
$query_string="ndc=" . $ndc . "&api_key=" . $my_api_key;
$tmp_sig = hash_hmac('sha256', $query_string, $s_key, true);
$sig = base64url_encode($tmp_sig);
$url = $url . $query_string . "&sig=" . $sig;
// set some curl options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, true);
// run the query
$response = curl_exec($ch);
var_dump($response);
?>
Merci à Thomas d'avoir parlé avec moi à ce sujet.
J'ai fait la correction comme vous l'avez suggéré, mais j'ai quand même eu une erreur. –