Skip to main content

Hi, I'm New to SFCC OCAPI. My purpose is to Export all Orders from "development.demandware.net" after certain date and this can happen Quite frequently like once in every 2 days. I'm currently using Python to achieve this using the endpoint "s/{{SITEID}}/dw/shop/v18_1/order_search". The problem is One call is getting me only 25 Records. Again i have change the query from dynamically to start from RecordNo 26 for the next call. So, If I have like 10,000 records, it makes upto 400 calls everytime the scirpt runs.  The alternative options i'm aware of is:

- OCAPI Batch requests

- OCAPI Export job (Tried this, but haven't got enough knowledge to set this up) 

 

So, I'd like to know if my purposed is achievable using the batch request. I tried to do this using the  documentation. And, the response was 200 with no response body using the below code.

 

url = f"https://{DOMAIN}/s/-/dw/batch"

url_param = {'client_id': CLIENT_ID}

header = {'Authorization': 'Bearer ' + token,

'Origin': f'https://{DOMAIN}',

'Content-Type': 'multipart/mixed;boundary=23dh3f9f4',

'x-dw-http-method': 'POST',

'x-dw-resource-path': 's/{{SITEID}}/dw/shop/v18_1/order_search'}

body = """

{

"query" :

{

"filtered_query": {

"query": { "match_all_query": {} },

"filter": {

"range_filter": {

"field": "creation_date",

"from": "%s",

"from_inclusive": true

}

}

}

},

"select" : "(**)",

"sorts": [{

"field": "order_no",

"sort_order": "asc"

}],

"start": %s

}""" %(RETRIVE_RECORDS_FROM, startRecordFrom)

response = requests.post(url, params = url_param, headers = header, data = body)

 

My code doesn't have a x-dw-content-id as the above is an initial request. If its possible to achieve my purpose, how should my sub-request looks like.? And after that how do i retrieve the data of my request.?

 

I maybe asking for too much information. But, i couldn't find much information about this online so had to ask every question i have in one post. 

 

Thanks in advance.

 

#OCAPI #Python #Process Batch  @* B2C Commerce * @B2C Commerce Developers

1 个回答
  1. 2023年9月12日 13:07

    Hi @Harsha Vardhan Seelam,

    First of all in one request If I remember correctly you can increase the count number to get more order then 25. Just check some documentation. I think there is still a limit (probably 100 not sure) 

     

    You should these request as pagination request so you always give the number for your search starting point and how many records you want to fetch

     

    Here is an example for your pyhton how to add count and start to your requests, In this example we are searching customers who have "gmail" in their profile and fetching 10 of them from the start of the 10th customer, so it will return 10-19 customer in the search list. So you can tweak these numbers and get the results that you want programmatically.

    import requests

    import json

    url = "https://yourclientdomain.com/s/-/dw/data/v19_1//customer_lists/EUR/customer_search"

    payload = json.d_u_m_p_s({ // I change here please remove "_" becauase Trailhead does not allow the word :D

    "query": {

    "text_query": {

    "fields": [

    "credentials.login"

    ],

    "search_phrase": "gmail"

    }

    },

    "start": 10,

    "count": 10

    })

    headers = {

    'Content-Type': 'application/json',

    'Authorization': 'Bearer aaaaaaaaaaaaa'

    }

    response = requests.request("POST", url, headers=headers, data=payload)

    print(response.text)

     

    Second you can do batch request which means, you can call two different endpoint in one call. In your case it's not needed I guess but you can still use it. If I remember correctly, in one batch request you can call 50 different endpoint in one call. You need to use multipart/mixed request with some unique boundry number(anything you want). And then place all of your requests in the body. Here is an example:

    import requests

    url = "https://yourclientdomain.com/s/-/dw/batch?client_id=yourclientid"

    payload = "--23dh3f9f4\r\nx-dw-content-id: update_customer_1\r\nx-dw-http-method: PATCH\r\nx-dw-resource-path-extension: customer_lists/EUR/customers/00143509\r\n\r\n{\"c_tags\" : [\"test1multiupdate\"]}\r\n--23dh3f9f4\r\nx-dw-content-id: update_customer_2\r\nx-dw-http-method: PATCH\r\nx-dw-resource-path-extension: customer_lists/EUR/customers/00193009\r\n\r\n{\"c_tags\" : [\"test2multiupdate\"]}\r\n--23dh3f9f4--"

    headers = {

    'Content-Type': 'multipart/mixed; boundary=23dh3f9f4',

    'x-dw-client-id': 'your client id',

    'x-dw-http-method': 'GET',

    'x-dw-resource-path': '/s/-/dw/data/v19_1/',

    'Authorization': 'Bearer yourbearertoken'

    }

    response = requests.request("POST", url, headers=headers, data=payload)

    print(response.text)

    I hope this will help your cause :) 

0/9000