Skip to content
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions mouser/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class MouserPartSearchRequest(MouserBaseRequest):
'partnumber': ('POST', '/search/partnumber'),
'partnumberandmanufacturer': ('', ''),
'manufacturerlist': ('', ''),
'ibn': ('GET', '/search/getIBNs?ibnCode='),
}

def get_clean_response(self):
Expand Down Expand Up @@ -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 {}
24 changes: 18 additions & 6 deletions mouser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 '''
Expand All @@ -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:
Expand All @@ -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 '''

Expand Down
18 changes: 18 additions & 0 deletions mouser/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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':
Expand Down
3 changes: 2 additions & 1 deletion run_tests.py
Original file line number Diff line number Diff line change
@@ -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()
17 changes: 17 additions & 0 deletions tests/test_mouser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()