Skip to content

Commit 94c2ca3

Browse files
committed
Added docs to create a custom config class. Updated README.md to reflect new changes. Added the configurations to __init__ for importing digikey. Minor message fix
1 parent 5b6ed30 commit 94c2ca3

File tree

4 files changed

+75
-38
lines changed

4 files changed

+75
-38
lines changed

README.md

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ manufacturers overlap other manufacturer part numbers.
1515
## Install
1616
```sh
1717
pip install digikey-api
18-
19-
export DIGIKEY_CLIENT_ID="client_id"
20-
export DIGIKEY_CLIENT_SECRET="client_secret"
21-
export DIGIKEY_STORAGE_PATH="cache_dir"
2218
```
2319

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

4137
```python
42-
import os
4338
import digikey
4439
from digikey.v3.productinformation import KeywordSearchRequest
4540

46-
os.environ['DIGIKEY_CLIENT_ID'] = 'client_id'
47-
os.environ['DIGIKEY_CLIENT_SECRET'] = 'client_secret'
48-
os.environ['DIGIKEY_CLIENT_SANDBOX'] = 'False'
49-
os.environ['DIGIKEY_STORAGE_PATH'] = 'cache_dir'
41+
dk_config = digikey.DigikeyJsonConfig(file_name='dk_conf.json')
42+
dk_config.set('client-id', 'ENTER_CLIENT_ID')
43+
dk_config.set('client-secret', 'ENTER_CLIENT_SECRET')
44+
45+
dk_api = digikey.DigikeyAPI(dk_config, is_sandbox=False)
5046

5147
# Query product number
5248
dkpn = '296-6501-1-ND'
53-
part = digikey.product_details(dkpn)
49+
part = dk_api.product_details(dkpn)
5450

5551
# Search for parts
5652
search_request = KeywordSearchRequest(keywords='CRCW080510K0FKEA', record_count=10)
57-
result = digikey.keyword_search(body=search_request)
53+
result = dk_api.keyword_search(body=search_request)
5854
```
5955

60-
## Logging [API V3]
61-
Logging is not forced upon the user but can be enabled according to convention:
62-
```python
63-
import logging
64-
65-
logger = logging.getLogger(__name__)
66-
logger.setLevel(logging.DEBUG)
67-
68-
digikey_logger = logging.getLogger('digikey')
69-
digikey_logger.setLevel(logging.DEBUG)
70-
71-
handler = logging.StreamHandler()
72-
handler.setLevel(logging.DEBUG)
73-
logger.addHandler(handler)
74-
digikey_logger.addHandler(handler)
75-
```
56+
## API Configuration Storage
57+
`DigikeyAPI` requires a configuration class that will handle getting, storing, and saving key-value pairs. Currently
58+
only `DigikeyJsonConfig` is implemented for storing settings in a JSON file, but a custom configuration can be created.
59+
See [docs/DigikeyBaseConfig.md](docs/DigikeyBaseConfig.md) for more details on that.
7660

7761
## Top-level APIs
7862

7963
#### Product Information
8064
All functions from the [PartSearch](https://developer.digikey.com/products/product-information/partsearch/) API have been implemented.
81-
* `digikey.keyword_search()`
82-
* `digikey.product_details()`
83-
* `digikey.digi_reel_pricing()`
84-
* `digikey.suggested_parts()`
85-
* `digikey.manufacturer_product_details()`
65+
* `DigikeyAPI.keyword_search()`
66+
* `DigikeyAPI.product_details()`
67+
* `DigikeyAPI.digi_reel_pricing()`
68+
* `DigikeyAPI.suggested_parts()`
69+
* `DigikeyAPI.manufacturer_product_details()`
8670

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

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

9680
#### Barcode
9781
TODO
@@ -103,7 +87,7 @@ It is possible to retrieve the number of max requests and current requests by pa
10387
```python
10488
api_limit = {}
10589
search_request = KeywordSearchRequest(keywords='CRCW080510K0FKEA', record_count=10)
106-
result = digikey.keyword_search(body=search_request, api_limits=api_limit)
90+
result = dk_api.keyword_search(body=search_request, api_limits=api_limit)
10791
```
10892

10993
The dict will be filled with the information returned from the API:
@@ -114,3 +98,4 @@ The dict will be filled with the information returned from the API:
11498
}
11599
```
116100
Sometimes the API does not return any rate limit data, the values will then be set to None.
101+

digikey/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
from digikey.v2.api import (search, part)
33
from digikey.v3.api import DigikeyAPI
4+
from digikey.configfile import (DigikeyBaseConfig, DigikeyJsonConfig)
45

56
logger = logging.getLogger(__name__)
67

digikey/oauth/oauth2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ def __init__(self,
132132
if not a_id or not a_secret:
133133
raise ValueError(
134134
'CLIENT ID and SECRET must be set. '
135-
'Set "DIGIKEY_CLIENT_ID" and "DIGIKEY_CLIENT_SECRET" '
136-
'as an environment variable, or pass your keys directly to the client.'
135+
'Set "client-id" and "client-secret" '
136+
'in the configuration constructor.'
137137
)
138138

139139
self._id = a_id

docs/DigikeyBaseConfig.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Digikey-API Settings Storage Configuration
2+
3+
The `DigikeyAPI` class of the V3 API must be given a configuration class as one of it's arguments.
4+
The purpose of this configuration class is to determine how the API will store some settings like the client-id, client-secret, and other stuff.
5+
6+
As of right now, there is only 1 available configuration class which is `DigikeyJsonConfig`. Of course you can create your own configurator class and input that into `DigikeyAPI`, for example if you want the API to store it's configuration in a database.
7+
8+
## DigikeyJsonConfig
9+
10+
This configuration classes stores the API settings in a JSON file. When initializing this class, a file name/path must be given.
11+
12+
Example:
13+
```python
14+
dk_config = digikey.DigikeyJsonConfig(file_name='test_conf.json')
15+
dk_config.set('client-id', 'ENTER_CLIENT_ID')
16+
dk_config.set('client-secret', 'ENTER_CLIENT_SECRET')
17+
```
18+
19+
## Create your own storage configuration
20+
21+
You can create your own storage configurator as mentioned to define how to store the API's settings. The `DigikeyBaseConfig` class can be inherited to create it. You must override and define 3 functions:
22+
23+
- save(self): This function gets called when the API wants to save the settings.
24+
- get(self, key: str): This function gets called when the API wants to retrieve a value for a given key. Return `None` if it doesn't exist
25+
- set(self, key: str, val: str): This function gets called when the API wants to store a value for a given key.
26+
27+
As an example, this is how `DigikeyJsonConfig` is implemented:
28+
```python
29+
class DigikeyJsonConfig(DigikeyBaseConfig):
30+
def __init__(self, file_name):
31+
super().__init__()
32+
self.file_name = file_name
33+
# Get config from file if it exists
34+
if os.path.exists(self.file_name):
35+
with open(self.file_name, 'r') as f:
36+
self.config = json.load(f)
37+
else:
38+
self.config = {}
39+
40+
def save(self):
41+
with open(self.file_name, 'w') as f:
42+
json.dump(self.config, f)
43+
44+
def get(self, key: str):
45+
if key in self.config:
46+
return self.config[key]
47+
return None
48+
49+
def set(self, key: str, val: str):
50+
self.config[key] = val
51+
```

0 commit comments

Comments
 (0)