-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: news api * linting * fix typo
- Loading branch information
Showing
4 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from typing import Optional, Union | ||
|
||
from alpaca.common.rest import RESTClient | ||
|
||
from alpaca.common.enums import BaseURL | ||
|
||
from alpaca.data.requests import NewsRequest | ||
|
||
from alpaca.data.models.news import NewsSet | ||
|
||
from alpaca.common.types import RawData | ||
|
||
|
||
class NewsClient(RESTClient): | ||
""" | ||
The REST client for interacting with Alpaca News API endpoints. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
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: | ||
""" | ||
Instantiates a Historical Data Client. | ||
Args: | ||
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 | ||
to for proxy/testing. | ||
""" | ||
super().__init__( | ||
api_key=api_key, | ||
secret_key=secret_key, | ||
oauth_token=oauth_token, | ||
use_basic_auth=use_basic_auth, | ||
api_version="v1beta1", | ||
base_url=url_override if url_override is not None else BaseURL.DATA, | ||
sandbox=False, | ||
raw_data=raw_data, | ||
) | ||
|
||
def get_news(self, request_params: NewsRequest) -> Union[RawData, NewsSet]: | ||
"""Returns news data | ||
Args: | ||
request_params (NewsRequest): The request params to filter the news data""" | ||
response = self.get( | ||
path="/news", | ||
data=request_params.to_request_fields(), | ||
) | ||
if self._use_raw_data: | ||
return response | ||
|
||
return NewsSet(**response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from datetime import datetime | ||
from typing import Optional, List | ||
|
||
from alpaca.common.models import ValidateBaseModel as BaseModel | ||
from alpaca.data import NewsImageSize | ||
|
||
|
||
class NewsImage(BaseModel): | ||
""" | ||
images (URLs) related to given article | ||
Attributes: | ||
size (NewsImageSize): Possible values for size are thumb, small and large. | ||
url (str): url to image from news article. | ||
""" | ||
|
||
size: NewsImageSize | ||
url: str | ||
|
||
|
||
class News(BaseModel): | ||
""" | ||
images (URLs) related to given article | ||
Attributes: | ||
id (str): News article ID | ||
headline (str): Headline or title of the article | ||
author (str): Original author of news article | ||
created_at (datetime): Date article was created (RFC 3339) | ||
updated_at (datetime): Date article was updated (RFC 3339) | ||
summary (str): Summary text for the article (may be first sentence of content) | ||
content (str): Content of the news article (might contain HTML) | ||
url (Optional[str]): URL of article (if applicable) | ||
images (List[NewsImage]): List of images (URLs) related to given article (may be empty) | ||
symbols (str): List of related or mentioned symbols | ||
source (str): Source where the news originated from (e.g. Benzinga) | ||
""" | ||
|
||
id: float | ||
headline: str | ||
author: str | ||
created_at: datetime | ||
updated_at: datetime | ||
summary: str | ||
content: str | ||
url: Optional[str] | ||
images: List[NewsImage] | ||
symbols: List[str] | ||
source: str | ||
|
||
|
||
class NewsSet(BaseModel): | ||
""" | ||
images (URLs) related to given article | ||
Attributes: | ||
news (List[News]): Array of news objects | ||
next_page_token (Optional[str]): Pagination token for next page | ||
""" | ||
|
||
news: List[News] | ||
next_page_token: Optional[str] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters