Forum Discussion

EDO_01_1789's avatar
EDO_01_1789
Helper I
4 years ago
Solved

convert python code to M inside query

Hello everyone,

would anyone be able to help me translate this code which is written in python to M language in power query.

I can't convert

 

import http.client

conn = http.client.HTTPConnection("app.salsify.com")

payload = "{\n    \"configuration\": {\n        \"entity_type\": \"all\",\n        \"filter\": \"=list:default\",\n        \"properties\": \"'UPC','Product Name','Brand','Main Image','Short Description','Long Description'\",\n        \"include_all_columns\": false,\n        \"sort_order\": \"desc\",\n        \"sort_property\": \"Brand\"\n    }\n}"

headers = {
    'Content-Type': "application/json",
    'Authorization': "Bearer s-999-999-999-999",
    'User-Agent': "PostmanRuntime/7.19.0",
    'Accept': "*/*",
    'Cache-Control': "no-cache",
    'Host': "app.salsify.com",
    'Accept-Encoding': "gzip, deflate",
    'Content-Length': "304",
    'Cookie': "_cookie_id",
    'Connection': "keep-alive",
    'cache-control': "no-cache"
    }

conn.request("POST", "api,orgs,s-999-999-999-999,export_runs", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))

# where org_id = s-999-999-999-999, api_token = YOUR-AUTH-TOKEN, and cookie id = _cookie_id

 

thank you for your help.

  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi EDO_01_1789,

    Any update on this? Did the above suggestions help with your scenario? if that is the case, you can consider Kudo or accept the helpful suggestions to help others who faced similar requirements.

    If these also don't help, please share more detailed information to help us clarify your scenario to test.

    How to Get Your Question Answered Quickly 

    Regards,

    Xiaoxin Sheng

8 Replies

  • You use the regular Web.Contents() call and specify the payload in the Content  section.

     

    let
        url = "https://app.salsify.com",
        payload = "{\""configuration\"": {\""entity_type\"": \""all\"",\""filter\"": \""=list:default\"",\""properties\"": \""'UPC','Product Name','Brand','Main Image','Short Description','Long Description'\"",\""include_all_columns\"": false,\""sort_order\"": \""desc\"",\""sort_property\"": \""Brand\""}}",
        Source = Web.Contents(url,[Headers=[#"Content-Type"= "application/json",Authorization = "Bearer s-999-999-999-999"],Content=Text.ToBinary(payload)])
    in
        Source

     

    Without access to the API that's all the help we can provide.

    • EDO_01_1789's avatar
      EDO_01_1789
      Helper I

      Hello lbendlin 
      sorry for taking time to answer you.
      I share with you below the link of the site which gives you more information about the API. https://developers.salsify.com/reference/get-export-status

      I've been trying to connect to the site for more than a week.

      The site tells me to use
      org_id : in our example we will use ABCDE
      export_id : in our example we will use XYZ

      here is the code that the site generates for me in python and I would like to transform it into M language in the query.

      is this possible ?

      import http.client
      
      conn = http.client.HTTPConnection("app.salsify.com")
      
      payload = ""
      
      headers = {
          'Content-Type': "application/json",
          'Authorization': "Bearer YOUR-AUTH-TOKEN",
          'User-Agent': "PostmanRuntime/7.19.0",
          'Accept': "*/*",
          'Cache-Control': "no-cache",
          'Host': "app.salsify.com",
          'Accept-Encoding': "gzip, deflate",
          'Cookie': "_cookie_id",
          'Connection': "keep-alive",
          'cache-control': "no-cache"
          }
      
      conn.request("GET", "api,orgs,s-999-999-999-999,export_runs,19350473", payload, headers)
      
      res = conn.getresponse()
      data = res.read()
      
      print(data.decode("utf-8"))
      
      # where export_id = 19350473 org_id = s-999-999-999-999, api_token = YOUR-AUTH-TOKEN, cookie id = _cookie_id

      thanks for your help

       

      • lbendlin's avatar
        lbendlin
        Super User

        The process is the same as in my previous response, minus the payload. You switch between POST and GET by including a content option (or not).

  • lbendlin ,

     

    I just tried this code and it doesn't work
    here is my error message

     

    here is the code I pasted in the query

    let
        url = "https://app.salsify.com",
        payload = "{\""configuration\"": {\""entity_type\"": \""all\"",\""filter\"": \""=list:default\"",\""properties\"": \""'UPC','Product Name','Brand','Main Image','Short Description','Long Description'\"",\""include_all_columns\"": false,\""sort_order\"": \""desc\"",\""sort_property\"": \""Brand\""}}",
        Source = Web.Contents(url, [Headers=[#"Content-Type"="application/json", Authorization="abcde"]])
    in
        Source

     

     

    • lbendlin's avatar
      lbendlin
      Super User

      The message is clear on what you need to do.  Easiest will be to go to data source settings, remove the API source, and then refresh, specifying anonymous auth.

       

      your "authorization"  header must have the format "Bearer abcde" , not just "abcde" .

      • EDO_01_1789's avatar
        EDO_01_1789
        Helper I

        it's starting to work, but do you know why I don't have any data?

         

         

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi EDO_01_1789,

    Any update on this? Did the above suggestions help with your scenario? if that is the case, you can consider Kudo or accept the helpful suggestions to help others who faced similar requirements.

    If these also don't help, please share more detailed information to help us clarify your scenario to test.

    How to Get Your Question Answered Quickly 

    Regards,

    Xiaoxin Sheng