api: use API_HOST env variable everywhere (#21814)

* api base url to global constant

* update api/__init__.py
pull/21838/head
Dean Lee 2021-08-03 19:49:49 +08:00 committed by GitHub
parent c900bce1b0
commit 543e019f71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 12 additions and 10 deletions

View File

@ -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)

View File

@ -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):

View File

@ -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);

View File

@ -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);

View File

@ -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);
}

View File

@ -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);

View File

@ -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);