From 07fa43bc13f858a4cb60932659b3fc10eb5d4f00 Mon Sep 17 00:00:00 2001 From: Victor Roussekov Date: Fri, 3 Mar 2023 10:07:48 +1100 Subject: [PATCH 1/2] Add support for basic authorization for StockHistoricalDataClient and RESTClient --- alpaca/common/rest.py | 9 +++++++++ alpaca/data/historical/stock.py | 2 ++ 2 files changed, 11 insertions(+) diff --git a/alpaca/common/rest.py b/alpaca/common/rest.py index ffc70978..c7efac36 100644 --- a/alpaca/common/rest.py +++ b/alpaca/common/rest.py @@ -1,4 +1,5 @@ import time +import base64 from abc import ABC from typing import Any, List, Optional, Type, Union, Tuple, Iterator @@ -29,6 +30,7 @@ def __init__( api_key: Optional[str] = None, secret_key: Optional[str] = None, oauth_token: Optional[str] = None, + use_basic_auth: bool = False, api_version: str = "v2", sandbox: bool = False, raw_data: bool = False, @@ -59,6 +61,7 @@ def __init__( self._api_version: str = api_version self._base_url: Union[BaseURL, str] = base_url self._sandbox: bool = sandbox + self._use_basic_auth: bool = use_basic_auth self._use_raw_data: bool = raw_data self._session: Session = Session() @@ -156,6 +159,12 @@ def _get_auth_headers(self) -> dict: if self._oauth_token: headers["Authorization"] = "Bearer " + self._oauth_token + elif self._use_basic_auth: + api_key_secret = "{key}:{secret}".format( + key=self._api_key, secret=self._secret_key + ).encode("utf-8") + encoded_api_key_secret = base64.b64encode(api_key_secret).decode("utf-8") + headers["Authorization"] = "Basic " + encoded_api_key_secret else: headers["APCA-API-KEY-ID"] = self._api_key headers["APCA-API-SECRET-KEY"] = self._secret_key diff --git a/alpaca/data/historical/stock.py b/alpaca/data/historical/stock.py index 1d084c8e..eeaa0687 100644 --- a/alpaca/data/historical/stock.py +++ b/alpaca/data/historical/stock.py @@ -45,6 +45,7 @@ def __init__( api_key: Optional[str] = None, secret_key: Optional[str] = None, oauth_token: Optional[str] = None, + use_basic_auth: bool = False, raw_data: bool = False, url_override: Optional[str] = None, ) -> None: @@ -64,6 +65,7 @@ def __init__( api_key=api_key, secret_key=secret_key, oauth_token=oauth_token, + use_basic_auth=use_basic_auth, api_version="v2", base_url=url_override if url_override is not None else BaseURL.DATA, sandbox=False, From 0e0ff3b19f08dff98c1f20d65c1357a15dd9c8df Mon Sep 17 00:00:00 2001 From: Victor Roussekov Date: Fri, 3 Mar 2023 10:43:43 +1100 Subject: [PATCH 2/2] Add documentation --- alpaca/common/rest.py | 1 + alpaca/data/historical/stock.py | 1 + 2 files changed, 2 insertions(+) diff --git a/alpaca/common/rest.py b/alpaca/common/rest.py index c7efac36..fe5fe6f8 100644 --- a/alpaca/common/rest.py +++ b/alpaca/common/rest.py @@ -47,6 +47,7 @@ def __init__( api_key (Optional[str]): The api key string for authentication. secret_key (Optional[str]): The corresponding secret key string for the api key. oauth_token (Optional[str]): The oauth token if authenticating via OAuth. + use_basic_auth (bool): Whether API requests should use basic authorization headers. api_version (Optional[str]): The API version for the endpoints. sandbox (bool): False if the live API should be used. raw_data (bool): Whether API responses should be wrapped in data models or returned raw. diff --git a/alpaca/data/historical/stock.py b/alpaca/data/historical/stock.py index eeaa0687..742a9b42 100644 --- a/alpaca/data/historical/stock.py +++ b/alpaca/data/historical/stock.py @@ -56,6 +56,7 @@ def __init__( api_key (Optional[str], optional): Alpaca API key. Defaults to None. secret_key (Optional[str], optional): Alpaca API secret key. Defaults to None. oauth_token (Optional[str]): The oauth token if authenticating via OAuth. Defaults to None. + use_basic_auth (bool, optional): If true, API requests will use basic authorization headers. raw_data (bool, optional): If true, API responses will not be wrapped and raw responses will be returned from methods. Defaults to False. This has not been implemented yet. url_override (Optional[str], optional): If specified allows you to override the base url the client points