Update Values In Object Store Using Anypoint Platform Object Store APIs

Author: Shyam Kulkarni & Dhanashree Zope

In this blog, we will see how we can update key-value in Object Store through Object Store APIs.

For making it easy, I have created a Postman Collection which has been automated to perform the operations on the Object Store. We will see it in detail, as we progress.

First, we will create a Mule Project, on which we will perform operations:

1. Start Anypoint Studio and Create a Mule Project, name it as “object-store-demo” At one glance, flow looks like this, we will configure it in further steps:

2. Add a listener, and configure it. Set path to “/test” and in connector configuration set port to 8081. 

3. Add a Logger which logs “Start of the flow” and further add a Retrieve component from the Object Store module and configure it as follows:

Here, I have kept the key name as “lastSuccessRunDate”, you can keep it as per your case.

4. Add a Transform Message and create two variables: endDate and lastExecutionRunDate. Add the following Dataweave Script in those two variables.

endDate:
%dw 2.0output application/json

now() as Date {format: “yyyy-MM-dd”}

lastExecutionRunDate:
%dw 2.0
output application/json

payload as Date {format: “yyyy-MM-dd”}

5. Add a logger to log the above two variables. In the Message section, you can add the following script:

%dw 2.0
output application/json

{
  “endDate”: vars.endDate as String, 
“lastExecutionRunDate”: vars.lastExecutionRunDate as String
}

6. Next, add a Store component from the Object Store module and configure it as follows:

Object Store Configuration is the same for Retrieve and Store Component.(Keep it as is)

7. At last add a logger, stating “End of the flow”.

8. Now, export this application, it will generate a jar and deploy the jar over cloudhub.

9.  Once deployed, navigate to Rest Client (i.e Postman) and enter the following request url: 

http://object-store-demo-sk.us-e2.cloudhub.io/test

This request URL might look different in your case, if while deploying application, you enter a different application name.

So, you will get the following response:

10. Now, again hit the same URL through Postman and you can see the retrieval date being changed (you can observe it in lastExecutionRunDate) 

Also, we are able to see the object store created.

Now, as our application is working as expected, we will move on to the main part, i.e updating the key-value in the object store through object store APIs.

You can download the Postman collection and the environment through this link:

https://drive.google.com/drive/folders/12zETVixxWGYDrVYZoa60tnLYC-fk3uuG?usp=sharing

Once, you download this collection & environment and import it in your workspace, it will appear as follows:

In the environment, you need to add org-id (Anypoint Platform’s Organization ID) and env-id (Environment specific ID e.g Sandbox, Dev), which can be found over API manager’s URL which is depicted in following image:

Steps to update values in Object Store, follow below sequence of API calls:

1. Get Access Token API:

This API is used to login to Anypoint Platform and returns an access token as a response.

You need to pass your Anypoint Platform’s username and password in the body.

Also, in the Cookies section, go to Domain Allowlist section and add domain as *.mulesoft.com

Now, if you see the Pre-Request Script section, you will find this script:

const jar = pm.cookies.jar();
jar.clear(pm.request.url, function (error) {
  console.log(“Clear Cookies”);
});

What is the significance of this script? Let’s see..

Now remove this script and hit the API again. You will get “invalid csrf token” as response. 

So, to eradicate this, we need to go to the cookies section and remove .csrf 

Now, if you hit the API again, it will work fine and give you a response.

Thus, that script is used for deleting the .csrf from the cookies section every time you hit API. Hence, the manual intervention of deleting got vanished from the picture.

Now, the access token we received in the response, will be stored in the Environment’s authToken variable.

2. Get Store ID API:

This API is used to fetch the object store id.

You can see the storeId received in response is stored in Environment’s object-store-id variable. 

3. Get Partition API:

This API is used to fetch the number of partitions as part of the object store.

4. Get Key-Value API:

This API is used to fetch key value information for the particular partition.

Note: Depending on which key-value needs to be fetched, the partition-name and partition-key-name need to be updated in the Environment or directly hard-code in the URL.

Also, the value will be encoded so if we need to check the value then the value needs to be decoded first.

5. Update Key Value API:

This API is used to update key value information for the particular partition.

You need to pass key name, Value type and String Value that you want to update in the body. 

Note: Depending on which key-value needs to be fetched, the partition-name and partition-key-name need to be updated in the Environment or directly hard-code in the URL.

Now, you can see that the value got updated in the object store.

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.