Announcement

Collapse
No announcement yet.

What's the SMT code to call a JSON API function??!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    What's the SMT code to call a JSON API function??!

    What is the SMT code to call a function in the JSON API??!
    I know this seems like a dumb question with all the docs and everything but this key ingredient to my success is missing (at least to me!)
    I have done the MivaLearn JSON API course! This is not in it.

    This is kind of where I'm at but totally not working yet because it's all wrong lol. Please help.

    Code:
    <mvt:capture variable="g.myJSONtoSend">
    {
        "Store_Code": "batman",
        "Function": "CustomerList_Load_Query",
        "Count": 10,
        "Offset": 0
    }
    </mvt:capture>
    
    <mvt:assign name="g.jsonURI" value="'https://mydevsite.mivamerchant.net/mm5/json.mvc?Store_Code=batman&Function=CustomerList_Load_Query'" />
    <mvt:assign name="g.line" value="asciichar( 13 ) $ asciichar( 10 )" />
    
    <mvt:assign name="g.headers" value=" 'Content-Type: application/json' $ g.line " />
    <mvt:assign name="g.headers" value=" g.headers $ 'X-Miva-API-Authorization: MIVA MYTOKENAUTHCODEIsHERE' $ g.line " />
    <mvt:assign name="g.headers" value=" g.headers $ 'Content-Length: 605' $ g.line " />
    
    ... so do I use mvt:call? add my g.myJSONtoSend var to the headers? So not sure what's next here.
    
    <mvt:call action="g.jsonURI" method="'POST'" headers="'g.headers'">
        <mvt:eval expr="s.callvalue" />
    </mvt:call>
    Last edited by dreamingdigital; 03-05-19, 03:53 PM.
    Colin Puttick
    Miva Web Developer @ Glendale Designs

    #2
    Brennan Can you please provide a proof of concept here. Doens't have to be my example. Just provide everything in SMT code please like my try, not just the JSON data parts.
    Colin Puttick
    Miva Web Developer @ Glendale Designs

    Comment


      #3
      OR is it like this if doing it via SMT?
      Code:
      <mvt:capture variable="g.myJSONtoSend">
      {
      "Store_Code": "batman",
      "Function": "CustomerList_Load_Query",
      "Count": 10,
      "Offset": 0
      }
      </mvt:capture>
      
      <mvt:do file="g.Module_Feature_CUS_JSON" name="l.success" value="JSON_CustomerList_Load_Query(g.myJSONtoSend )" />

      then do a json decode on l.success?
      Colin Puttick
      Miva Web Developer @ Glendale Designs

      Comment


        #4
        Here is the sample code to call the JSON API from the templates using mvt:call:

        https://docs.miva.com/code-samples/a...-template-code

        We'll get this linked in MivaLearn as well

        With mvt:capture now supported in the templates you can replace the big escaped mvt:assign statement in the example with mvt:capture. It would be cleaner, although both work.
        Brennan Heyde
        VP Product
        Miva, Inc.
        [email protected]
        https://www.miva.com

        Comment


          #5
          Thank you! I will give that a try. So looks like I was mostly on track to begin with :) yay.
          Colin Puttick
          Miva Web Developer @ Glendale Designs

          Comment


            #6
            I'm going to post some working code based on the above but has the functions here so the search on the forum works. I formatted a few things to make them more obvious and added the testing output vars for easier programming. Uses this loading customers function: https://docs.miva.com/json-api/funct...ist_load_query and only loads the ones that have a customer custom field value of not 1 in the PROFILE_PRIVATE custom field.



            Code:
            <mvt:assign name="g.json_function"  value ="'CustomerList_Load_Query'" />
            <mvt:assign name="g.access_token"   value ="'PUT_YOUR_ACCESS_TOKEN_HERE'" />
            <mvt:assign name="g.signature"      value ="crypto_base64_decode('PUT_YOUR_SIGNATURE_FOR_THAT_ACCESS_TOKEN_HERE')" />
            <mvt:assign name="g.timestamp"         value="' \"Miva_Request_Timestamp\" : ' $ '\"' $ s.time_t $ '\",' " />
            <mvt:assign name="g.endpoint"       value ="'https://' $ g.domain:name $ '/mm5/json.mvc'" />
            <mvt:capture variable="g.json_data">
            {
                <mvt:eval expr="g.timestamp" />
                "Store_Code":     "&mvtj:store:code;",
                "Function":     "&mvtj:global:json_function;",
                "Count":         "15",
                "Offset":         "0",
                "Sort":         "-dt_created",
                "Filter":[
                   {
                      "name":"search",
                      "value":[
                         {
                            "field":"CustomField_Values:customfields:PROFILE_PRIVATE",
                            "operator":"NE",
                            "value":"1"
                         }
                      ]
                   }
                ]
            }
            </mvt:capture>
            <mvt:assign name="l.ok" value="crypto_hmac_sha256(g.json_data,g.signature,'binary',g.hmac_response)" />
            <mvt:assign name="g.b64encoded_hmac_response" value="crypto_base64_encode(g.hmac_response)" />  
            <mvt:assign name="g.headers" value="'X-Miva-API-Authorization: MIVA-HMAC-SHA256 ' $ g.access_token $ ':' $ g.b64encoded_hmac_response $ asciichar( 13 ) $ asciichar( 10 )" />
            
            Store and Endpoint:
            <pre>&mvt:store:code; : &mvt:global:endpoint;</pre>
            
            Headers:
            <pre>&mvt:global:headers;</pre>
            
            JSON Data:
            <pre>&mvt:global:json_data;</pre>
            
            <mvt:call action="g.endpoint" method="'RAW'" headers="g.headers" content-type="'application/json'" fields="'g.json_data'">
                <mvt:assign name="g.response" value="g.response $ s.callvalue" />
            </mvt:call>
            
            Response:
            <pre>&mvt:global:response;</pre>
            Colin Puttick
            Miva Web Developer @ Glendale Designs

            Comment


              #7
              [edit: I had another "Oh duh!" moment when I realized my site is password protected and this was causing the problem I posted about.
              I'm leaving this here in case anyone else get an "authorization required" error and this might help them]

              I'm trying to adapt dreamingdigital's example to flush the transients created by Tess's transients module.
              I have this setup as a page on the website that I want it to work on.
              I worked out the access denied errors and now I"m getting "authorization required."

              I'm guessing it has something to do with the transients module and maybe group permissions? Any ideas?


              here is my code with the token and sig redacted.

              Code:
              <mvt:assign name="g.json_function"  value ="'Delete_Transient'" />
              <mvt:assign name="g.access_token"   value ="'xxxxxxx'" />
              <mvt:assign name="g.signature"      value ="crypto_base64_decode('xxxxxx)" />
              <mvt:assign name="g.timestamp"         value="' \"Miva_Request_Timestamp\" : ' $ '\"' $ s.time_t $ '\",' " />
              <mvt:assign name="g.endpoint"       value ="g.domain:base_surl $ 'json.mvc'" />
              <mvt:capture variable="g.json_data">
              {
                  <mvt:eval expr="g.timestamp" />
                  "Store_Code":     "&mvtj:store:code;",
                  "Function":     "Module",
                  "Module_Code": "tg_transients",
                  "Module_Function": "Delete_All_Transients"
              }
              </mvt:capture>
              <mvt:assign name="l.ok" value="crypto_hmac_sha256(g.json_data,g.signature,'binary',g.hmac_response)" />
              <mvt:assign name="g.b64encoded_hmac_response" value="crypto_base64_encode(g.hmac_response)" />  
              <mvt:assign name="g.headers" value="'X-Miva-API-Authorization: MIVA-HMAC-SHA256 ' $ g.access_token $ ':' $ g.b64encoded_hmac_response $ asciichar( 13 ) $ asciichar( 10 )" />
              
              Store and Endpoint:
              <pre>&mvt:store:code; : &mvt:global:endpoint;</pre>
              
              Headers:
              <pre>&mvt:global:headers;</pre>
              
              JSON Data:
              <pre>&mvt:global:json_data;</pre>
              
              <mvt:call action="g.endpoint" method="'RAW'" headers="g.headers" content-type="'application/json'" fields="'g.json_data'">
                  <mvt:assign name="g.response" value="g.response $ s.callvalue" />
              </mvt:call>
              
              Response:
              <pre>&mvt:global:response;</pre>
              Last edited by kayakbabe; 07-12-19, 09:17 PM. Reason: edited to insert the cause of the problem

              Comment

              Working...
              X