Skip to content

Commit

Permalink
Merge pull request #5 from AG3NTZ3R0/fixPackageStructure
Browse files Browse the repository at this point in the history
Fix Broken Package Structure
  • Loading branch information
AG3NTZ3R0 authored Feb 23, 2024
2 parents 4bafee1 + 87ded41 commit fdadfde
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 124 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
.idea

# PyCache
__pycache__
__pycache__

# Python Development Environment
build
src/cnbc.egg-info
main.py
134 changes: 19 additions & 115 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,132 +12,36 @@ an open-source tool that uses a publicly available API and is
intended for research and educational purposes.
</td></tr></table>

## Install
## Installation

```shell
pip install cnbc
```

## [Subscribe to CNBC API](https://rapidapi.com/apidojo/api/cnbc/ 'CNBC API')

## Tutorial
## Utilization

```python
import cnbc
```
### APIWrapper
The APIWrapper class is used to make requests to the CNBC API. <br>
Note: A majority of the CNBC API endpoints require parameters. These must be set by an additional instruction.

## APIWrapper
The APIWrapper class is the main class of the package. It is used to make requests to the CNBC API.

### Initialization
To initialize the APIWrapper, you need to provide your API key and the endpoint you want to use. The available endpoints are defined in the `Endpoints` class.
```python
from cnbc.api_wrapper import APIWrapper
from cnbc.constants.endpoints import Endpoints
from cnbc import APIWrapper, Endpoints

api_wrapper = APIWrapper(
api_key='YOUR_API_KEY',
endpoint=Endpoints.GET_METADATA
endpoint=Endpoints.TRANSLATE
)
```
#### Endpoint Parameters
A majority of the endpoints require parameters. There is an additional step in this scenario.
```python
endpoint_params = api_wrapper.get_params()
# Set the specific parameter(s) for the endpoint
endpoint_params['issueIds'] = '123,459,789'
api_wrapper.set_params(endpoint_params)
```

### Request
To make a request to the CNBC API, use the `request` method. This method returns the JSON response from the API.
```python
json_resp = api_wrapper.request()
```

### Reuse
You can reuse the APIWrapper instance to make multiple requests to the CNBC API. Just set the new endpoint and parameters.
```python
api_wrapper.set_endpoint(Endpoints.GET_SUMMARY)

endpoint_params = api_wrapper.get_params()
endpoint_params['issueIds'] = '987,654,321'
api_wrapper.set_params(endpoint_params)

json_resp = api_wrapper.request()
```

### Endpoints

#### GET_METADATA
Get metadata that supports other endpoints.

#### AUTO_COMPLETE
Get auto complete suggestions by term or phrase.
##### Parameters
- `q`: The term or phrase for which to get auto complete suggestions.

#### LIST_INDICES
List all available indices.

#### LIST_TRENDING_NEWS
List trending news.
##### Parameters
- `count`: The number of trending news to list.
- `tag` (optional): The tag to filter the news.

#### LIST_SPECIAL_REPORTS
List special reports.
##### Parameters
- `pageSize`: The number of items per page.
- `page`: The page number.

#### LIST_SYMBOL_NEWS
List latest news by symbol name.
##### Parameters
- `symbol`: The specific symbol to get news for.
- `pageSize`: The number of items per page.
- `page`: The page number.

#### GET_EARNINGS_CHART
Generate image of earnings chart of specific stock quote, index, exchange, etc.
##### Parameters
- `issueId`: The ID of the specific stock quote, index, exchange, etc.
- `numberOfYears`: The number of years for which to generate the earnings chart.

#### GET_PROFILE
Get summary information of stock quote, index, exchange, etc.
##### Parameters
- `issueId`: The ID of the specific stock quote, index, exchange, etc.

#### GET_CHART
Get raw data to draw price chart of stock quote, index, exchange, etc.
##### Parameters
- `symbol`: The specific symbol to get chart data for.
- `interval`: The interval for the price chart data.

#### TRANSLATE
Get issue ID from specific symbol.
##### Parameters
- `symbol`: The specific symbol to translate into an issue ID.

#### GET_SUMMARY
Get summary information of stock quote, index, exchange, etc.
##### Parameters
- `issueIds`: A comma-separated list of issue IDs to get summary information.

#### GET_FUNDAMENTALS
Get fundamental information of stock quote, index, exchange, etc.
##### Parameters
- `issueIds`: A comma-separated list of issue IDs to get fundamental information.

#### GET_PRICELINE_CHART
Generate image of price line chart of specific stock quote, index, exchange, etc.
##### Parameters
- `issueId`: The ID of the specific stock quote, index, exchange, etc.
- `numberOfDays`: The number of days for which to generate the price line chart.

#### GET_PEERS
Get peers relating to stock quote, index, exchange, etc.
##### Parameters
- `symbol`: The specific symbol to get peers relating to it.
# The APIWrapper class will supply the required parameters for the configured CNBC API endpoint.
api_wrapper_params = api_wrapper.params
api_wrapper_params['symbol'] = 'AAPL'
api_wrapper.params = api_wrapper_params
# The APIWrapper class will make a request to the CNBC API and return the response in JSON.
json_response = api_wrapper.request()

# The APIWrapper class can be repurposed to make multiple requests to the CNBC API.
api_wrapper.endpoint = Endpoints.GET_SUMMARY
api_wrapper.params = {'issueIds': json_response['issueId']}
json_response = api_wrapper.request()
```
3 changes: 3 additions & 0 deletions src/cnbc/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .api_wrapper import APIWrapper
from .endpoints import Endpoints
from .exceptions import APIRequestException, NetworkError, InvalidParameterConfiguration
15 changes: 9 additions & 6 deletions src/cnbc/api_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"""
import requests

from src.cnbc.constants.endpoints import Endpoints
from src.cnbc.exceptions import APIRequestException, NetworkError, InvalidParameterConfiguration
from .endpoints import Endpoints
from .exceptions import APIRequestException, NetworkError, InvalidParameterConfiguration


class APIWrapper:
Expand All @@ -26,7 +26,7 @@ def __init__(self, api_key: str, endpoint: Endpoints, timeout: int = 10):
self.timeout: int

self._endpoint, self._params = endpoint.value
self._headers = {'x-rapidapi-host': Endpoints.HOST, 'x-rapidapi-key': api_key}
self._headers = {'x-rapidapi-host': Endpoints.HOST.value, 'x-rapidapi-key': api_key}
self._timeout = timeout

def _safe_delete(self):
Expand Down Expand Up @@ -62,9 +62,12 @@ def params(self):

@params.setter
def params(self, params: dict[str, str]):
if set(self._params.keys()) == set(params.keys()):
self._params.update(params)
else:
try:
if set(self._params.keys()) == set(params.keys()):
self._params.update(params)
else:
raise InvalidParameterConfiguration()
except AttributeError:
raise InvalidParameterConfiguration()

@params.deleter
Expand Down
File renamed without changes.
Empty file removed tests/cnbc/constants/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion tests/cnbc/test_api_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from unittest.mock import patch, MagicMock

from src.cnbc.api_wrapper import APIWrapper
from src.cnbc.constants.endpoints import Endpoints
from src.cnbc.endpoints import Endpoints
from src.cnbc.exceptions import InvalidParameterConfiguration


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import unittest

from src.cnbc.constants.endpoints import Endpoints
from src.cnbc.endpoints import Endpoints


class TestEndpoints(unittest.TestCase):
Expand Down

0 comments on commit fdadfde

Please sign in to comment.