Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ manufacturers overlap other manufacturer part numbers.
[![Donate](https://img.shields.io/badge/Donate-PayPal-gold.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=53HWHHVCJ3D4J&currency_code=EUR&source=url)

# What does it do
`digikey-api` is an [Digkey Part Search API](https://api-portal.digikey.com/node/8517) client for Python 3.6+. API response data is returned as Python objects that attempt to make it easy to get the data you want. Not all endpoints have been implemented.
`digikey-api` is an [Digkey Part Search API](https://api-portal.digikey.com/node/8517) client for Python 3.12+. API response data is returned as Python objects that attempt to make it easy to get the data you want. Not all endpoints have been implemented.

# Quickstart

Expand All @@ -25,15 +25,15 @@ export DIGIKEY_STORAGE_PATH="path/to/cache/dir"

The cache dir is used to store the OAUTH access and refresh token, if you delete it you will need to login again.

# API V3
# API V4
## Register
Register an app on the Digikey API portal: [Digi-Key API V3](https://developer.digikey.com/get_started). You will need
Register an app on the Digikey API portal: [Digi-Key API V4](https://developer.digikey.com/get_started). 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.

When registering an app the OAuth Callback needs to be set to `https://localhost:8139/digikey_callback`.

## Use [API V3]
## Use [API V4]
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.

Expand All @@ -48,8 +48,8 @@ import os
from pathlib import Path

import digikey
from digikey.v3.productinformation import KeywordSearchRequest
from digikey.v3.batchproductdetails import BatchProductDetailsRequest
from digikey.v4.productinformation import KeywordRequest
from digikey.v4.batchproductdetails import BatchProductDetailsRequest

CACHE_DIR = Path('path/to/cache/dir')

Expand All @@ -63,7 +63,7 @@ dkpn = '296-6501-1-ND'
part = digikey.product_details(dkpn)

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

# Only if BatchProductDetails endpoint is explicitly enabled
Expand All @@ -73,7 +73,7 @@ batch_request = BatchProductDetailsRequest(products=mpn_list)
part_results = digikey.batch_product_details(body=batch_request)
```

## Logging [API V3]
## Logging [API V4]
Logging is not forced upon the user but can be enabled according to convention:
```python
import logging
Expand Down Expand Up @@ -118,7 +118,7 @@ The API has a limited amount of requests you can make per time interval [Digikey
It is possible to retrieve the number of max requests and current requests by passing an optional api_limits kwarg to an API function:
```python
api_limit = {}
search_request = KeywordSearchRequest(keywords='CRCW080510K0FKEA', record_count=10)
search_request = KeywordSearchRequest(keywords='CRCW080510K0FKEA', limit=10)
result = digikey.keyword_search(body=search_request, api_limits=api_limit)
```

Expand All @@ -129,4 +129,29 @@ The dict will be filled with the information returned from the API:
'api_requests_remaining': 139
}
```
Sometimes the API does not return any rate limit data, the values will then be set to None.
Sometimes the API does not return any rate limit data, the values will then be set to `None`.


## Offsets
The api has a maximum of 50 parts in a request.
In order to gather more than the limit input into the `KeywordRequest` an offset must be input:
```python
# request items 51-100
search_request = KeywordRequest(keywords='RaspberryPi pico',limit = 50,offset=50)
```

This can also be used to gather all items to a request very simply:
```python
offset = 0
products = []

while(offset == 0 or (len(result.products) >= 49)):

search_request = KeywordRequest(keywords='RaspberryPi pico',limit = 50,offset=offset)
result = digikey.keyword_search(body=search_request)

for product in result.products:
products.append(product)

offset +=50
```
5 changes: 1 addition & 4 deletions digikey/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
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)
from digikey.v4.api import DigikeyApi

name = 'digikey'
8 changes: 5 additions & 3 deletions digikey/oauth/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def __init__(self,
version: int = 2,
sandbox: bool = False):

if version == 3:
if version == 3 or version == 4:
if sandbox:
self.auth_url = AUTH_URL_V3_SB
self.token_url = TOKEN_URL_V3_SB
Expand All @@ -129,7 +129,7 @@ def __init__(self,
a_token_storage_path = a_token_storage_path or os.getenv('DIGIKEY_STORAGE_PATH')
if not a_token_storage_path or not Path(a_token_storage_path).exists():
raise ValueError(
'STORAGE PATH must be set and must exist.'
f'STORAGE PATH ({a_token_storage_path}) must be set and must exist. '
'Set "DIGIKEY_STORAGE_PATH" as an environment variable, '
'or pass your keys directly to the client.'
)
Expand Down Expand Up @@ -252,7 +252,9 @@ def get_access_token(self) -> Oauth2Token:
('localhost', PORT),
lambda request, address, server: HTTPServerHandler(
request, address, server, self._id, self._secret))
httpd.socket = ssl.wrap_socket(httpd.socket, certfile=str(Path(filename)), server_side=True)
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(str(Path(filename)))
httpd.socket=context.wrap_socket(httpd.socket,server_side=True,)
httpd.stop = 0

# This function will block until it receives a request
Expand Down
Empty file added digikey/v4/__init__.py
Empty file.
Loading