MuleSoft

Object Store V2 REST API

Introduction

What is Object Store v2 Rest API?

The Object Store v2 APIs allow you to use REST for the following operations:

  • Retrieve a list of object stores and their associated keys for a given application.
  • Store and access key-value pairs within an object store.
  • Delete key-value pairs from an object store.
  • Access usage statistics for Object Store within your organization.

Object Store V2 REST API Anypoint exchange link :

https://anypoint.mulesoft.com/exchange/portals/anypoint-platform/f1e97bc6-315a-4490-82a7-23abe036327a.anypoint-platform/object-store-v2

Implementation

Step 1:

To use the Object Store V2 REST API we need to have an application deployed in CloudHub with object store used in it.

Step2:

Open the application you have deployed in Runtime Manager. Click on the object store



 

You can see no partitions are showing now we need to add some data to each partition to create them.

Default partition

A default partition is created and value is added to it.

Partition1

Another partition (partition1) is created and value is added to it.

Partition2

A partition (partition2) is created and value is added to it.

Step3:

To use the Object Store v2 API it requires authentication with the Anypoint Platform Access Management API using client credentials.

So first we need to Obtain the OAuth token and send that token to the Object Store v2 API header and then only we can get the response.

The first operation we are going to do is to fetch stores

Step4:

Create an application and give any name of your choice. I have given Obj-v2-API.

Now drag and drop a listener on the message flow and configure its port to 8081 and path to /getStores

Now drag and drop an HTTP Request connector on the flow.

Step5:

Paste the fetch token endpoint (https://anypoint.mulesoft.com/accounts/oauth2/token) in the URL section of the HTTP request. And change the method to post.

Step6:

Now go to your Anypoint platform account Runtime Manager and open the application that has the object store in it. Open the object store page.

Now click on the show client credential button.

After clicking it the client id and secret will be displayed.

Step7:

Now copy and paste the client id and secret of your object store in the below data weave code.

%dw 2.0

output application/x-www-form-urlencoded

{

    client_id:” “,

    client_secret:” “,

    grant_type:”client_credentials”

}

To fetch the token we need to send a body that contains the client id and secret and the grant type. For this endpoint, the grant type is client credentials. And the body needs to be sent in x-www-form-urlencoded format.

After putting the client id and secret in the above data weave code copy and paste it into the body of the HTTP Request connector.

Now go to the advance tab of the HTTP Request. And set

Target Variable – access_token

Target Values – payload.access_token

Step8:

Now we need the configure the endpoint to get the stores. The endpoint is given below.
https://object-store-us-east-1.anypoint.mulesoft.com/api/v1/organizations/{organizationId}/environments/{environmentd}/stores

Now we need the organization Id, environment Id and the region-specific domain.

To get the organization Id and environment Id go to Access management then to Business Groups then select the business group.

After selecting the Business group in my case TGH go to Environments and select sandbox

From the URL you will get the organization Id and environment Id

Now we need the domain

First, check in which region the application is deployed. In my case it is us-east2

So for us-e2 the domain is “object-store-us-east-2.anypoint.mulesoft.com”. To find yours you can navigate to the “Access the Object Store v2 API” section in the MuleSoft documentation.

Link for MuleSoft documentation – https://docs.mulesoft.com/object-store/osv2-apis#object-store-v2-api

Step9:

Now, drag and drop an HTTP request connector into the flow. Add a global configuration, paste the domain into the “Host” section, change the protocol to HTTPS, set the port to 443, and rename it to “HTTP_V2_API_Request_Configuration.”

Then click on okay

Now replace the placeholders of “organizationId” and “environmentId” in the below resource path with the values that you got from access management. My resource path looks like this.

/api/v1/organizations/3121bc2f-8c0f-4a2e-8694-9fdd74dfdea9/environments/6ae57df1-1786-48cc-98e3-e8f5b323f467/stores

Now copy and paste this resource path in the path section of the second HTTP request.

Now copy and paste the below data weave code in the headers section. First, turn on the expression mode and then paste it.

%dw 2.0

output application/java

{

            “Authorization” : “bearer ” ++ vars.access_token

}

Now save and run the project.

After the application is deployed hit the endpoint “http://localhost:8081/getStores” using Postman and keep the method as “GET”.

You will get a response like the above picture. From here we need the storeId.

Step10:

Now we will use the 1st storeId (“APP_object-store-app__defaultPersistentObjectStore”) because it is the storeId for the application we deployed. It contains the name of the application in the id

Now we are going to create a flow to insert some values in the Partition1.

Drag and drop a listener on the flow and configure its path to /putString.

Now copy the Token HTTP Request connector paste it into the new flow and rename it to “Get Token” also rename the previous connector to “Get Token” It self.

Now drag and drop a transform message on the new flow. Copy and paste the below data weave code in it.

%dw 2.0

output application/json

{

            “keyId”: “1”,

            “stringValue”: “TEST1”,

            “valueType”:”STRING”

}

We need to send in payload “keyId” which contains the key “stringValue” which contains the string value and “valueType” which contains the type of the value ex string.

Step11:

Now drag and drop another HTTP Request connector and refer to the existing global configuration. Rename it to “Put String”.

Now the host and ports are already configured we need to just configure the resource path

This is the resource path we should configure /api/v1/organizations/{organizationId}/environments/{environmentId}/stores/{storeId}/partitions/{partitionId}/keys/{keyId}

Replace the placeholders with the values. After configuration, my resource path looks like.

“/api/v1/organizations/3121bc2f-8c0f-4a2e-8694-9fdd74dfdea9/environments/6ae57df1-1786-48cc-98e3-e8f5b323f467/stores/APP_object-store-app__defaultPersistentObjectStore/partitions/Partition1/keys/” ++ payload.keyId as String

Now copy and paste this resource path into the path section of the HTTP request.

Now paste the below data weave code in the header section.

%dw 2.0

output application/java

{

            “Authorization” : “bearer ” ++ vars.access_token

}

And change the method to Put.

Now save the application and run it. Once it is deployed hit this endpoint http://localhost:8081/putString keep the method as Get.

The response is a success.

The data is added to the object store it can be seen in the runtime manager.

Here you can see the data stored using Object Store V2 API is visible but when stored using the connector it is not visible says Binary

The value is binary because Mule 4 wraps values in a Mule object, which causes them to be visible only as binary in the Anypoint Platform.

Step12:

Now let’s store some Number value

Edit the transform message and replace it with the below data weave code.

%dw 2.0

output application/json

{

            “keyId”: “2”,

            “numberValue”: 123,

            “valueType”:”NUMBER”

}

Now save and run the project. Once it is deployed hit the http://localhost:8081/putString endpoint

You will see the response in Postman is a success

And data is added to the object store.

Step13:

Now let’s fetch all the keys in a partition. We will fetch from partition1.

For this, we don’t need to make many changes. So we are going to copy and paste the put string flow and make the changes there.

Duplicate the put string flow

Please change the connectors and the flow names by referring to the image below for better understanding.

Just replace the resource path of the “Get All keys”  HTTP Request with the resource path given below.

“/api/v1/organizations/3121bc2f-8c0f-4a2e-8694-9fdd74dfdea9/environments/6ae57df1-1786-48cc-98e3-e8f5b323f467/stores/APP_object-store-app__defaultPersistentObjectStore/partitions/Partition1/keys”

Change the method to “Get”.

Then change the Lister path to /getAllKeys

Save and run the project. When the project is successfully deployed hit the below URL

http://localhost:8081/getAllKeys

You will get a response as shown in the image below. 

Step14:

Now let’s check another endpoint that fetches all the partitions in an application. To implement this again copy-paste the “Get-All-Keys-Flow”. Change the name of the flow and connectors according to the image below.

Change the listener’s path to “/getAllPartitions”

Replace the resource path in the “Get All Partitions” HTTP Request connector with the resource path given below.

“/api/v1/organizations/3121bc2f-8c0f-4a2e-8694-9fdd74dfdea9/environments/6ae57df1-1786-48cc-98e3-e8f5b323f467/stores/APP_object-store-app__defaultPersistentObjectStore/partitions”

Now save and run the project. After the project is redeployed hit the endpoint  http://localhost:8081/getAllPartitions and keep the Method as Get.

You will get a successful response with a list of the object store partitions in an application.

You can refer to the exchange to explore more endpoints of Object Store V2 REST API.

Here is the link –

https://anypoint.mulesoft.com/exchange/portals/anypoint-platform/f1e97bc6-315a-4490-82a7-23abe036327a.anypoint-platform/object-store-v2

Author

Yuvraj Sinha

Leave a comment

Your email address will not be published. Required fields are marked *