diff --git a/README.md b/README.md index 79c4d7f5c..ecca277ac 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,23 @@ +# SKU Seller API Integration + +This feature implements the SKU Seller API integration, allowing users to consult and delete an SKU Seller. An SKU Seller is a seller associated with an SKU, which is essential for managing multiple sellers offering the same product in a marketplace. + +## Features + +- **Consult SKU Seller**: Retrieve information about a specific SKU Seller. +- **Delete SKU Seller**: Remove the association of a seller with an SKU. + +## Usage + +### API Client + +```python +from src.api_client import APIClient +from src.authentication import Authentication + +auth = Authentication(token="your_api_token") +api_client = APIClient(base_url="https://api.vtex.com", headers=auth.get_headers()) +``` # VTEX SDK 🛒 ⚙️ [VTEX](https://vtex.com) platform .NET SDK. diff --git a/src/api_client.py b/src/api_client.py new file mode 100644 index 000000000..5a38d7f7a --- /dev/null +++ b/src/api_client.py @@ -0,0 +1,21 @@ +import requests + +class APIClient: + def __init__(self, base_url, headers): + self.base_url = base_url + self.headers = headers + + def get_sku_seller(self, sku_id, seller_id): + url = f"{self.base_url}/sku/{sku_id}/seller/{seller_id}" + response = requests.get(url, headers=self.headers) + response.raise_for_status() + return response.json() + + def delete_sku_seller(self, sku_id, seller_id): + url = f"{self.base_url}/sku/{sku_id}/seller/{seller_id}" + response = requests.delete(url, headers=self.headers) + response.raise_for_status() + return response.status_code + +# Example usage: +# client = APIClient(base_url="https://api.vtex.com", headers={"Authorization": "Bearer token"}) diff --git a/src/authentication.py b/src/authentication.py new file mode 100644 index 000000000..48aa2685c --- /dev/null +++ b/src/authentication.py @@ -0,0 +1,16 @@ +class Authentication: + def __init__(self, token): + self.token = token + + def get_headers(self): + return { + "Authorization": f"Bearer {self.token}", + "Content-Type": "application/json" + } + +# Example usage: +# auth = Authentication(token="your_api_token") +# headers = auth.get_headers() +# api_client = APIClient(base_url="https://api.vtex.com", headers=headers) +# service = SkuSellerService(api_client) +# sku_seller = service.get_sku_seller("sku123", "seller456") diff --git a/src/error_handling.py b/src/error_handling.py new file mode 100644 index 000000000..c9f40d8e4 --- /dev/null +++ b/src/error_handling.py @@ -0,0 +1,19 @@ +class APIError(Exception): + def __init__(self, status_code, message="API request failed"): + self.status_code = status_code + self.message = message + super().__init__(self.message) + + def __str__(self): + return f"{self.message} (Status Code: {self.status_code})" + +def handle_api_errors(response): + if not response.ok: + raise APIError(status_code=response.status_code, message=response.text) + +# Example usage in APIClient: +# response = requests.get(url, headers=self.headers) +# handle_api_errors(response) +# return response.json() +# +# This function can be used in the APIClient methods to handle errors. diff --git a/src/models.py b/src/models.py new file mode 100644 index 000000000..fe9452d16 --- /dev/null +++ b/src/models.py @@ -0,0 +1,15 @@ +class SkuSeller: + def __init__(self, sku_id, seller_id, price, inventory): + self.sku_id = sku_id + self.seller_id = seller_id + self.price = price + self.inventory = inventory + + @classmethod + def from_json(cls, data): + return cls( + sku_id=data.get('skuId'), + seller_id=data.get('sellerId'), + price=data.get('price'), + inventory=data.get('inventory') + ) diff --git a/src/service_layer.py b/src/service_layer.py new file mode 100644 index 000000000..474c9fcc8 --- /dev/null +++ b/src/service_layer.py @@ -0,0 +1,28 @@ +from src.api_client import APIClient +from src.models import SkuSeller + +class SkuSellerService: + def __init__(self, api_client): + self.api_client = api_client + + def get_sku_seller(self, sku_id, seller_id): + try: + data = self.api_client.get_sku_seller(sku_id, seller_id) + return SkuSeller.from_json(data) + except Exception as e: + print(f"Error retrieving SKU Seller: {e}") + return None + + def delete_sku_seller(self, sku_id, seller_id): + try: + status_code = self.api_client.delete_sku_seller(sku_id, seller_id) + return status_code == 204 + except Exception as e: + print(f"Error deleting SKU Seller: {e}") + return False + +# Example usage: +# api_client = APIClient(base_url="https://api.vtex.com", headers={"Authorization": "Bearer token"}) +# service = SkuSellerService(api_client) +# sku_seller = service.get_sku_seller("sku123", "seller456") +# success = service.delete_sku_seller("sku123", "seller456") diff --git a/tests/test_sku_seller_service.py b/tests/test_sku_seller_service.py new file mode 100644 index 000000000..74e96a7e4 --- /dev/null +++ b/tests/test_sku_seller_service.py @@ -0,0 +1,33 @@ +import unittest +from unittest.mock import MagicMock +from src.service_layer import SkuSellerService +from src.models import SkuSeller + +class TestSkuSellerService(unittest.TestCase): + def setUp(self): + self.mock_api_client = MagicMock() + self.service = SkuSellerService(self.mock_api_client) + + def test_get_sku_seller(self): + # Mock API response + self.mock_api_client.get_sku_seller.return_value = { + 'skuId': 'sku123', + 'sellerId': 'seller456', + 'price': 100.0, + 'inventory': 50 + } + + sku_seller = self.service.get_sku_seller('sku123', 'seller456') + self.assertIsInstance(sku_seller, SkuSeller) + self.assertEqual(sku_seller.sku_id, 'sku123') + self.assertEqual(sku_seller.seller_id, 'seller456') + + def test_delete_sku_seller(self): + # Mock API response + self.mock_api_client.delete_sku_seller.return_value = 204 + + success = self.service.delete_sku_seller('sku123', 'seller456') + self.assertTrue(success) + +if __name__ == '__main__': + unittest.main()