-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/vtex-apptype' of github.com:weni-ai/weni-integr…
…ations-engine into feature/create-vtex-catalog
- Loading branch information
Showing
35 changed files
with
976 additions
and
245 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
marketplace/applications/migrations/0017_alter_app_platform.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 3.2.4 on 2023-11-23 20:57 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('applications', '0016_app_configured'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='app', | ||
name='platform', | ||
field=models.CharField(choices=[('IA', 'inteligence-artificial'), ('WF', 'weni-flows'), ('RC', 'rocketchat'), ('VT', 'vtex')], max_length=2), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,100 @@ | ||
from marketplace.clients.base import RequestClient | ||
|
||
|
||
class VtexPublicClient(RequestClient): | ||
def list_products(self, domain): | ||
url = f"https://{domain}/api/catalog_system/pub/products/search/" | ||
response = self.make_request( | ||
url, method="GET" | ||
) # TODO: list all paginate products | ||
return response | ||
class VtexAuthorization(RequestClient): | ||
def __init__(self, app_key, app_token): | ||
self.app_key = app_key | ||
self.app_token = app_token | ||
|
||
def _get_headers(self): | ||
headers = { | ||
"Accept": "application/json", | ||
"Content-Type": "application/json", | ||
"X-VTEX-API-AppKey": self.app_key, | ||
"X-VTEX-API-AppToken": self.app_token, | ||
} | ||
return headers | ||
|
||
|
||
class VtexCommonClient(RequestClient): | ||
def check_domain(self, domain): | ||
try: | ||
url = f"https://{domain}/api/catalog_system/pub/products/search/" | ||
response = self.make_request(url, method="GET") | ||
return response.status_code == 206 | ||
except Exception: | ||
return False | ||
|
||
|
||
class VtexPublicClient(VtexCommonClient): | ||
def search_product_by_sku_id(self, skuid, domain, sellerid=1): | ||
url = f"https://{domain}/api/catalog_system/pub/products/search?fq=skuId:{skuid}&sellerId={sellerid}" | ||
response = self.make_request(url, method="GET") | ||
return response | ||
|
||
|
||
class VtexPrivateClient(VtexAuthorization, VtexCommonClient): | ||
def is_valid_credentials(self, domain): | ||
try: | ||
url = ( | ||
f"https://{domain}/api/catalog_system/pvt/products/GetProductAndSkuIds" | ||
) | ||
headers = self._get_headers() | ||
response = self.make_request(url, method="GET", headers=headers) | ||
return response.status_code == 200 | ||
except Exception: | ||
return False | ||
|
||
def list_all_products_sku_ids(self, domain, page_size=1000): | ||
all_skus = [] | ||
page = 1 | ||
|
||
while True: | ||
url = f"https://{domain}/api/catalog_system/pvt/sku/stockkeepingunitids?page={page}&pagesize={page_size}" | ||
headers = self._get_headers() | ||
response = self.make_request(url, method="GET", headers=headers) | ||
|
||
sku_ids = response.json() | ||
if not sku_ids: | ||
break | ||
|
||
all_skus.extend(sku_ids) | ||
page += 1 | ||
|
||
return all_skus | ||
|
||
def list_active_sellers(self, domain): | ||
url = f"https://{domain}/api/seller-register/pvt/sellers" | ||
headers = self._get_headers() | ||
response = self.make_request(url, method="GET", headers=headers) | ||
sellers_data = response.json() | ||
return [seller["id"] for seller in sellers_data["items"] if seller["isActive"]] | ||
|
||
def get_product_details(self, sku_id, domain): | ||
url = ( | ||
f"https://{domain}/api/catalog_system/pvt/sku/stockkeepingunitbyid/{sku_id}" | ||
) | ||
headers = self._get_headers() | ||
response = self.make_request(url, method="GET", headers=headers) | ||
return response.json() | ||
|
||
def pub_simulate_cart_for_seller(self, sku_id, seller_id, domain): | ||
cart_simulation_url = f"https://{domain}/api/checkout/pub/orderForms/simulation" | ||
payload = {"items": [{"id": sku_id, "quantity": 1, "seller": seller_id}]} | ||
|
||
response = self.make_request(cart_simulation_url, method="POST", json=payload) | ||
simulation_data = response.json() | ||
|
||
if simulation_data["items"]: | ||
item_data = simulation_data["items"][0] | ||
return { | ||
"is_available": item_data["availability"] == "available", | ||
"price": item_data["price"], | ||
"list_price": item_data["listPrice"], | ||
} | ||
else: | ||
return { | ||
"is_available": False, | ||
"price": 0, | ||
"list_price": 0, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from marketplace.core.types.base import AppType | ||
from marketplace.applications.models import App | ||
|
||
|
||
class EcommerceAppType(AppType): | ||
platform = App.PLATFORM_VTEX | ||
category = AppType.CATEGORY_ECOMMERCE |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from rest_framework import serializers | ||
from rest_framework.exceptions import ValidationError | ||
|
||
from marketplace.core.serializers import AppTypeBaseSerializer | ||
from marketplace.applications.models import App | ||
|
||
|
||
class VtexSerializer(serializers.Serializer): | ||
domain = serializers.CharField(required=True) | ||
app_key = serializers.CharField(required=True) | ||
app_token = serializers.CharField(required=True) | ||
wpp_cloud_uuid = serializers.UUIDField(required=True) | ||
|
||
def validate_wpp_cloud_uuid(self, value): | ||
""" | ||
Check that the wpp_cloud_uuid corresponds to an existing App with code 'wpp-cloud'. | ||
""" | ||
try: | ||
App.objects.get(uuid=value, code="wpp-cloud") | ||
except App.DoesNotExist: | ||
raise ValidationError( | ||
"The wpp_cloud_uuid does not correspond to a valid 'wpp-cloud' App." | ||
) | ||
return str(value) | ||
|
||
|
||
class VtexAppSerializer(AppTypeBaseSerializer): | ||
config = serializers.SerializerMethodField() | ||
|
||
class Meta: | ||
model = App | ||
fields = ( | ||
"code", | ||
"uuid", | ||
"project_uuid", | ||
"platform", | ||
"config", | ||
"created_by", | ||
"created_on", | ||
"modified_by", | ||
) | ||
read_only_fields = ("code", "uuid", "platform") | ||
|
||
def get_config(self, obj): | ||
config = obj.config.copy() | ||
api_credentials = config.get("api_credentials", {}) | ||
if api_credentials: | ||
api_credentials["app_key"] = "***" | ||
api_credentials["app_token"] = "***" | ||
|
||
return config |
Empty file.
Oops, something went wrong.