I have to get information about product stocks from marketplace's API via POST request. API requires to send products IDs (sku) in url Example: https://api.market.com/campaigns/{campaignId}/warehouse/{warehouseId}/stocks/actual.json?sku=sku1&sku=sku2&sku=sku3 So I guess, I have to pass a dict like {'sku': '1', 'sku': '2', 'sku': '3'} But of course, It's impossible to create a dict with same keys. I don't know how to solve this task. I made a function, using urllib (urlencode) that works. But it create an url with only last element in params. params = {"sku": "ps-22-1", "sku2": "ps-22-7-2", "sku3": "ps-22-7-3"}
def get_stocks(self, campaign_id, warehouse_id, sku):
""" Method for parse stocks
Parameters:
campaign_id (int): client_id in Store
warehouse_id (int): warehouse_id in Warehouse
sku (str): product sku in Product
Returns:
data (json): JSON with stocks data """
url = f"{self.url}campaigns/{campaign_id}/warehouses/{warehouse_id}/stocks/actual.json?"
req = requests.get(url + urllib.parse.urlencode(sku),
headers=self.headers)
if req.status_code == 200:
return True, '200', req.json()
return False, req.json()["error"]["message"]
I keep products IDs in my DB in such model:
class Product(models.Model):
name = models.CharField(max_length=150)
sku = models.CharField(max_length=10)
Create a dictionary which contains all the list of products IDs (sku), then pass it through the request as data, like shown below:
def get_stocks(self, campaign_id, warehouse_id, sku):
""" Method for parse stocks
Parameters:
campaign_id (int): client_id in Store
warehouse_id (int): warehouse_id in Warehouse
sku (str): product sku in Product
Returns:
data (json): JSON with stocks data """
url = f"{self.url}campaigns/{campaign_id}/warehouses/{warehouse_id}/stocks/actual.json?"
data = {"sku_ids":["ps-22-1","ps-22-7-2","ps-22-7-3"]}
req = requests.get(url + urllib.parse.urlencode(sku),
headers=self.headers,
data=data)
if req.status_code == 200:
return True, '200', req.json()
return False, req.json()["error"]["message"]
And You have to make the changes in the API too, to filter multiple values. In the below example, I am using SqlAlchemy ORM. You can change yours according to your ORM.
data = request.data()
session().query(Product).filter(Product.sku.in_(data['sku_ids'])).all()