Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
d84997c
Dropped API v2 support.
set-soft Jun 3, 2021
601983d
Fixed: responses is not a release dependence.
set-soft Jun 3, 2021
78a7f72
Dropped the OrderSupport API.
set-soft Jun 3, 2021
c94af50
Dropped fake-useragent usage.
set-soft Jun 3, 2021
845043f
Updated the setup.py
set-soft Jun 3, 2021
179cc6e
Fixed: one more use of fake-useragent
set-soft Jun 3, 2021
642b8e6
Added script to query a valid user agent.
set-soft Jun 3, 2021
b4172e4
Excluded SETEdit project
set-soft Jun 3, 2021
ca0cc82
Incorporated certauth.
set-soft Jun 3, 2021
87e7de7
Added missing dependencies
set-soft Jun 3, 2021
1835885
Removed original badgets
set-soft Jun 3, 2021
dcbbfbe
First working KiCost plug-in, still quite raw
set-soft Sep 29, 2021
08b0cc0
Removed the use of f-strings
set-soft Sep 29, 2021
0b0fb76
Oops! one more f-string
set-soft Sep 29, 2021
5b6ff42
Removed the return hint in functions.
set-soft Sep 30, 2021
4994b18
Added missing dependency: dateutil
set-soft Sep 30, 2021
cb5cb32
Fixed dateutil package name
set-soft Sep 30, 2021
ab2581e
Avoided to expand args before fixed ones (Python 3.5+)
set-soft Sep 30, 2021
f7d4744
Removed arguments type hints from OAuth2.
set-soft Sep 30, 2021
7b2f597
Added Python 2.7 replacement for datetime.timezone
set-soft Sep 30, 2021
b6b148c
Added Python 2.7 replacement for http.server
set-soft Sep 30, 2021
4a4fd45
Added JSONDecodeError replacement for Python 2.7
set-soft Sep 30, 2021
a108f57
Removed the use of Path objects, just plain filenames.
set-soft Sep 30, 2021
5a0947d
Adapted urllib.parser imports
set-soft Sep 30, 2021
22cc62a
Now generated pickle files are Python 2 and 3 compatible.
set-soft Sep 30, 2021
3ecf185
Relaxed the requests and urllib3 versions.
set-soft Oct 18, 2021
9c6ce78
Changed the check used for DIGIKEY_STORAGE_PATH
set-soft Oct 19, 2021
c3a9342
Fixed the Python 2.7 arguments compatibility
set-soft Oct 19, 2021
891e0d2
Better handling of product_details_response
set-soft Oct 19, 2021
56f3801
Changed the debug save/load to a cache mechanism
set-soft Oct 20, 2021
43364d2
Eliminated logger.info messages
set-soft Oct 25, 2021
dfb6c2c
Supressed "Not Found" exception as error
set-soft Oct 25, 2021
b28120a
Added support to cache None replies
set-soft Oct 25, 2021
9c7f6a1
Added ProductDescription to the data included
set-soft Oct 26, 2021
aa7088f
Now all options are passed as arguments
set-soft Oct 28, 2021
e4e1075
Added configuration for x_digikey_locale_* options
set-soft Nov 2, 2021
91eecb5
Encapsulated the utils.py functionality in a class
set-soft Nov 2, 2021
0377d00
Simplified the cache mechanism
set-soft Nov 3, 2021
3d9b4a1
Small change in suffix to accomodate to KiCost changes
set-soft Nov 3, 2021
d93d124
Added Python 2.7 to the setup
set-soft Mar 21, 2022
3c6a98e
Added Debian package generation
set-soft Mar 21, 2022
c8d9126
Added Makefile for the Debian package
set-soft Mar 21, 2022
66ae394
Added release and pypi workflows
set-soft Mar 21, 2022
5f70313
Added a CHANGELOG
set-soft Mar 21, 2022
501a83c
Trying to fix requirements issue
set-soft Mar 21, 2022
8aceed3
Bumped version to 0.1.1
set-soft Mar 21, 2022
6f86db0
Added target to generate a signed package
set-soft May 9, 2022
6cc8c33
Added option to exclude marketplace products
set-soft Oct 6, 2022
62ecd22
Avoid problems when Digi-Key times-out
set-soft Oct 6, 2022
bca165d
Bumped version to 0.1.2
set-soft Oct 6, 2022
2c58720
Removed deprecated ssl.wrap_socket
Oct 26, 2023
b3fbe85
Use the new SSL approach only for Python >= 3.12
set-soft Nov 8, 2023
2f50bcf
Merge branch 'mundodisco8-Deprecated_Wrap_Socket'
set-soft Nov 8, 2023
ee020da
Bumped version to 0.1.3
set-soft Nov 8, 2023
575cd90
Updated the test example
set-soft Dec 5, 2023
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
26 changes: 26 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This workflows will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

name: Upload Python Package

on:
push:
tags:
- "v*"

jobs:
deploy:

runs-on: ubuntu-latest
container: setsoft/kicad_pybuild:latest

steps:
- uses: actions/checkout@v2

- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python3 setup.py sdist bdist_wheel
twine upload dist/*
86 changes: 86 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
name: release

on:
push:
tags-ignore:
- "v1"
- "v1_k6"
- "v2"
- "v2_k6"
- "v3"
- "v3_k6"
- "v4"
- "v4_k6"

jobs:
check:
runs-on: ubuntu-latest
outputs:
is_tag: ${{ steps.check.outputs.is_tag }}

steps:
- name: Check if it's a tagged release
id: check
run: |
REF=${{ github.ref }}
URL="https://api.github.com/repos/${GITHUB_REPOSITORY}/releases/tags/${REF##*/}"
StatusCode=$(curl -o -I -L -s -w "%{http_code}" -X GET -G $URL)
echo $StatusCode
if [[ "$StatusCode" == 200 ]]; then
echo "This is tagged release!"
echo "::set-output name=is_tag::no"
else
echo "This is a tag not release!"
echo "::set-output name=is_tag::yes"
fi

build:
runs-on: ubuntu-latest
container: setsoft/kicad_pybuild:latest
needs: check
if: ${{ needs.check.outputs.is_tag == 'yes' }}
outputs:
deb_name: ${{ steps.mdeb.outputs.deb_name }}

steps:
- name: Checkout
uses: actions/checkout@v1

- name: Build
id: mdeb
run: |
make deb
mv ../*.deb .
echo "::set-output name=deb_name::"`ls *.deb`
mkdir output
mv *.deb output

- name: Store
uses: actions/upload-artifact@v1
with:
name: package
path: output

release:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1

- name: Retrieve
uses: actions/download-artifact@v1
with:
name: package

- name: Release
uses: docker://antonyurchenko/git-release:latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DRAFT_RELEASE: "false"
PRE_RELEASE: "false"
CHANGELOG_FILE: "CHANGELOG.md"
with:
args: |
package/${{needs.build.outputs.deb_name}}

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,5 @@ generate-v3-api/
# Other repository dependent
tests/local/
.env/config.ini

*.epr
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.1.3] - 2023-11-08
### Added
- Support for Python 3.12 (ssl.wrap_socket no longer available)

## [0.1.2] - 2022-10-06
### Added
- Option to exclude marketpace products

### Fixed
- Problems when a Digi-Key query gives time-out

## [0.1.1] - 2022-03-21
### Fixed
- Requirements

## [0.1.0] - 2022-03-21
### Added
- First release
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include requirements.txt
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/make

deb:
DEB_BUILD_OPTIONS=nocheck fakeroot dpkg-buildpackage -uc -b

deb_sig:
DEB_BUILD_OPTIONS=nocheck fakeroot dpkg-buildpackage -b

deb_clean:
fakeroot debian/rules clean


.PHONY: deb deb_clean
168 changes: 36 additions & 132 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,143 +1,47 @@
Python Client for Digikey API
KiCost plug-in for Digi-Key API
=================================
Search for parts in the Digi-Key catalog by keyword using KeywordSearch. Then make a PartDetails call to retrieve all
real time information about the part including pricing. PartDetails works best with Digi-Key part numbers as some
manufacturers overlap other manufacturer part numbers.
This is an experimental plug-in to allow KiCost to do native Digi-Key API requests.

[![Pypi](https://img.shields.io/pypi/v/digikey-api.svg?color=brightgreen)](https://pypi.org/project/digikey-api/)
[![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)
The plug-in is based on the [Python Client for Digikey API](https://github.com/peeter123/digikey-api)
by [Peter Oostewechel](https://github.com/peeter123).

# 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.
I'm not using the original project as a module because it pulls some rare dependencies.

# Quickstart

## 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
## Register
Register an app on the Digikey API portal: [Digi-Key API V3](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]
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.

You can test your application using the sandbox API, the data returned from a Sandbox API may not be complete, but the
structure of the Sandbox API response will be a representation of what to expect in Production.

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'

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

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

## Top-level APIs
The license is GPL v3 as the original work.

#### 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()`

#### 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()`

#### 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()`

#### Barcode
TODO

## API Limits
The API has a limited amount of requests you can make per time interval [Digikey Rate Limits](https://developer.digikey.com/documentation/shared-concepts#rate-limits).

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)
result = digikey.keyword_search(body=search_request, api_limits=api_limit)
```

The dict will be filled with the information returned from the API:
```python
{
'api_requests_limit': 1000,
'api_requests_remaining': 139
}
```
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'
# Quickstart

dkpn = '296-6501-1-ND'
part = digikey.part(dkpn)
print(part)
# <Part mpn=NE555DR>
- Register at Digi-Key. You need a valid user in order to register an application and in order to authenticate.
- Go to the API portal: [Digi-Key API Solutions](https://developer.digikey.com/get_started)
- Login to Digi-Key from the API portal
- Create an [organization](https://developer.digikey.com/teams)
- Choose the `Production Apps` operation
- Create a production app
- Choose a `Production App name`
- Use `https://localhost:8139/digikey_callback` for the OAuth Callback. So you can authorize the use of the app.
- Give a description
- Enable the `Product Information` API
- Click `Add production app`
- Now select your newly created `Production App`
- In the `Credential` section enable the options to show the `Client ID` and `Client Secret`
- Copy these values to a file named `config.txt` containing:

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

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

## Top-level API
* `digikey.search()`
* `digikey.part()`
- Create a folder `~/.config/kicost_digikey_api_v3`
- Store the config.txt file
- Now install the plug-in:
- Clone the git repo somewhere using `git clone https://github.com/set-soft/kicost-digikey-api-v3.git`
- Enter to `kicost-digikey-api-v3`
- Install it using `pip3 install -e .`
- Now test the plug-in:
- run `python3 test_production.py`
- A browser window will pop-up asking to login to Digi-Key. Login.
- Choose the allow option to enable the token.
- Now you'll load a local page, allow it.
- Now the token is stored on disk and you won't need to confirm it for months.
- You should get the information for Digi-Key part 296-6501-6-ND

## Data models
* `digikey.models.KeywordSearchResult`
* `digikey.models.Part`
8 changes: 8 additions & 0 deletions debian/README.Debian
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
kicost-digikey-api-v3 for Debian

Please edit this to provide information specific to
this kicost-digikey-api-v3 Debian package.

(Automatically generated by debmake Version 4.3.2)

-- Salvador E. Tropea <> Mon, 21 Mar 2022 16:06:37 -0300
24 changes: 24 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
kicost-digikey-api-v3 (0.1.3-1) stable; urgency=medium

* Added support for Python 3.12

-- Salvador E. Tropea <salvador@inti.gob.ar> Wed, 08 Nov 2023 08:58:30 -0300

kicost-digikey-api-v3 (0.1.2-1) stable; urgency=medium

* Added option to exclude market place results
* Fixed problems when digi-key times out

-- Salvador E. Tropea <salvador@inti.gob.ar> Thu, 06 Oct 2022 09:43:00 -0300

kicost-digikey-api-v3 (0.1.1-1) stable; urgency=medium

* New upstream release

-- Salvador E. Tropea <salvador@inti.gob.ar> Mon, 21 Mar 2022 16:55:01 -0300

kicost-digikey-api-v3 (0.1.0-1) stable; urgency=low

* Initial release.

-- Salvador E. Tropea <stropea@inti.gob.ar> Mon, 21 Mar 2022 16:06:37 -0300
15 changes: 15 additions & 0 deletions debian/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Source: kicost-digikey-api-v3
Section: electronics
Priority: optional
Maintainer: Salvador E. Tropea <stropea@inti.gob.ar>
Build-Depends: debhelper-compat (= 12), dh-python, python3-all
Standards-Version: 4.5.0
Homepage: https://github.com/set-soft/kicost-digikey-api-v3/
X-Python3-Version: >= 3.2

Package: kicost-digikey-api-v3
Architecture: all
Multi-Arch: foreign
Depends: ${misc:Depends}, ${python3:Depends}
Description: Digi-Key API plug-in for KiCost
This package adds native support for Digi-Key to KiCost.
Loading