Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration storage class. Made DigikeyAPI into a class #15

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
105 changes: 26 additions & 79 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ manufacturers overlap other manufacturer part numbers.
## Install
```sh
pip install digikey-api

export DIGIKEY_CLIENT_ID="client_id"
export DIGIKEY_CLIENT_SECRET="client_secret"
export DIGIKEY_STORAGE_PATH="cache_dir"
```

# API V3
Expand All @@ -39,59 +35,53 @@ structure of the Sandbox API response will be a representation of what to expect
For valid responses make sure you use the client ID and secret for a [Production App](https://developer.digikey.com/documentation/organization)

```python
import os
import digikey
from digikey.v3.productinformation import KeywordSearchRequest

os.environ['DIGIKEY_CLIENT_ID'] = 'client_id'
os.environ['DIGIKEY_CLIENT_SECRET'] = 'client_secret'
os.environ['DIGIKEY_CLIENT_SANDBOX'] = 'False'
os.environ['DIGIKEY_STORAGE_PATH'] = 'cache_dir'
dk_config = digikey.DigikeyJsonConfig(file_name='dk_conf.json')
dk_api = digikey.DigikeyAPI(dk_config, is_sandbox=False)
dk_api.set_client_info(client_id='ENTER_CLIENT_ID', client_secret='ENTER_CLIENT_SECRET')

# Query product number
dkpn = '296-6501-1-ND'
part = digikey.product_details(dkpn)
part = dk_api.product_details(dkpn)

# Search for parts
search_request = KeywordSearchRequest(keywords='CRCW080510K0FKEA', record_count=10)
result = digikey.keyword_search(body=search_request)
result = dk_api.keyword_search(body=search_request)
```

## Logging [API V3]
Logging is not forced upon the user but can be enabled according to convention:
```python
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

digikey_logger = logging.getLogger('digikey')
digikey_logger.setLevel(logging.DEBUG)

handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
digikey_logger.addHandler(handler)
```
## API Configuration Storage
`DigikeyAPI` requires a configuration class that will handle getting, storing, and saving key-value pairs. Currently
only `DigikeyJsonConfig` is implemented for storing settings in a JSON file, but a custom configuration can be created.
See [docs/DigikeyBaseConfig.md](docs/DigikeyBaseConfig.md) for more details on that.

## Top-level APIs

#### Configuration Related Functions
* `set_client_info()`
* Arguments are `client_id` and `client_secret`
* `DigikeyAPI.needs_client_id()`
* Returns `True` if a client ID is needed/missing
* `DigikeyAPI.needs_client_secret()`
* Returns `True` if a client secret is needed/missing

#### Product Information
All functions from the [PartSearch](https://developer.digikey.com/products/product-information/partsearch/) API have been implemented.
* `digikey.keyword_search()`
* `digikey.product_details()`
* `digikey.digi_reel_pricing()`
* `digikey.suggested_parts()`
* `digikey.manufacturer_product_details()`
* `DigikeyAPI.keyword_search()`
* `DigikeyAPI.product_details()`
* `DigikeyAPI.digi_reel_pricing()`
* `DigikeyAPI.suggested_parts()`
* `DigikeyAPI.manufacturer_product_details()`

#### Batch Product Details
The one function from the [BatchProductDetailsAPI](https://developer.digikey.com/products/batch-productdetails/batchproductdetailsapi) API has been implemented.
* `digikey.batch_product_details()`
* `DigikeyAPI.batch_product_details()`

#### Order Support
All functions from the [OrderDetails](https://developer.digikey.com/products/order-support/orderdetails/) API have been implemented.
* `digikey.salesorder_history()`
* `digikey.status_salesorder_id()`
* `DigikeyAPI.salesorder_history()`
* `DigikeyAPI.status_salesorder_id()`

#### Barcode
TODO
Expand All @@ -103,7 +93,7 @@ It is possible to retrieve the number of max requests and current requests by pa
```python
api_limit = {}
search_request = KeywordSearchRequest(keywords='CRCW080510K0FKEA', record_count=10)
result = digikey.keyword_search(body=search_request, api_limits=api_limit)
result = dk_api.keyword_search(body=search_request, api_limits=api_limit)
```

The dict will be filled with the information returned from the API:
Expand All @@ -115,46 +105,3 @@ The dict will be filled with the information returned from the API:
```
Sometimes the API does not return any rate limit data, the values will then be set to None.

# API V2 [Deprecated]
**NOTE: API V2 is not supported anymore by Digi-Key and you cannot register new applications**

See API V3 above to use the new API.

## Register
Register an app on the Digikey API portal: [Digi-Key API V2](https://api-portal.digikey.com/start). You will need the client
ID and the client secret to use the API. You will also need a Digi-Key account to authenticate, using the Oauth2 process.

## Use
Python will automatically spawn a browser to allow you to authenticate using the Oauth2 process. After obtaining a token
the library will cache the access token and use the refresh token to automatically refresh your credentials.

```python
import os
import digikey

os.environ['DIGIKEY_CLIENT_ID'] = 'client_id'
os.environ['DIGIKEY_CLIENT_SECRET'] = 'client_secret'
os.environ['DIGIKEY_STORAGE_PATH'] = 'cache_dir'

dkpn = '296-6501-1-ND'
part = digikey.part(dkpn)
print(part)
# <Part mpn=NE555DR>

print(part.manufacturer)
# 'Texas Instruments'
```

## Test
```sh
python -m pytest --cov=digikey --doctest-modules --ignore=setup.py
python -m mypy digikey --ignore-missing-imports
```

## Top-level API
* `digikey.search()`
* `digikey.part()`

## Data models
* `digikey.models.KeywordSearchResult`
* `digikey.models.Part`
8 changes: 3 additions & 5 deletions digikey/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from digikey.v2.api import (search, part)
from digikey.v3.api import (keyword_search, product_details, digi_reel_pricing, suggested_parts,
manufacturer_product_details)
from digikey.v3.api import (status_salesorder_id, salesorder_history)
from digikey.v3.api import (batch_product_details)
import logging
from digikey.v3.api import DigikeyAPI
from digikey.configfile import (DigikeyBaseConfig, DigikeyJsonConfig)

name = 'digikey'
46 changes: 46 additions & 0 deletions digikey/configfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os
import json


class DigikeyBaseConfig:
"""
Base class for a configuration handler which saves info related to Digikey's API like the client-ID
This class must not be directly used, instead another class that inherits this class. The class that inherits
this class must override save(), get(), and set() with the same parameters and returns.
Check out DigikeyJsonConfig for more details as to how to do this.
"""
def __init__(self):
pass

def save(self):
pass

def get(self, key: str):
pass

def set(self, key: str, val: str):
pass


class DigikeyJsonConfig(DigikeyBaseConfig):
def __init__(self, file_name):
super().__init__()
self.file_name = file_name
# Get config from file if it exists
if os.path.exists(self.file_name):
with open(self.file_name, 'r') as f:
self.config = json.load(f)
else:
self.config = {}

def save(self):
with open(self.file_name, 'w') as f:
json.dump(self.config, f)

def get(self, key: str):
if key in self.config:
return self.config[key]
return None

def set(self, key: str, val: str):
self.config[key] = val
Loading