diff --git a/README.md b/README.md index 2bed79c..1de6ad4 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,11 @@ Run `mouser --help` for more information about the usage. mouser search partnumber --number XXX ``` +#### IBN Search +```bash +mouser search ibn --number XXX +``` + #### Export order to CSV ``` bash mouser order get --number XXX --export diff --git a/mouser/api.py b/mouser/api.py index 06f0bc8..cd955d2 100644 --- a/mouser/api.py +++ b/mouser/api.py @@ -93,6 +93,7 @@ class MouserPartSearchRequest(MouserBaseRequest): 'partnumber': ('POST', '/search/partnumber'), 'partnumberandmanufacturer': ('', ''), 'manufacturerlist': ('', ''), + 'ibn': ('GET', '/search/getIBNs?ibnCode='), } def get_clean_response(self): @@ -207,3 +208,27 @@ def keyword_search(self, keyword, record_limit=0, option='None'): return self.run(self.body) else: return False + + def ibn_search(self, ibn_code): + '''Mouser IBN Search''' + + if not ibn_code: + return False + + self.operation = 'ibn' + self._set_base_url_for_operation() + self.method, url_fragment = self.operations[self.operation] + self.body = {} + self.api_url = self.base_url + url_fragment + str(ibn_code) + self.url = self.api_url + + return self.run() + + def get_ibn_data(self): + response_data = self.get_response() + if isinstance(response_data, dict): + ibn_list = response_data.get('IBN', []) + if isinstance(ibn_list, list) and ibn_list: + return ibn_list[0] + + return {} diff --git a/mouser/base.py b/mouser/base.py index b1814df..18c5763 100644 --- a/mouser/base.py +++ b/mouser/base.py @@ -6,6 +6,7 @@ # Mouser Base URL BASE_URL = 'https://api.mouser.com/api/v1.0' +MOBILE_URL = 'https://api.mouser.com/api/mobile/v2' def get_api_keys(filename=None): @@ -49,23 +50,28 @@ class MouserAPIRequest: def __init__(self, url, method, file_keys=None, *args): if not url or not method: return None - self.api_url = BASE_URL + url + self.api_url = getattr(self, 'base_url', BASE_URL) + url self.method = method # Append argument if len(args) == 1: - self.api_url += '/' + str(args[0]) + self.api_url += str(args[0]) # Append API Key - if self.name == 'Part Search': + if hasattr(self, 'name') and self.name == 'Part Search' and getattr(self, 'operation', None) == 'ibn': + self.api_key = None + elif hasattr(self, 'name') and self.name == 'Part Search': self.api_key = get_api_keys(file_keys)[1] else: self.api_key = get_api_keys(file_keys)[0] - if self.api_key: - self.url = self.api_url + '?apiKey=' + self.api_key + if self.api_key is not None: + if '?' in self.api_url: + self.url = self.api_url + '&apiKey=' + self.api_key + else: + self.url = self.api_url + '?apiKey=' + self.api_key else: - raise FileNotFoundError('API Keys Are Missing') + self.url = self.api_url def get(self, url): response = requests.get(url=url) @@ -106,6 +112,7 @@ class MouserBaseRequest(MouserAPIRequest): allowed_methods = ['GET', 'POST'] operation = None operations = {} + base_url = BASE_URL def __init__(self, operation, file_keys=None, *args): ''' Init ''' @@ -122,6 +129,7 @@ def __init__(self, operation, file_keys=None, *args): return self.operation = operation + self._set_base_url_for_operation() (method, url) = self.operations.get(self.operation, ('', '')) if not url or not method or method not in self.allowed_methods: @@ -130,6 +138,10 @@ def __init__(self, operation, file_keys=None, *args): super().__init__(url, method, file_keys, *args) + def _set_base_url_for_operation(self): + if self.operation == 'ibn': + self.base_url = MOBILE_URL + def export_csv(self, file_path: str, data: dict): ''' Export dictionary data to CSV ''' diff --git a/mouser/cli.py b/mouser/cli.py index 28a9865..83de82a 100644 --- a/mouser/cli.py +++ b/mouser/cli.py @@ -22,6 +22,13 @@ def mouser_cli(request_type, operation, number, keyword, record_limit, option, export): ''' Main CLI entry point ''' + # Determine operation and code based on request_type + if request_type == 'ibn': + number = operation + operation = 'getIBNs' + else: + operation = operation + args = [] # Create request @@ -47,6 +54,17 @@ def mouser_cli(request_type, operation, number, keyword, record_limit, option, e request = MouserPartSearchRequest(operation, API_KEYS_FILE, *args) if request.url: + if operation == 'ibn': + if not number: + print('[ERROR]\tMissing IBN Code') + else: + search = request.ibn_search(number) + if search: + print('[LINK]\t' + request.url) + print('[DATA]') + print(json.dumps(request.get_response(), indent=4, sort_keys=True)) + return + print(f'[LINK]\t{request.api_url}') if operation == 'partnumber': diff --git a/run_tests.py b/run_tests.py index 343dbd3..2e2110d 100644 --- a/run_tests.py +++ b/run_tests.py @@ -1,7 +1,8 @@ -from tests.test_mouser import test_version, test_search_partnumber, test_search_keyword +from tests.test_mouser import test_version, test_search_partnumber, test_search_keyword, test_search_ibn if __name__ == '__main__': test_version() test_search_partnumber() test_search_keyword() + test_search_ibn() diff --git a/tests/test_mouser.py b/tests/test_mouser.py index 6399bfb..ac4d0b9 100644 --- a/tests/test_mouser.py +++ b/tests/test_mouser.py @@ -35,7 +35,24 @@ def test_search_keyword(file_keys='mouser_api_keys.yaml'): assert part['ManufacturerPartNumber'] == 'DMP2066LSN-7' assert part['MouserPartNumber'] == '621-DMP2066LSN-7' + +def test_search_ibn(): + + ibn_code = '60W0DO' + + request = MouserPartSearchRequest('ibn') + assert request.api_url == 'https://api.mouser.com/api/mobile/v2/search/getIBNs?ibnCode=' + + success = request.ibn_search(ibn_code) + assert success is True + + ibn_data = request.get_ibn_data() + assert ibn_data.get('IBNCode') == ibn_code + assert 'MouserPartNumber' in ibn_data + + if __name__ == '__main__': test_version() test_search_partnumber() test_search_keyword() + test_search_ibn()