auphofBSF / community-digikey-api-v3-lite

A lite but full feature able wrapper for the digikey v3 api's
Apache License 2.0
1 stars 2 forks source link

Keyword search on Product #1

Open auphofBSF opened 4 years ago

auphofBSF commented 4 years ago

@lbrolliar asked in https://github.com/peeter123/digikey-api/issues/3#issuecomment-598356953 Quick question. Is there code for doing a keyword search to find the Digikey part number?

auphofBSF commented 4 years ago

A quick question, but unfortunately opens a longer explanation:

The short version

it is there but just depends on how you want to use it.

The longer Version

The lite version of the protocol wraps the swagger generated api, this can be seen in https://github.com/auphofBSF/community-digikey-api-v3-lite/blob/d77e21156dfebccd4293511cda85a90b721752d4/src/digikey_api_v3_lite/productinformation.py#L29 All the digikey api's that have been implemented in https://github.com/auphofBSF/community-digikey-api-codegen-python-clients (Currently ProductInformation and OrderSupport ) will have all supported API calls in the generated python code.

In this case the generated API is import digikey_productinformation. They can be used directly but you have to manage the API instance. Or they are wrapped in the Lite classes such as in class ProductInformation def product_details and the class def __init__() does all the management .
In the case of wanting to do a keyword search using the Lite api import digikey_api_v3_lite A new method needs to be created following the same structure as def product_details . The method name matching the api name for consistency.

the relevant Digikey REST API doc for this keyword search is https://developer.digikey.com/products/product-information/partsearch/keywordsearch

The documentation for the generated API python code can be viewed in the source once generated. If this is installed automatically it will be in the pip install locations ...site_packages... alternatively the various call and the methods documentation can be viewed by doing the following

"""
Example of using finding documentation on python Digikey API calls 
"""
import digikey_api_v3_lite  as digiAPI

# SETUP the Digikey authentication see https://developer.digikey.com/documentation/organization#production
os.environ['DIGIKEY_CLIENT_ID'] = '<DIGIKEY_APPLICATION_ID>'
os.environ['DIGIKEY_CLIENT_SECRET'] = '<DIGIKEY_SECRET>'
os.environ['DIGIKEY_STORAGE_PATH'] = r'<PATH_TO_THE_TOKEN_CACHING_LOCATION>'

product_information_object = digiAPI.ProductInformation()
digikey_api_ProdInfo = product_information_object._api_instance
# list all methods of the generated_api that 
print([method for method in dir(digikey_api_ProdInfo ) if '__' not in method])
# Should print the following 
#['api_client', 'digi_reel_pricing', 'digi_reel_pricing_with_http_info', 'keyword_search', 'keyword_search_with_http_info', 'product_details', 'product_details_with_http_info', 'suggested_parts', 'suggested_parts_with_http_info']

# choose a particular method ie "keyword_search" and retrieve the generated doc's
print(digikey_api_ProdInfo.keyword_search.__doc__)

I am taking the time to describe this as further implementations work needs to be done depending on specific use cases. I have just done some work recently on another API and have got myself more familiar with python decorators. I believe this Lite API can be a set of Decorators to automatically wrap the generated API's. This will reduce the amount of individual code to write and enable rapid integration of all the Digikey API's . I just notice there are now additional API's available [BOM, Taxonomy Search] Anyone wanting to generate a Decorator that automatically wraps the https://github.com/auphofBSF/community-digikey-api-codegen-python-clients is more than welcome to submit a PR. Alternatively I will also accept PR for just extending the Lite API class as per def product_details shown above.

Additionally python generated API doc set should be automatically generated by something like sphinx and be there to complement the Digikey JSON API documentation. I have raised this as an enhancement https://github.com/auphofBSF/community-digikey-api-codegen-python-clients/issues/1

I am unable to do much more in the next week but may be able to look into some of this in the coming weeks.