api: use API_HOST env variable everywhere (#21814)
* api base url to global constant * update api/__init__.pypull/21838/head
parent
c900bce1b0
commit
543e019f71
|
@ -1,9 +1,12 @@
|
|||
import jwt
|
||||
import os
|
||||
import requests
|
||||
from datetime import datetime, timedelta
|
||||
from common.basedir import PERSIST
|
||||
from selfdrive.version import version
|
||||
|
||||
API_HOST = os.getenv('API_HOST', 'https://api.commadotai.com')
|
||||
|
||||
class Api():
|
||||
def __init__(self, dongle_id):
|
||||
self.dongle_id = dongle_id
|
||||
|
@ -34,12 +37,10 @@ class Api():
|
|||
|
||||
|
||||
def api_get(endpoint, method='GET', timeout=None, access_token=None, **params):
|
||||
backend = "https://api.commadotai.com/"
|
||||
|
||||
headers = {}
|
||||
if access_token is not None:
|
||||
headers['Authorization'] = "JWT "+access_token
|
||||
|
||||
headers['User-Agent'] = "openpilot-" + version
|
||||
|
||||
return requests.request(method, backend+endpoint, timeout=timeout, headers=headers, params=params)
|
||||
return requests.request(method, API_HOST+endpoint, timeout=timeout, headers=headers, params=params)
|
||||
|
|
|
@ -72,7 +72,7 @@ def register(show_spinner=False) -> str:
|
|||
try:
|
||||
register_token = jwt.encode({'register': True, 'exp': datetime.utcnow() + timedelta(hours=1)}, private_key, algorithm='RS256')
|
||||
cloudlog.info("getting pilotauth")
|
||||
resp = api_get("v2/pilotauth/", method='POST', timeout=15,
|
||||
resp = api_get("/v2/pilotauth/", method='POST', timeout=15,
|
||||
imei=imei1, imei2=imei2, serial=serial, public_key=public_key, register_token=register_token)
|
||||
|
||||
if resp.status_code in (402, 403):
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
namespace CommaApi {
|
||||
|
||||
const QString BASE_URL = getenv("API_HOST") != nullptr ? getenv("API_HOST") : "https://api.commadotai.com";
|
||||
QByteArray rsa_sign(const QByteArray &data);
|
||||
QString create_jwt(const QJsonObject &payloads = {}, int expiry = 3600);
|
||||
|
||||
|
|
|
@ -96,7 +96,7 @@ MapPanel::MapPanel(QWidget* parent) : QWidget(parent) {
|
|||
if (auto dongle_id = getDongleId()) {
|
||||
// Fetch favorite and recent locations
|
||||
{
|
||||
QString url = "https://api.commadotai.com/v1/navigation/" + *dongle_id + "/locations";
|
||||
QString url = CommaApi::BASE_URL + "/v1/navigation/" + *dongle_id + "/locations";
|
||||
RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_NavDestinations", 30, true);
|
||||
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &MapPanel::parseResponse);
|
||||
QObject::connect(repeater, &RequestRepeater::failedResponse, this, &MapPanel::failedResponse);
|
||||
|
@ -104,7 +104,7 @@ MapPanel::MapPanel(QWidget* parent) : QWidget(parent) {
|
|||
|
||||
// Destination set while offline
|
||||
{
|
||||
QString url = "https://api.commadotai.com/v1/navigation/" + *dongle_id + "/next";
|
||||
QString url = CommaApi::BASE_URL + "/v1/navigation/" + *dongle_id + "/next";
|
||||
RequestRepeater* repeater = new RequestRepeater(this, url, "", 10, true);
|
||||
HttpRequest* deleter = new HttpRequest(this);
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@ DriveStats::DriveStats(QWidget* parent) : QFrame(parent) {
|
|||
add_stats_layouts("PAST WEEK", week_);
|
||||
|
||||
if (auto dongleId = getDongleId()) {
|
||||
QString url = "https://api.commadotai.com/v1.1/devices/" + *dongleId + "/stats";
|
||||
QString url = CommaApi::BASE_URL + "/v1.1/devices/" + *dongleId + "/stats";
|
||||
RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_DriveStats", 30);
|
||||
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &DriveStats::parseResponse);
|
||||
}
|
||||
|
|
|
@ -111,7 +111,7 @@ PrimeUserWidget::PrimeUserWidget(QWidget* parent) : QWidget(parent) {
|
|||
|
||||
// set up API requests
|
||||
if (auto dongleId = getDongleId()) {
|
||||
QString url = "https://api.commadotai.com/v1/devices/" + *dongleId + "/owner";
|
||||
QString url = CommaApi::BASE_URL + "/v1/devices/" + *dongleId + "/owner";
|
||||
RequestRepeater *repeater = new RequestRepeater(this, url, "ApiCache_Owner", 6);
|
||||
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &PrimeUserWidget::replyFinished);
|
||||
}
|
||||
|
@ -256,7 +256,7 @@ SetupWidget::SetupWidget(QWidget* parent) : QFrame(parent) {
|
|||
|
||||
// set up API requests
|
||||
if (auto dongleId = getDongleId()) {
|
||||
QString url = "https://api.commadotai.com/v1.1/devices/" + *dongleId + "/";
|
||||
QString url = CommaApi::BASE_URL + "/v1.1/devices/" + *dongleId + "/";
|
||||
RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_Device", 5);
|
||||
|
||||
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &SetupWidget::replyFinished);
|
||||
|
|
|
@ -45,7 +45,7 @@ Replay::Replay(QString route, SubMaster *sm_, QObject *parent) : sm(sm_), QObjec
|
|||
pm = new PubMaster(s);
|
||||
}
|
||||
|
||||
const QString url = "https://api.commadotai.com/v1/route/" + route + "/files";
|
||||
const QString url = CommaApi::BASE_URL + "/v1/route/" + route + "/files";
|
||||
http = new HttpRequest(this, !Hardware::PC());
|
||||
QObject::connect(http, &HttpRequest::receivedResponse, this, &Replay::parseResponse);
|
||||
http->sendRequest(url);
|
||||
|
|
Loading…
Reference in New Issue