Sunday, 30 June 2019

Composite calls in REST API

OAUTH Flow :
===========
1. web server flow

1.Client app directs user to Salesforce authorization endpoint.
2. User logs into Salesforce page with their credentials
3. If successful login user routed to callback URL with
authorization code.
4.Client app sends authorization code to Salesforce for access token.
5.Salesforce returns an access token, refresh token, and instance URL.
6.Application uses the access token to access salesforce.

2. User - agent flow

1.client app directs user to salesforce authorization endpoint.
2. User logs into salesforce page with their credentials.
3. If successful login, user routed to callback URL with access token more.
4. Application uses the access token to access salesforce data.

3. User -password Flow

1. Client app requests token using username and password.
2. Salesforce verifies credentials and returns access token and instance url.
3. Application uses the access token to access salesforce data.

call back url :
================
it would route that authenticated user back to with either the authorization code or
event the token back in the mesage.

client-side caching :
======================
if- match header
if-None-Match headers
Requires use of an Etag(s)

if-Modified
if-Unmodified
works against individual records.

omposite calls :
================

By using composite resources we can make multiple request
in single REST API call.

1.Composite

Executes a series of REST API requests in a single call.
you can use the output of one request as the input to a
subsequent request.

/services/data/v45.0/composite


ex :


"compositeRequest":[ 
  { 
"method":"POST",
"url":"/services/data/v41.0/sobjects/Account",
"referenceId":"refAccount",
"body":{ 
"Name":"Walt Disney Account",
"BillingStreet" : "Walt Disney World Resort",
                "BillingCity" : "Orlando",
                "BillingState" : "Florida"
}
  },
  { 
"method":"POST",
"url":"/services/data/v38.0/sobjects/Contact",
"referenceId":"refAccountContact",
"body":{ 
"AccountId":"@{refAccount.id}",
"FirstName" : "Walt",
                "LastName" : "Disney"
}
  }
]
}

2. Batching Rest Resources.

Execute a set of subrequests in a single request.
Subrequests are executed independently and information
can't be passed between subrequest calls.

a. A single Batch REST request can execute upto 25 sub-requests.
b. Sub-requests contains the resource (URI) and the method to execute.
c. Each sub-request is an unrelated API call.
d. Sub-requests are executed serially, in order,and as the running user.
e.As each sub-request completes, the call is committed.
f. haltOnError - is optional parameter
   Indicates if the batch should stop on any error that is encountered.
 

   ex : post
   /services/data/v45.0/composite/batch
 
 {"batchRequests": haltonerror:true, [
  {
   "method":"GET",
   "url":"v45.0/sobjects/account/0010K0000221VRSQA2" 
  },
 {
   "method":"GET",
   "url":"v45.0/sobjects/contact/0030K00001p6tf9QAA" 
  },
  {
  "method":"GET",
  "url":"v45.0/sobjects/account/0010K0000221Va0QAE?fields=Name,BillingPostalCode"}
]}

2. TreeSave REST Resource

Creates one or more sObject trees with root records of the
specified type. A Sobject tree is a collection of nested,
parent-child records with a single root record.

a. only insert is supported
b. All records are rolled back on any error.
c. upto to a total of 200 records across all trees.
d. up to five records of different Types.
e. SObject trees up to five levels deep.

ex :

/services/data/v45.0/composite/tree/Account

   {"records":[
  {
   "attributes":{"type":"Account","referenceId":"ref1"},
   "Name":"NewTree1" 
  },
 {
    "attributes":{"type":"Account","referenceId":"ref2"},
   "Name":"NewTree2" 
  },
  {
  "attributes":{"type":"Account","referenceId":"ref3"},
   "Name":"NewTree3"
 }
]}

ex :

/services/data/v45.0/composite/tree/Account

{
"records" : [
                {
                    "attributes" : {
                        "type" : "Account",
                        "referenceId" : "DisneyAccount"
                    },
                 
                    "Name" : "Walt Disney World Resort",
                    "BillingStreet" : "Walt Disney World Resort",
                    "BillingCity" : "Orlando",
                    "BillingState" : "Florida",                   
                    "Contacts" : {
                        "records" : [
                            {
                                "attributes" : {
                                    "type" : "Contact",
                                    "referenceId" : "WaltDisneyContact"
                                },                               
                                "FirstName" : "Walt",
                                "LastName" : "Disney"
                            },
                            {
                                "attributes" : {
                                    "type" : "Contact",
                                    "referenceId" : "RoyDisneyContact"
                                },                               
                                "FirstName" : "Roy",
                                "LastName" : "Disney"
                            }
                        ]
                    }                   
                }
            ]
}

No comments:

Post a Comment