diff --git a/README.md b/README.md index aae362b..388ea19 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,26 @@ -![TechSpecs Logo](https://i.imgur.com/JwSpZO8.jpg) -![TechSpecs Logo](https://i.imgur.com/JZ3GqAU.jpg) +

+ TechSpecs Logo +

-# Introducing TechSpecs Python +# Introduction -This python library provides automatic access to the standardized technical specifications of the world's consumer electronics, including the latest smartphones, tablets, smartwatches, laptops, and more. +TechSpecs Python provides easy access to the standardized technical specifications of the world's consumer electronics, including the latest smartphones, tablets, smartwatches, laptops, monitors, TVs and more. -## API Key +# Database Stats + +At TechSpecs, we are committed to providing the most comprehensive and up-to-date database of technical specifications for consumer electronics products. Here are some statistics on our current database: + +Total number of products: 100,000+ +- Brands: 400+ +- Categories: 6 (Smartphones, Tablets, Smartwatches, Monitors, TVs, and Laptops) +- Technical specifications: Over 300 for each product, including dimensions, weight, display features, connectivity options, and more. +- Daily updates: We update our database on a daily basis to ensure that our API provides the most accurate and up-to-date technical specifications for the products you're interested in. -- Get an API key [here](https://developer.dashboard.techspecs.io/) +We take pride in providing a database that is comprehensive and reliable, and we are constantly working to expand and improve it. If you have any questions or issues, please don't hesitate to reach out to our support team for assistance. +## API Key + +- [Signup](https://techspecs.io/) to get your TechSpecs API Key ## Requirements @@ -16,7 +28,6 @@ This python library provides automatic access to the standardized technical spec ## Installation -![How to install TechSpecs](https://i.imgur.com/m9yxeWW.png) ```sh pip install techspecs @@ -24,150 +35,267 @@ pip install techspecs ## Usage -The library needs to be configured with your account's api key and base which is -available in your [TechSpecs Dashboard](https://developer.dashboard.techspecs.io/). +The library needs to be configured with your TechSpecs api key and base URL which is +available in your [TechSpecs Dashboard](https://techspecs.io/dashboard). -Set `techspecs_key` to your key value and `techspecs_base` to your base value. +Set `techspecs_api_key` to your key value and `techspecs_base_url` to your base value. -### Basic Search -#### Search for a device by specifying it's model name, version number or features +### Product Search ```python # Search for a product by name, version or features import techspecs import json -# TechSpecs API Key -techspecs_key = "techspecs_api_key" +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" -# TechSpecs base https://apis.dashboard.techspecs.io/{techspecs_base} -techspecs_base = 'a8TD3mkN49fhg2y' +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" query = { 'keyword': 'iPhone 13', # product name or version number to search - 'category': 'all', # product category to search + 'category': '', # product category to search (e.g. 'Smartphones', 'Tablets', or leave empty to search all categories) + 'page': 0 # page number to fetch results from } -# choose between "pretty" or "raw" mode for viewing response -response = techspecs.search(base, query, key, mode='pretty') +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Search for a product by name, version, or features +try: + response = techspecs.product_search(techspecs_base_url, query, techspecs_api_key, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") -# print the search results -print(response) ``` -### Advanced Search -#### List all products by brand, category and release date + +### Product Details + ```python -# List all products by brand, category and release date +# Get the detailed specifications of a product import techspecs -# TechSpecs API Key -techspecs_key = "techspecs_api_key" +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" -# TechSpecs base https://apis.dashboard.techspecs.io/{techspecs_base} -techspecs_base = "a8TD3mkN49fhg2y" +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" -# enter the page number to fetch results from -page = 1 +# TechSpecs product ID +techspecs_product_id = "63e96260ff7af4b68a304e40" -# type in the name of the brand you're looking for or leave this field empty to see results from all brands -brand = ["Apple"] +# Query dictionary +query = { + "productId": techspecs_product_id +} -# type in the name of the category you're looking for or leave this field empty to see results from all categories -category = ["smartphone"] +# Output mode ('raw' or 'pretty') +mode = 'pretty' -# please provide a date range to narrow your search. Leave this field empty to fetch all results from all dates -date = { - "from": "2010-01-01", # YYYY-MM-DD - "to": "2022-03-15" # YYYY-MM-DD -} +try: + # Validate techspecs_product_id + if not isinstance(techspecs_product_id, str): + raise ValueError('TechSpecs Product ID should be a string.') + + # Validate mode + if mode not in ['raw', 'pretty']: + raise ValueError('Invalid mode. Mode should be "raw" or "pretty".') + + # Call TechSpecs API to get product details + response = techspecs.product_detail(techspecs_base_url, techspecs_product_id, techspecs_api_key, mode=mode) + + # Print the product details + print(response) +except Exception as e: + print(f"An error occurred: {e}") -# choose between "pretty" or "raw" mode for viewing response -response = techspecs.products(techspecs_base, brand, category, date, page, techspecs_key, mode='pretty') -# print the search results -print(response) ``` -### Apple Machine ID Search -#### Search for Apple products by machine id + +### List all categories ```python -# Search for an Apple product by machine id import techspecs -# TechSpecs API Key -techspecs_key = "techspecs_api_key" +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" -# TechSpecs base https://apis.dashboard.techspecs.io/{techspecs_base} -techspecs_base = "a8TD3mkN49fhg2y" +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" -# machine id to search -machine_id = "iphone8,3" +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Define constants +DEFAULT_MODE = 'pretty' +VALID_MODES = ['pretty', 'raw'] + +# Validate parameters +def validate_parameters(mode=DEFAULT_MODE): + if mode not in VALID_MODES: + raise ValueError(f'Invalid mode: {mode}. Mode should be one of {VALID_MODES}.') + +# Validate search parameters +try: + validate_parameters(mode=mode) +except ValueError as e: + print(f"Invalid search parameters: {e}") + exit() + +# Call TechSpecs API to get all categories +try: + response = techspecs.get_all_categories(techspecs_base_url, techspecs_api_key, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") -# choose between "pretty" or "raw" mode for viewing response -response = techspecs.apple_machine_id(techspecs_base, machine_id, techspecs_key, mode='pretty') -# print the specifications of the product -print(response) ``` -### Product Details +### List all brands ```python -# Get the standardized specifications of a specified product import techspecs -# TechSpecs API Key -techspecs_key = "techspecs_api_key" +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" -# TechSpecs base https://apis.dashboard.techspecs.io/{techspecs_base} -techspecs_base = "a8TD3mkN49fhg2y" +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" -# TechSpecs product id -techspecs_id = "6186b047987cda5f88311983" +# Output mode ('raw' or 'pretty') +mode = 'pretty' -# choose between "pretty" or "raw" mode for viewing response -response = techspecs.detail(techspecs_base, techspecs_id, techspecs_key, mode='pretty') +# Define function to validate parameters +def validate_parameters(mode): + if mode not in ['raw', 'pretty']: + raise ValueError(f'Invalid mode: {mode}. Mode should be one of ["raw", "pretty"].') + +# Validate parameters +try: + validate_parameters(mode) +except ValueError as e: + print(f"Invalid parameters: {e}") + exit() + +# Call TechSpecs API to get all brands +try: + response = techspecs.get_all_brands(techspecs_base_url, techspecs_api_key, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") -# print the specifications of the product -print(response) ``` -### List all brands + +### Advanced Search ```python +# List all products by brand, category and release date import techspecs +import datetime + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Set constants +DEFAULT_PAGE = 0 +DEFAULT_MODE = 'pretty' +VALID_MODES = ['pretty', 'raw'] + +# Define function to validate date format +def is_valid_date(date_str): + try: + datetime.datetime.strptime(date_str, '%Y-%m-%d') + return True + except ValueError: + return False + +# Define function to validate parameters +def validate_parameters(brand, category, date=None, page=DEFAULT_PAGE, mode=DEFAULT_MODE): + if not isinstance(brand, list): + raise ValueError('Brand should be a list.') + + if not isinstance(category, list): + raise ValueError('Category should be a list.') + + if date is not None: + if not isinstance(date, dict): + raise ValueError('Invalid date format. Date should be a dictionary with keys "from" and "to".') + elif 'from' not in date or 'to' not in date: + raise ValueError('Invalid date format. Date dictionary should have keys "from" and "to".') + elif not is_valid_date(date['from']) or not is_valid_date(date['to']): + raise ValueError('Invalid date format. Date should be in the format YYYY-MM-DD.') + + if not isinstance(page, int) or page < 0: + raise ValueError('Page should be a non-negative integer.') + + if mode not in VALID_MODES: + raise ValueError(f'Invalid mode: {mode}. Mode should be one of {VALID_MODES}.') + +# Define search parameters +brand = ["Apple"] +category = ["Smartphones"] +date = { + "from": "2010-01-01", + "to": "2022-03-15" +} +page = 0 -# TechSpecs API Key -techspecs_key = "techspecs_api_key" - -# TechSpecs base https://apis.dashboard.techspecs.io/{techspecs_base} -techspecs_base = "a8TD3mkN49fhg2y" +# Validate search parameters +try: + validate_parameters(brand, category, date=date, page=page, mode=mode) +except ValueError as e: + print(f"Invalid search parameters: {e}") + exit() -# choose between "pretty" or "raw" mode for viewing response -response = techspecs.brands(techspecs_base, techspecs_key, mode='pretty') -# print the list of all brands -print(response) +# Call TechSpecs API to get all products +try: + response = techspecs.get_all_products(techspecs_base_url, techspecs_api_key, brand, category, date, page, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") ``` -### List all categories + + +### Machine ID Search +#### Search for Apple products by machine id ```python +# Search for an Apple product by machine id import techspecs -# TechSpecs API Key -techspecs_key = "techspecs_api_key" +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +# Serial number of the Apple machine to look up machineid_or_codename ex iphone 11,6 +machine_id = "iphone 11,6" -# TechSpecs base https://apis.dashboard.techspecs.io/{techspecs_base} -techspecs_base = "a8TD3mkN49fhg2y" +# Output mode ('raw' or 'pretty') +mode = 'pretty' -# choose between "pretty" or "raw" mode for viewing response -response = techspecs.categories(techspecs_base, techspecs_key, mode='pretty') +# Look up the Apple machine by its serial number +try: + response = techspecs.machine_id_search(techspecs_base_url, techspecs_api_key, machine_id, mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") -# print the list of all categories -print(response) ``` diff --git a/examples/advanced_search.py b/examples/advanced_search.py index 17f7d32..4994b87 100644 --- a/examples/advanced_search.py +++ b/examples/advanced_search.py @@ -1,29 +1,70 @@ -# List all products by brand, category and release date import techspecs +import datetime -# TechSpecs API Key -techspecs_key = "techspecs_api_key" +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" -# TechSpecs base https://apis.dashboard.techspecs.io/{techspecs_base} -techspecs_base = "a8TD3mkN49fhg2y" +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" -# enter the page number to fetch results from -page = 1 +# Output mode ('raw' or 'pretty') +mode = 'pretty' -# type in the name of the brand you're looking for or leave this field empty to see results from all brands -brand = ["Apple"] +# Set constants +DEFAULT_PAGE = 0 +DEFAULT_MODE = 'pretty' +VALID_MODES = ['pretty', 'raw'] -# type in the name of the category you're looking for or leave this field empty to see results from all categories -category = ["smartphone"] +# Define function to validate date format +def is_valid_date(date_str): + try: + datetime.datetime.strptime(date_str, '%Y-%m-%d') + return True + except ValueError: + return False -# please provide a date range to narrow your search. Leave this field empty to fetch all results from all dates -date = { - "from": "2010-01-01", # YYYY-MM-DD - "to": "2022-03-15" # YYYY-MM-DD +# Define function to validate parameters +def validate_parameters(brand, category, date=None, page=DEFAULT_PAGE, mode=DEFAULT_MODE): + if not isinstance(brand, list): + raise ValueError('Brand should be a list.') + + if not isinstance(category, list): + raise ValueError('Category should be a list.') + + if date is not None: + if not isinstance(date, dict): + raise ValueError('Invalid date format. Date should be a dictionary with keys "from" and "to".') + elif 'from' not in date or 'to' not in date: + raise ValueError('Invalid date format. Date dictionary should have keys "from" and "to".') + elif not is_valid_date(date['from']) or not is_valid_date(date['to']): + raise ValueError('Invalid date format. Date should be in the format YYYY-MM-DD.') + + if not isinstance(page, int) or page < 0: + raise ValueError('Page should be a non-negative integer.') + + if mode not in VALID_MODES: + raise ValueError(f'Invalid mode: {mode}. Mode should be one of {VALID_MODES}.') + +# Define search parameters +brand = ["Apple"] +category = ["Smartphones"] +date = { + "from": "2010-01-01", + "to": "2022-03-15" } +page = 0 + +# Validate search parameters +try: + validate_parameters(brand, category, date=date, page=page, mode=mode) +except ValueError as e: + print(f"Invalid search parameters: {e}") + exit() -# choose between "pretty" or "raw" mode for viewing response -response = techspecs.products(techspecs_base, brand, category, date, page, techspecs_key, mode='pretty') -# print the search results -print(response) +# Call TechSpecs API to get all products +try: + response = techspecs.get_all_products(techspecs_base_url, techspecs_api_key, brand, category, date, page, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") diff --git a/examples/all_brands.py b/examples/all_brands.py deleted file mode 100644 index c77346e..0000000 --- a/examples/all_brands.py +++ /dev/null @@ -1,13 +0,0 @@ -import techspecs - -# TechSpecs API Key -techspecs_key = "techspecs_api_key" - -# TechSpecs base https://apis.dashboard.techspecs.io/{techspecs_base} -techspecs_base = "a8TD3mkN49fhg2y" - -# choose between "pretty" or "raw" mode for viewing response -response = techspecs.brands(techspecs_base, techspecs_key, mode='pretty') - -# print the list of all brands -print(response) diff --git a/examples/all_categories.py b/examples/all_categories.py deleted file mode 100644 index 18ef2e1..0000000 --- a/examples/all_categories.py +++ /dev/null @@ -1,13 +0,0 @@ -import techspecs - -# TechSpecs API Key -techspecs_key = "techspecs_api_key" - -# TechSpecs base https://apis.dashboard.techspecs.io/{techspecs_base} -techspecs_base = "a8TD3mkN49fhg2y" - -# choose between "pretty" or "raw" mode for viewing response -response = techspecs.categories(techspecs_base, techspecs_key, mode='pretty') - -# print the list of all categories -print(response) diff --git a/examples/basic_search.py b/examples/basic_search.py deleted file mode 100644 index b95ba53..0000000 --- a/examples/basic_search.py +++ /dev/null @@ -1,20 +0,0 @@ -# Search for a product by name, version or features -import techspecs -import json - -# TechSpecs API Key -techspecs_key = "techspecs_api_key" - -# TechSpecs base https://apis.dashboard.techspecs.io/{techspecs_base} -techspecs_base = 'a8TD3mkN49fhg2y' - -query = { - 'keyword': 'iPhone 13', # product name or version number to search - 'category': 'all', # product category to search -} - -# choose between "pretty" or "raw" mode for viewing response -response = techspecs.search(base, query, key, mode='pretty') - -# print the search results -print(response) diff --git a/examples/get_all_brands.py b/examples/get_all_brands.py new file mode 100644 index 0000000..f335b77 --- /dev/null +++ b/examples/get_all_brands.py @@ -0,0 +1,29 @@ +import techspecs + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Define function to validate parameters +def validate_parameters(mode): + if mode not in ['raw', 'pretty']: + raise ValueError(f'Invalid mode: {mode}. Mode should be one of ["raw", "pretty"].') + +# Validate parameters +try: + validate_parameters(mode) +except ValueError as e: + print(f"Invalid parameters: {e}") + exit() + +# Call TechSpecs API to get all brands +try: + response = techspecs.get_all_brands(techspecs_base_url, techspecs_api_key, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") diff --git a/examples/get_all_categories.py b/examples/get_all_categories.py new file mode 100644 index 0000000..940c5f2 --- /dev/null +++ b/examples/get_all_categories.py @@ -0,0 +1,33 @@ +import techspecs + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Define constants +DEFAULT_MODE = 'pretty' +VALID_MODES = ['pretty', 'raw'] + +# Validate parameters +def validate_parameters(mode=DEFAULT_MODE): + if mode not in VALID_MODES: + raise ValueError(f'Invalid mode: {mode}. Mode should be one of {VALID_MODES}.') + +# Validate search parameters +try: + validate_parameters(mode=mode) +except ValueError as e: + print(f"Invalid search parameters: {e}") + exit() + +# Call TechSpecs API to get all categories +try: + response = techspecs.get_all_categories(techspecs_base_url, techspecs_api_key, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") diff --git a/examples/machineid_search.py b/examples/machineid_search.py new file mode 100644 index 0000000..31a572b --- /dev/null +++ b/examples/machineid_search.py @@ -0,0 +1,20 @@ +import techspecs + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +# Serial number of the Apple machine to look up machineid_or_codename ex iphone 11,6 +machine_id = "iphone 11,6" + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Look up the Apple machine by its serial number +try: + response = techspecs.machine_id_search(techspecs_base_url, techspecs_api_key, machine_id, mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") diff --git a/examples/product_detail.py b/examples/product_detail.py index e9b5629..1e82814 100644 --- a/examples/product_detail.py +++ b/examples/product_detail.py @@ -1,17 +1,35 @@ -# Get the standardized specifications of a specified product import techspecs -# TechSpecs API Key -techspecs_key = "techspecs_api_key" +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" -# TechSpecs base https://apis.dashboard.techspecs.io/{techspecs_base} -techspecs_base = "a8TD3mkN49fhg2y" +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" -# TechSpecs product id -techspecs_id = "6186b047987cda5f88311983" +# TechSpecs product ID +techspecs_product_id = "63e96260ff7af4b68a304e40" -# choose between "pretty" or "raw" mode for viewing response -response = techspecs.detail(techspecs_base, techspecs_id, techspecs_key, mode='pretty') +# Query dictionary +query = { + "productId": techspecs_product_id +} -# print the specifications of the product -print(response) +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +try: + # Validate techspecs_product_id + if not isinstance(techspecs_product_id, str): + raise ValueError('TechSpecs Product ID should be a string.') + + # Validate mode + if mode not in ['raw', 'pretty']: + raise ValueError('Invalid mode. Mode should be "raw" or "pretty".') + + # Call TechSpecs API to get product details + response = techspecs.product_detail(techspecs_base_url, techspecs_product_id, techspecs_api_key, mode=mode) + + # Print the product details + print(response) +except Exception as e: + print(f"An error occurred: {e}") diff --git a/examples/product_search.py b/examples/product_search.py new file mode 100644 index 0000000..5d5480c --- /dev/null +++ b/examples/product_search.py @@ -0,0 +1,24 @@ +import techspecs +import json + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +query = { + 'keyword': 'iPhone 13', # product name or version number to search + 'category': '', # product category to search (e.g. 'Smartphones', 'Tablets', or leave empty to search all categories) + 'page': 0 # page number to fetch results from +} + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Search for a product by name, version, or features +try: + response = techspecs.product_search(techspecs_base_url, query, techspecs_api_key, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") diff --git a/setup.cfg b/setup.cfg index 912f9ad..9cdc9c8 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,9 +1,9 @@ [metadata] name = TechSpecs -version = 1.0.0 +version = 0.0.0 author = TechSpecs author_email = support@techspecs.io -description = TechSpecs library for python +description = TechSpecs Python long_description = file: README.md long_description_content_type = text/markdown url = https://github.com/techspecs/techspecs-python @@ -21,4 +21,4 @@ packages = find: python_requires = >=3.6 [options.packages.find] -where = src \ No newline at end of file +where = src diff --git a/src/Techspecs.egg-info/PKG-INFO b/src/Techspecs.egg-info/PKG-INFO index ee79f29..c60e6f1 100644 --- a/src/Techspecs.egg-info/PKG-INFO +++ b/src/Techspecs.egg-info/PKG-INFO @@ -1,13 +1,12 @@ Metadata-Version: 2.1 Name: techspecs -Version: 0.0.7 -Summary: Library to make Techspecs API calls -Home-page: https://github.com/pypa/sampleproject -Author: Example Author -Author-email: author@example.com -License: UNKNOWN -Project-URL: Bug Tracker, https://github.com/pypa/sampleproject/issues -Platform: UNKNOWN +Version: 2.0.0 +Summary: TechSpecs Python +Home-page: https://github.com/techspecs/techspecs-python +Author: TechSpecs +Author-email: support@techspecs.io +Project-URL: Bug Tracker, https://github.com/techspecs/techspecs-python/issues +Keywords: TechSpecs API,specifications,phone specs,tablet specs,laptop specs,specs,smartwatch specs,smartphone specs,smartphones,tablets,smartwatches,laptops,Smartphone Specifications,Tablet Specifications,Smartwatch Specifications,Laptop Specifications,Technical Specifications,consumer electronics,ecommerce,techspecs-python,techspecs python,specifications library,tech specs API,tech specs Classifier: Programming Language :: Python :: 3 Classifier: License :: OSI Approved :: MIT License Classifier: Operating System :: OS Independent @@ -15,10 +14,300 @@ Requires-Python: >=3.6 Description-Content-Type: text/markdown License-File: LICENSE -# Techspecs +

+ TechSpecs Logo +

+ +# Introduction + +TechSpecs Python provides easy access to the standardized technical specifications of the world's consumer electronics, including smartphones, tablets, smartwatches, laptops, monitors, TVs and more. + +## Documentation + +- See the [TechSpecs API Docs](https://techspecs.readme.io) + +## API Key + +- [Signup](https://techspecs.io/) to get your TechSpecs API Key + + +## Requirements + +- Python 3.6+ + + +## Installation + +```sh +pip install techspecs +``` + +## Usage + +The library needs to be configured with your TechSpecs api key and base URL which is +available in your [TechSpecs Dashboard](https://techspecs.io/dashboard). + +Set `techspecs_api_key` to your key value and `techspecs_base_url` to your base value. + +### Product Search +#### Search for a device by specifying it's model name, version number or features + +```python +# Search for a product by name, version or features +import techspecs +import json + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +query = { + 'keyword': 'iPhone 13', # product name or version number to search + 'category': '', # product category to search (e.g. 'Smartphones', 'Tablets', or leave empty to search all categories) + 'page': 0 # page number to fetch results from +} + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Search for a product by name, version, or features +try: + response = techspecs.product_search(techspecs_base_url, query, techspecs_api_key, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") + + +``` + + +### Product Details + +```python +# Get the detailed specifications of a product +import techspecs + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +# TechSpecs product ID +techspecs_product_id = "63e96260ff7af4b68a304e40" + +# Query dictionary +query = { + "productId": techspecs_product_id +} + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +try: + # Validate techspecs_product_id + if not isinstance(techspecs_product_id, str): + raise ValueError('TechSpecs Product ID should be a string.') + + # Validate mode + if mode not in ['raw', 'pretty']: + raise ValueError('Invalid mode. Mode should be "raw" or "pretty".') + + # Call TechSpecs API to get product details + response = techspecs.product_detail(techspecs_base_url, techspecs_product_id, techspecs_api_key, mode=mode) + + # Print the product details + print(response) +except Exception as e: + print(f"An error occurred: {e}") + + +``` + + +### List all categories +```python +import techspecs + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Define constants +DEFAULT_MODE = 'pretty' +VALID_MODES = ['pretty', 'raw'] + +# Validate parameters +def validate_parameters(mode=DEFAULT_MODE): + if mode not in VALID_MODES: + raise ValueError(f'Invalid mode: {mode}. Mode should be one of {VALID_MODES}.') + +# Validate search parameters +try: + validate_parameters(mode=mode) +except ValueError as e: + print(f"Invalid search parameters: {e}") + exit() + +# Call TechSpecs API to get all categories +try: + response = techspecs.get_all_categories(techspecs_base_url, techspecs_api_key, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") + + +``` + + +### List all brands +```python +import techspecs + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Define function to validate parameters +def validate_parameters(mode): + if mode not in ['raw', 'pretty']: + raise ValueError(f'Invalid mode: {mode}. Mode should be one of ["raw", "pretty"].') + +# Validate parameters +try: + validate_parameters(mode) +except ValueError as e: + print(f"Invalid parameters: {e}") + exit() + +# Call TechSpecs API to get all brands +try: + response = techspecs.get_all_brands(techspecs_base_url, techspecs_api_key, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") + + +``` + + +### Advanced Search +#### List all products by brand, category and release date +```python +# List all products by brand, category and release date +import techspecs +import datetime + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Set constants +DEFAULT_PAGE = 0 +DEFAULT_MODE = 'pretty' +VALID_MODES = ['pretty', 'raw'] + +# Define function to validate date format +def is_valid_date(date_str): + try: + datetime.datetime.strptime(date_str, '%Y-%m-%d') + return True + except ValueError: + return False + +# Define function to validate parameters +def validate_parameters(brand, category, date=None, page=DEFAULT_PAGE, mode=DEFAULT_MODE): + if not isinstance(brand, list): + raise ValueError('Brand should be a list.') + + if not isinstance(category, list): + raise ValueError('Category should be a list.') + + if date is not None: + if not isinstance(date, dict): + raise ValueError('Invalid date format. Date should be a dictionary with keys "from" and "to".') + elif 'from' not in date or 'to' not in date: + raise ValueError('Invalid date format. Date dictionary should have keys "from" and "to".') + elif not is_valid_date(date['from']) or not is_valid_date(date['to']): + raise ValueError('Invalid date format. Date should be in the format YYYY-MM-DD.') + + if not isinstance(page, int) or page < 0: + raise ValueError('Page should be a non-negative integer.') + + if mode not in VALID_MODES: + raise ValueError(f'Invalid mode: {mode}. Mode should be one of {VALID_MODES}.') + +# Define search parameters +brand = ["Apple"] +category = ["Smartphones"] +date = { + "from": "2010-01-01", + "to": "2022-03-15" +} +page = 0 + +# Validate search parameters +try: + validate_parameters(brand, category, date=date, page=page, mode=mode) +except ValueError as e: + print(f"Invalid search parameters: {e}") + exit() + + +# Call TechSpecs API to get all products +try: + response = techspecs.get_all_products(techspecs_base_url, techspecs_api_key, brand, category, date, page, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") + + +``` + + +### Machine ID Search +#### Search for Apple products by machine id +```python +# Search for an Apple product by machine id +import techspecs + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +# Serial number of the Apple machine to look up machineid_or_codename ex iphone 11,6 +machine_id = "iphone 11,6" + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Look up the Apple machine by its serial number +try: + response = techspecs.machine_id_search(techspecs_base_url, techspecs_api_key, machine_id, mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") + + +``` -This is a python package to make [Techspecs](https://techspecs.readme.io/reference/getting-started-with-your-api-1) -API calls easier. -[Get API key](https://developer.dashboard.techspecs.io/) -to get started. diff --git a/src/Techspecs.egg-info/SOURCES.txt b/src/Techspecs.egg-info/SOURCES.txt index 6b12837..8b59a1a 100644 --- a/src/Techspecs.egg-info/SOURCES.txt +++ b/src/Techspecs.egg-info/SOURCES.txt @@ -2,6 +2,10 @@ LICENSE README.md pyproject.toml setup.cfg +src/TechSpecs.egg-info/PKG-INFO +src/TechSpecs.egg-info/SOURCES.txt +src/TechSpecs.egg-info/dependency_links.txt +src/TechSpecs.egg-info/top_level.txt src/Techspecs.egg-info/PKG-INFO src/Techspecs.egg-info/SOURCES.txt src/Techspecs.egg-info/dependency_links.txt @@ -11,4 +15,11 @@ src/techspecs/techspecs.py src/techspecs.egg-info/PKG-INFO src/techspecs.egg-info/SOURCES.txt src/techspecs.egg-info/dependency_links.txt -src/techspecs.egg-info/top_level.txt \ No newline at end of file +src/techspecs.egg-info/top_level.txt +tests/test_get_all_brandlogos.py +tests/test_get_all_brands.py +tests/test_get_all_categories.py +tests/test_machine_id_search.py +tests/test_product_detail.py +tests/test_product_search.py +tests/test_techspecs_advanced_search.py \ No newline at end of file diff --git a/src/techspecs/techspecs.py b/src/techspecs/techspecs.py index 87de619..1425615 100644 --- a/src/techspecs/techspecs.py +++ b/src/techspecs/techspecs.py @@ -1,119 +1,284 @@ import json import requests +def product_search(techspecs_base_url, query: dict, techspecs_api_key, mode='raw'): + """ + Search for products that match the specified query. -def search(techspecs_base, query: dict, key, mode='raw'): - url = f'https://apis.dashboard.techspecs.io/{techspecs_base}/api/product/search?' - header = { + :param base_url: the base URL of the TechSpecs API + :param query: a dictionary that contains the search query parameters + :param bearer_token: the bearer token for API authentication + :param mode: the output mode ('raw' or 'pretty') + :return: the search results in the specified output mode + """ + q = query["keyword"].strip() + url = f'{techspecs_base_url}/v4/product/search?query={q}' + headers = { "Accept": "application/json", - "x-blobr-key": key, + "Authorization": f"Bearer {techspecs_api_key}", "Content-Type": "application/json" } - parameter = {'query': query['keyword'].replace(' ', '%20')} - payload = {"category": query['category']} - req = requests.post(url, params=parameter, json=payload, headers=header).json() + payload = {"category": query['category'].strip()} + response = requests.post(url, json=payload, headers=headers).json() if mode == 'raw': - return req + return response elif mode == 'pretty': try: - mod_list = req['data']['results'] + mod_list = response['data'] return json.dumps(mod_list, indent=4) - except: - return req - + except KeyError: + return response else: return 'Invalid Mode' -def detail(techspecs_base, techspecs_id, key, mode='raw'): - url = f'https://apis.dashboard.techspecs.io/{techspecs_base}/api/product/get/' - header = { +def product_detail(techspecs_base_url, techspecs_product_id, techspecs_api_key, mode='raw'): + """ + Get detailed information about a specific product. + + :param base_url: the base URL of the TechSpecs API + :param product_id: the ID of the product to get information about + :param bearer_token: the bearer token for API authentication + :param mode: the output mode ('raw' or 'pretty') + :return: the product information in the specified output mode + """ + url = f'{techspecs_base_url}/v4/product/detail' + params = { + 'productId': techspecs_product_id.strip() + } + headers = { "Accept": "application/json", - "Accept-Encoding": "gzip, deflate", - "x-blobr-key": key + "Authorization": f"Bearer {techspecs_api_key}", } - req = requests.get(url + techspecs_id, headers=header).json() + try: + response = requests.get(url, headers=headers, params=params) + response.raise_for_status() + data = response.json() + except requests.exceptions.HTTPError as error: + return f"HTTP error occurred: {error}" + except requests.exceptions.RequestException as error: + return f"An error occurred: {error}" + except ValueError as error: + return f"Failed to decode JSON: {error}" + if mode == 'raw': - return req + return data elif mode == 'pretty': try: - mod_list = req['data']['product'] + mod_list = data['data'] modified_data = [] for m in mod_list: mod_dict = {} for a, b in m.items(): try: mod_dict[a] = b - except: + except TypeError: for x, y in b: mod_dict[x] = y modified_data.append(mod_dict) return json.dumps(modified_data, indent=4) - except: - return req + except KeyError: + return data else: return 'Invalid Mode' -def brands(techspecs_base, key, mode='raw'): - url = f'https://apis.dashboard.techspecs.io/{techspecs_base}/api/product/brands' - header = { + +def get_all_brands(techspecs_base_url, techspecs_api_key, mode='raw'): + """ + Get a list of all brands. + + :param base_url: the base URL of the TechSpecs API + :param bearer_token: the bearer token for API authentication + :param mode: the output mode ('raw' or 'pretty') + :return: the list of brands in the specified output mode + """ + url = f'{techspecs_base_url}/v4/brand/all' + headers = { "Accept": "application/json", - "x-blobr-key": key + "Authorization": f"Bearer {techspecs_api_key}" } - req = requests.get(url, headers=header).json() + try: + response = requests.get(url, headers=headers) + response.raise_for_status() + data = response.json() + except requests.exceptions.HTTPError as error: + return f"HTTP error occurred: {error}" + except requests.exceptions.RequestException as error: + return f"An error occurred: {error}" + except ValueError as error: + return f"Failed to decode JSON: {error}" + if mode == 'raw': - return req + return data elif mode == 'pretty': try: - mod_list = req['data']['brands'] + mod_list = data['data'] return json.dumps(mod_list, indent=4) - except: - return req + except KeyError: + return data else: return 'Invalid Mode' -def categories(techspecs_base, key, mode='raw'): - url = f'https://apis.dashboard.techspecs.io/{techspecs_base}/api/category/getAll' - header = { + +def get_all_categories(techspecs_base_url, techspecs_api_key, mode='raw'): + """ + Get a list of all categories. + + :param base_url: the base URL of the TechSpecs API + :param bearer_token: the bearer token for API authentication + :param mode: the output mode ('raw' or 'pretty') + :return: the list of categories in the specified output mode + """ + url = f'{techspecs_base_url}/v4/category/all' + headers = { "Accept": "application/json", - "x-blobr-key": key + "Authorization": f"Bearer {techspecs_api_key}" } - req = requests.get(url, headers=header).json() + try: + response = requests.get(url, headers=headers) + response.raise_for_status() + data = response.json() + except requests.exceptions.HTTPError as error: + return f"HTTP error occurred: {error}" + except requests.exceptions.RequestException as error: + return f"An error occurred: {error}" + except ValueError as error: + return f"Failed to decode JSON: {error}" + if mode == 'raw': - return req + return data elif mode == 'pretty': try: - mod_list = req['data']['Category'] - return json.dumps(mod_list[1], indent=4) - except: - return req + mod_list = data['data'] + return json.dumps(mod_list, indent=4) + except KeyError: + return data else: return 'Invalid Mode' -def products(techspecs_base, brand: list, category: list, date: dict, page, key, mode='raw'): - url = f"https://apis.dashboard.techspecs.io/{techspecs_base}/api/product/getAll?page=" + str(page) + +def get_all_products(techspecs_base_url, techspecs_api_key, brand=[], category=[], date={}, page=0, mode='raw'): + """ + Get a list of products matching the specified criteria. + + :param base_url: the base URL of the TechSpecs API + :param bearer_token: the bearer token for API authentication + :param brand: a list of brand names to include in the search (default: []) + :param category: a list of category names to include in the search (default: []) + :param date: a dictionary containing 'from' and 'to' date strings to limit the search (default: {}) + :param page: the page number of the search results to retrieve (default: 0) + :param mode: the output mode ('raw' or 'pretty') + :return: the list of products in the specified output mode + """ + url = f'{techspecs_base_url}/v4/product/all?page={page}' payload = { "brand": brand, "category": category, - "from": date['from'], - "to": date['to'] + "from": date.get('from', ''), + "to": date.get('to', '') } - header = { + headers = { "Accept": "application/json", - "X-BLOBR-KEY": key, + "Authorization": f"Bearer {techspecs_api_key}", "Content-Type": "application/json" } - req = requests.post(url, json=payload, headers=header).json() + try: + response = requests.post(url, json=payload, headers=headers) + response.raise_for_status() + data = response.json() + except requests.exceptions.HTTPError as error: + return f"HTTP error occurred: {error}" + except requests.exceptions.RequestException as error: + return f"An error occurred: {error}" + except ValueError as error: + return f"Failed to decode JSON: {error}" + if mode == 'raw': - return req + return data elif mode == 'pretty': try: - mod_list = req['data']['product'] + mod_list = data['data'] return json.dumps(mod_list, indent=4) - except: - return req + except KeyError: + return data else: return 'Invalid Mode' + + + + +def get_all_brand_logos(techspecs_base_url, techspecs_api_key, mode='raw'): + """ + Get a list of all brand logos. + + :param base_url: the base URL of the TechSpecs API + :param bearer_token: the bearer token for API authentication + :param mode: the output mode ('raw' or 'pretty') + :return: the list of brand logos in the specified output mode + """ + url = f'{techspecs_base_url}/v4/brandlogo/all' + headers = { + "Accept": "application/json", + "Authorization": f"Bearer {techspecs_api_key}" + } + try: + response = requests.get(url, headers=headers) + response.raise_for_status() + data = response.json() + except requests.exceptions.HTTPError as error: + return f"HTTP error occurred: {error}" + except requests.exceptions.RequestException as error: + return f"An error occurred: {error}" + except ValueError as error: + return f"Failed to decode JSON: {error}" + + if mode == 'raw': + return data + elif mode == 'pretty': + try: + mod_list = data['data'] + return json.dumps(mod_list, indent=4) + except KeyError: + return data + else: + return 'Invalid Mode' + + + + +def machine_id_search(techspecs_base_url, techspecs_api_key, machine_id, mode='raw'): + """ + Look up a product by its machine ID or code name. + + :param base_url: the base URL of the TechSpecs API + :param bearer_token: the bearer token for API authentication + :param machine_id: the machine ID or code name of the product to look up + :param mode: the output mode ('raw' or 'pretty') + :return: the search results in the specified output mode + """ + url = f'{techspecs_base_url}/v4/product/search/machineid?query={machine_id.strip()}' + headers = { + "Accept": "application/json", + "Authorization": f"Bearer {techspecs_api_key}" + } + try: + response = requests.post(url, headers=headers) + response.raise_for_status() + data = response.json() + except requests.exceptions.HTTPError as error: + return f"HTTP error occurred: {error}" + except requests.exceptions.RequestException as error: + return f"An error occurred: {error}" + except ValueError as error: + return f"Failed to decode JSON: {error}" + + if mode == 'raw': + return data + elif mode == 'pretty': + return json.dumps(data, indent=4) + else: + return 'Invalid Mode' + diff --git a/tests/all_brands.py b/tests/all_brands.py deleted file mode 100644 index b03714c..0000000 --- a/tests/all_brands.py +++ /dev/null @@ -1,7 +0,0 @@ -import techspecs -# All brands - -techspecs_key = "" -techspecs_base = "" -brands = techspecs.brands(techspecs_base, techspecs_key, mode='pretty') -print(brands) diff --git a/tests/all_categories.py b/tests/all_categories.py deleted file mode 100644 index 7d9b268..0000000 --- a/tests/all_categories.py +++ /dev/null @@ -1,7 +0,0 @@ -import techspecs -# All Categories - -techspecs_key = "" -techspecs_base = "" -categories = techspecs.categories(techspecs_base, techspecs_key, mode='pretty') -print(categories) diff --git a/tests/all_products.py b/tests/all_products.py deleted file mode 100644 index 2f40869..0000000 --- a/tests/all_products.py +++ /dev/null @@ -1,14 +0,0 @@ -import techspecs -# All Products By Brand And Category - -techspecs_key = "" -techspecs_base = "" -page = 1 -brand = [""] -category = [""] -time = { - "from": "2010-01-01", # YYYY-MM-DD - "to": "2022-03-15" # YYYY-MM-DD -} -all_products = techspecs.products(techspecs_base, brand, category, time, page, techspecs_key, mode='pretty') -print(all_products) diff --git a/tests/details.py b/tests/details.py deleted file mode 100644 index 9364642..0000000 --- a/tests/details.py +++ /dev/null @@ -1,9 +0,0 @@ -import techspecs -# Product Details - -techspecs_key = "" -techspecs_base = "" -techspecs_id = "" - -details = techspecs.detail(techspecs_base, techspecs_id, techspecs_key, mode='pretty') -print(details) diff --git a/tests/search.py b/tests/search.py deleted file mode 100644 index 1aa43aa..0000000 --- a/tests/search.py +++ /dev/null @@ -1,11 +0,0 @@ -import techspecs -# Search Product - -techspecs_key = "" -techspecs_base = "" -query = { - 'keyword': '', - 'category': '', -} -search = techspecs.search(techspecs_base, query, techspecs_key, mode='pretty') -print(search) diff --git a/tests/test_get_all_brandlogos.py b/tests/test_get_all_brandlogos.py new file mode 100644 index 0000000..bc438b7 --- /dev/null +++ b/tests/test_get_all_brandlogos.py @@ -0,0 +1,17 @@ +import techspecs + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Call TechSpecs API to get all brand logos +try: + response = techspecs.get_all_brand_logos(techspecs_base_url, techspecs_api_key, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") diff --git a/tests/test_get_all_brands.py b/tests/test_get_all_brands.py new file mode 100644 index 0000000..f335b77 --- /dev/null +++ b/tests/test_get_all_brands.py @@ -0,0 +1,29 @@ +import techspecs + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Define function to validate parameters +def validate_parameters(mode): + if mode not in ['raw', 'pretty']: + raise ValueError(f'Invalid mode: {mode}. Mode should be one of ["raw", "pretty"].') + +# Validate parameters +try: + validate_parameters(mode) +except ValueError as e: + print(f"Invalid parameters: {e}") + exit() + +# Call TechSpecs API to get all brands +try: + response = techspecs.get_all_brands(techspecs_base_url, techspecs_api_key, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") diff --git a/tests/test_get_all_categories.py b/tests/test_get_all_categories.py new file mode 100644 index 0000000..940c5f2 --- /dev/null +++ b/tests/test_get_all_categories.py @@ -0,0 +1,33 @@ +import techspecs + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Define constants +DEFAULT_MODE = 'pretty' +VALID_MODES = ['pretty', 'raw'] + +# Validate parameters +def validate_parameters(mode=DEFAULT_MODE): + if mode not in VALID_MODES: + raise ValueError(f'Invalid mode: {mode}. Mode should be one of {VALID_MODES}.') + +# Validate search parameters +try: + validate_parameters(mode=mode) +except ValueError as e: + print(f"Invalid search parameters: {e}") + exit() + +# Call TechSpecs API to get all categories +try: + response = techspecs.get_all_categories(techspecs_base_url, techspecs_api_key, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") diff --git a/tests/test_machine_id_search.py b/tests/test_machine_id_search.py new file mode 100644 index 0000000..19665b7 --- /dev/null +++ b/tests/test_machine_id_search.py @@ -0,0 +1,20 @@ +import techspecs + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +# Serial number of the Apple machine to look up machineid_or_codename ex iphone 11,6 +machine_id = "iphone 11,6" + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Look up the Apple machine by its serial number +try: + response = techspecs.machine_id_search(techspecs_base_url, techspecs_api_key, machine_id, mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") diff --git a/tests/test_product_detail.py b/tests/test_product_detail.py new file mode 100644 index 0000000..1e82814 --- /dev/null +++ b/tests/test_product_detail.py @@ -0,0 +1,35 @@ +import techspecs + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +# TechSpecs product ID +techspecs_product_id = "63e96260ff7af4b68a304e40" + +# Query dictionary +query = { + "productId": techspecs_product_id +} + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +try: + # Validate techspecs_product_id + if not isinstance(techspecs_product_id, str): + raise ValueError('TechSpecs Product ID should be a string.') + + # Validate mode + if mode not in ['raw', 'pretty']: + raise ValueError('Invalid mode. Mode should be "raw" or "pretty".') + + # Call TechSpecs API to get product details + response = techspecs.product_detail(techspecs_base_url, techspecs_product_id, techspecs_api_key, mode=mode) + + # Print the product details + print(response) +except Exception as e: + print(f"An error occurred: {e}") diff --git a/tests/test_product_search.py b/tests/test_product_search.py new file mode 100644 index 0000000..5d5480c --- /dev/null +++ b/tests/test_product_search.py @@ -0,0 +1,24 @@ +import techspecs +import json + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +query = { + 'keyword': 'iPhone 13', # product name or version number to search + 'category': '', # product category to search (e.g. 'Smartphones', 'Tablets', or leave empty to search all categories) + 'page': 0 # page number to fetch results from +} + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Search for a product by name, version, or features +try: + response = techspecs.product_search(techspecs_base_url, query, techspecs_api_key, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}") diff --git a/tests/test_techspecs_advanced_search.py b/tests/test_techspecs_advanced_search.py new file mode 100644 index 0000000..4994b87 --- /dev/null +++ b/tests/test_techspecs_advanced_search.py @@ -0,0 +1,70 @@ +import techspecs +import datetime + +# TechSpecs API base URL +techspecs_base_url = "https://api.techspecs.io" + +# TechSpecs API bearer token +techspecs_api_key = "your_techspecs_api_key" + +# Output mode ('raw' or 'pretty') +mode = 'pretty' + +# Set constants +DEFAULT_PAGE = 0 +DEFAULT_MODE = 'pretty' +VALID_MODES = ['pretty', 'raw'] + +# Define function to validate date format +def is_valid_date(date_str): + try: + datetime.datetime.strptime(date_str, '%Y-%m-%d') + return True + except ValueError: + return False + +# Define function to validate parameters +def validate_parameters(brand, category, date=None, page=DEFAULT_PAGE, mode=DEFAULT_MODE): + if not isinstance(brand, list): + raise ValueError('Brand should be a list.') + + if not isinstance(category, list): + raise ValueError('Category should be a list.') + + if date is not None: + if not isinstance(date, dict): + raise ValueError('Invalid date format. Date should be a dictionary with keys "from" and "to".') + elif 'from' not in date or 'to' not in date: + raise ValueError('Invalid date format. Date dictionary should have keys "from" and "to".') + elif not is_valid_date(date['from']) or not is_valid_date(date['to']): + raise ValueError('Invalid date format. Date should be in the format YYYY-MM-DD.') + + if not isinstance(page, int) or page < 0: + raise ValueError('Page should be a non-negative integer.') + + if mode not in VALID_MODES: + raise ValueError(f'Invalid mode: {mode}. Mode should be one of {VALID_MODES}.') + +# Define search parameters +brand = ["Apple"] +category = ["Smartphones"] +date = { + "from": "2010-01-01", + "to": "2022-03-15" +} +page = 0 + +# Validate search parameters +try: + validate_parameters(brand, category, date=date, page=page, mode=mode) +except ValueError as e: + print(f"Invalid search parameters: {e}") + exit() + + +# Call TechSpecs API to get all products +try: + response = techspecs.get_all_products(techspecs_base_url, techspecs_api_key, brand, category, date, page, mode=mode) + print(response) +except Exception as e: + print(f"An error occurred: {e}")