From 543e019f715a7e485c7402d1e145dc8e81d43611 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Tue, 3 Aug 2021 19:49:49 +0800 Subject: [PATCH] api: use API_HOST env variable everywhere (#21814) * api base url to global constant * update api/__init__.py --- common/api/__init__.py | 7 ++++--- selfdrive/athena/registration.py | 2 +- selfdrive/ui/qt/api.h | 1 + selfdrive/ui/qt/maps/map_settings.cc | 4 ++-- selfdrive/ui/qt/widgets/drive_stats.cc | 2 +- selfdrive/ui/qt/widgets/prime.cc | 4 ++-- selfdrive/ui/replay/replay.cc | 2 +- 7 files changed, 12 insertions(+), 10 deletions(-) diff --git a/common/api/__init__.py b/common/api/__init__.py index 365e4f5ea..f07fc1a08 100644 --- a/common/api/__init__.py +++ b/common/api/__init__.py @@ -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) diff --git a/selfdrive/athena/registration.py b/selfdrive/athena/registration.py index 39dfb3c23..a22198152 100755 --- a/selfdrive/athena/registration.py +++ b/selfdrive/athena/registration.py @@ -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): diff --git a/selfdrive/ui/qt/api.h b/selfdrive/ui/qt/api.h index 624e58bd6..57c730fdd 100644 --- a/selfdrive/ui/qt/api.h +++ b/selfdrive/ui/qt/api.h @@ -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); diff --git a/selfdrive/ui/qt/maps/map_settings.cc b/selfdrive/ui/qt/maps/map_settings.cc index 0b18f75e9..c91f1c586 100644 --- a/selfdrive/ui/qt/maps/map_settings.cc +++ b/selfdrive/ui/qt/maps/map_settings.cc @@ -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); diff --git a/selfdrive/ui/qt/widgets/drive_stats.cc b/selfdrive/ui/qt/widgets/drive_stats.cc index 09c271feb..57c0794a2 100644 --- a/selfdrive/ui/qt/widgets/drive_stats.cc +++ b/selfdrive/ui/qt/widgets/drive_stats.cc @@ -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); } diff --git a/selfdrive/ui/qt/widgets/prime.cc b/selfdrive/ui/qt/widgets/prime.cc index dc2c5039d..e3828f3d8 100644 --- a/selfdrive/ui/qt/widgets/prime.cc +++ b/selfdrive/ui/qt/widgets/prime.cc @@ -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); diff --git a/selfdrive/ui/replay/replay.cc b/selfdrive/ui/replay/replay.cc index 638ff7106..f65941551 100644 --- a/selfdrive/ui/replay/replay.cc +++ b/selfdrive/ui/replay/replay.cc @@ -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);