-
Notifications
You must be signed in to change notification settings - Fork 44
updated to digikey v4 and python 3.12 #51
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
Open
hurricaneJoef
wants to merge
2
commits into
peeter123:master
Choose a base branch
from
hurricaneJoef:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
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 (keyword_search, product_details, digi_reel_pricing, suggested_parts, | ||
) | ||
from digikey.v4.api import (status_salesorder_id, salesorder_history) | ||
from digikey.v4.api import (batch_product_details) | ||
|
||
name = 'digikey' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import os | ||
import logging | ||
from distutils.util import strtobool | ||
import digikey.oauth.oauth2 | ||
from digikey.exceptions import DigikeyError | ||
from digikey.v4.productinformation import (KeywordRequest, KeywordResponse, ProductDetails, DigiReelPricing, | ||
) | ||
from digikey.v4.productinformation.rest import ApiException | ||
from digikey.v4.ordersupport import (OrderStatusResponse, SalesOrderHistoryItem) | ||
from digikey.v4.batchproductdetails import (BatchProductDetailsRequest, BatchProductDetailsResponse) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class DigikeyApiWrapper(object): | ||
def __init__(self, wrapped_function, module): | ||
self.sandbox = False | ||
|
||
apinames = { | ||
digikey.v4.productinformation: 'products', | ||
digikey.v4.ordersupport: 'OrderDetails', | ||
digikey.v4.batchproductdetails: 'BatchSearch' | ||
} | ||
|
||
apiclasses = { | ||
digikey.v4.productinformation: digikey.v4.productinformation.ProductSearchApi, | ||
digikey.v4.ordersupport: digikey.v4.ordersupport.OrderDetailsApi, | ||
digikey.v4.batchproductdetails: digikey.v4.batchproductdetails.BatchSearchApi | ||
} | ||
|
||
apiname = apinames[module] | ||
apiclass = apiclasses[module] | ||
|
||
# Configure API key authorization: apiKeySecurity | ||
configuration = module.Configuration() | ||
configuration.api_key['X-DIGIKEY-Client-Id'] = os.getenv('DIGIKEY_CLIENT_ID') | ||
|
||
# Return quietly if no clientid has been set to prevent errors when importing the module | ||
if os.getenv('DIGIKEY_CLIENT_ID') is None or os.getenv('DIGIKEY_CLIENT_SECRET') is None: | ||
raise DigikeyError('Please provide a valid DIGIKEY_CLIENT_ID and DIGIKEY_CLIENT_SECRET in your env setup') | ||
|
||
# Use normal API by default, if DIGIKEY_CLIENT_SANDBOX is True use sandbox API | ||
configuration.host = 'https://api.digikey.com/' + apiname + '/v4' | ||
try: | ||
if bool(strtobool(os.getenv('DIGIKEY_CLIENT_SANDBOX'))): | ||
configuration.host = 'https://sandbox-api.digikey.com/' + apiname + '/v4' | ||
self.sandbox = True | ||
except (ValueError, AttributeError): | ||
pass | ||
|
||
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed | ||
# configuration.api_key_prefix['X-DIGIKEY-Client-Id'] = 'Bearer' | ||
|
||
# Configure OAuth2 access token for authorization: oauth2AccessCodeSecurity | ||
self._digikeyApiToken = digikey.oauth.oauth2.TokenHandler(version=3, sandbox=self.sandbox).get_access_token() | ||
configuration.access_token = self._digikeyApiToken.access_token | ||
|
||
# create an instance of the API class | ||
self._api_instance = apiclass(module.ApiClient(configuration)) | ||
|
||
# Populate reused ids | ||
self.authorization = self._digikeyApiToken.get_authorization() | ||
self.x_digikey_client_id = os.getenv('DIGIKEY_CLIENT_ID') | ||
|
||
self.wrapped_function = wrapped_function | ||
|
||
@staticmethod | ||
def _remaining_requests(header, api_limits): | ||
try: | ||
rate_limit = header['X-RateLimit-Limit'] | ||
rate_limit_rem = header['X-RateLimit-Remaining'] | ||
|
||
if api_limits is not None and type(api_limits) == dict: | ||
api_limits['api_requests_limit'] = int(rate_limit) | ||
api_limits['api_requests_remaining'] = int(rate_limit_rem) | ||
|
||
logger.debug('Requests remaining: [{}/{}]'.format(rate_limit_rem, rate_limit)) | ||
except (KeyError, ValueError) as e: | ||
logger.debug(f'No api limits returned -> {e.__class__.__name__}: {e}') | ||
if api_limits is not None and type(api_limits) == dict: | ||
api_limits['api_requests_limit'] = None | ||
api_limits['api_requests_remaining'] = None | ||
|
||
@staticmethod | ||
def _store_api_statuscode(statuscode, status): | ||
if status is not None and type(status) == dict: | ||
status['code'] = int(statuscode) | ||
|
||
logger.debug('API returned code: {}'.format(statuscode)) | ||
|
||
def call_api_function(self, *args, **kwargs): | ||
try: | ||
# If optional api_limits, status mutable object is passed use it to store API limits and status code | ||
api_limits = kwargs.pop('api_limits', None) | ||
status = kwargs.pop('status', None) | ||
|
||
func = getattr(self._api_instance, self.wrapped_function) | ||
logger.debug(f'CALL wrapped -> {func.__qualname__}') | ||
api_response = func(*args, self.x_digikey_client_id, authorization = self.authorization, **kwargs) | ||
self._remaining_requests(api_response[2], api_limits) | ||
self._store_api_statuscode(api_response[1], status) | ||
|
||
return api_response[0] | ||
except ApiException as e: | ||
logger.error(f'Exception when calling {self.wrapped_function}: {e}') | ||
self._store_api_statuscode(e.status, status) | ||
|
||
|
||
def keyword_search(*args, **kwargs) -> KeywordResponse: | ||
client = DigikeyApiWrapper('keyword_search_with_http_info', digikey.v4.productinformation) | ||
|
||
if 'body' in kwargs and type(kwargs['body']) == KeywordRequest: | ||
logger.info(f'Search for: {kwargs["body"].keywords}') | ||
logger.debug('CALL -> keyword_search') | ||
return client.call_api_function(*args, **kwargs) | ||
else: | ||
raise DigikeyError('Please provide a valid KeywordSearchRequest argument') | ||
|
||
|
||
def product_details(*args, **kwargs) -> ProductDetails: | ||
client = DigikeyApiWrapper('product_details_with_http_info', digikey.v4.productinformation) | ||
|
||
if len(args): | ||
logger.info(f'Get product details for: {args[0]}') | ||
return client.call_api_function(*args, **kwargs) | ||
|
||
|
||
def digi_reel_pricing(*args, **kwargs) -> DigiReelPricing: | ||
client = DigikeyApiWrapper('digi_reel_pricing_with_http_info', digikey.v4.productinformation) | ||
|
||
if len(args): | ||
logger.info(f'Calculate the DigiReel pricing for {args[0]} with quantity {args[1]}') | ||
return client.call_api_function(*args, **kwargs) | ||
|
||
|
||
def suggested_parts(*args, **kwargs) -> ProductDetails: | ||
client = DigikeyApiWrapper('suggested_parts_with_http_info', digikey.v4.productinformation) | ||
|
||
if len(args): | ||
logger.info(f'Retrieve detailed product information and two suggested products for: {args[0]}') | ||
return client.call_api_function(*args, **kwargs) | ||
|
||
|
||
def status_salesorder_id(*args, **kwargs) -> OrderStatusResponse: | ||
client = DigikeyApiWrapper('order_status_with_http_info', digikey.v4.ordersupport) | ||
|
||
if len(args): | ||
logger.info(f'Get order details for: {args[0]}') | ||
return client.call_api_function(*args, **kwargs) | ||
|
||
|
||
def salesorder_history(*args, **kwargs) -> [SalesOrderHistoryItem]: | ||
client = DigikeyApiWrapper('order_history_with_http_info', digikey.v4.ordersupport) | ||
|
||
if 'start_date' in kwargs and type(kwargs['start_date']) == str \ | ||
and 'end_date' in kwargs and type(kwargs['end_date']) == str: | ||
logger.info(f'Searching for orders in date range ' + kwargs['start_date'] + ' to ' + kwargs['end_date']) | ||
return client.call_api_function(*args, **kwargs) | ||
else: | ||
raise DigikeyError('Please provide valid start_date and end_date strings') | ||
|
||
|
||
def batch_product_details(*args, **kwargs) -> BatchProductDetailsResponse: | ||
client = DigikeyApiWrapper('batch_product_details_with_http_info', digikey.v4.batchproductdetails) | ||
|
||
if 'body' in kwargs and type(kwargs['body']) == BatchProductDetailsRequest: | ||
logger.info(f'Batch product search: {kwargs["body"].products}') | ||
logger.debug('CALL -> batch_product_details') | ||
return client.call_api_function(*args, **kwargs) | ||
else: | ||
raise DigikeyError('Please provide a valid BatchProductDetailsRequest argument') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# coding: utf-8 | ||
|
||
# flake8: noqa | ||
|
||
""" | ||
Batch Product Details Api | ||
|
||
Retrieve list of product details from list of part numbers # noqa: E501 | ||
|
||
OpenAPI spec version: v3 | ||
|
||
Generated by: https://github.com/swagger-api/swagger-codegen.git | ||
""" | ||
|
||
|
||
from __future__ import absolute_import | ||
|
||
# import apis into sdk package | ||
from digikey.v3.batchproductdetails.api.batch_search_api import BatchSearchApi | ||
|
||
# import ApiClient | ||
from digikey.v3.batchproductdetails.api_client import ApiClient | ||
from digikey.v3.batchproductdetails.configuration import Configuration | ||
# import models into sdk package | ||
from digikey.v3.batchproductdetails.models.api_error_response import ApiErrorResponse | ||
from digikey.v3.batchproductdetails.models.api_validation_error import ApiValidationError | ||
from digikey.v3.batchproductdetails.models.associated_product import AssociatedProduct | ||
from digikey.v3.batchproductdetails.models.basic_product import BasicProduct | ||
from digikey.v3.batchproductdetails.models.batch_product_details_request import BatchProductDetailsRequest | ||
from digikey.v3.batchproductdetails.models.batch_product_details_response import BatchProductDetailsResponse | ||
from digikey.v3.batchproductdetails.models.iso_search_locale import IsoSearchLocale | ||
from digikey.v3.batchproductdetails.models.kit_part import KitPart | ||
from digikey.v3.batchproductdetails.models.limited_taxonomy import LimitedTaxonomy | ||
from digikey.v3.batchproductdetails.models.media_links import MediaLinks | ||
from digikey.v3.batchproductdetails.models.pid_vid import PidVid | ||
from digikey.v3.batchproductdetails.models.price_break import PriceBreak | ||
from digikey.v3.batchproductdetails.models.product_details import ProductDetails |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from __future__ import absolute_import | ||
|
||
# flake8: noqa | ||
|
||
# import apis into api package | ||
from digikey.v3.batchproductdetails.api.batch_search_api import BatchSearchApi |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Distutils is gone in python 3.12: https://docs.python.org/3/library/distutils.html, I'm not sure how this is working for you.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To get this working with 3.12 I just used:
`
def strtobool(val):
"""Convert a string representation of truth to true (1) or false (0).
`
The function is only called once in the library so there might be a more eloquent solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
distutils.strtobool
still exsits in Python 3.13.3. It's part of setuptools. hurricaneJoef#2 adds the missing dependency.