{"__v":0,"_id":"588f722cbcace50f0052ba12","category":{"version":"588f722bbcace50f0052b9e1","project":"565f5fa26bafd40d0030a064","_id":"588f722bbcace50f0052b9e4","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-12-26T05:04:48.794Z","from_sync":false,"order":2,"slug":"samples-and-tutorials","title":"How Tos & Examples"},"parentDoc":null,"project":"565f5fa26bafd40d0030a064","user":"565f3941ea46251700972783","version":{"__v":1,"_id":"588f722bbcace50f0052b9e1","project":"565f5fa26bafd40d0030a064","createdAt":"2017-01-30T17:04:43.410Z","releaseDate":"2017-01-30T17:04:43.410Z","categories":["588f722bbcace50f0052b9e2","588f722bbcace50f0052b9e3","588f722bbcace50f0052b9e4","588f722bbcace50f0052b9e5","588f722bbcace50f0052b9e6","588f722bbcace50f0052b9e7"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"5.3.0","version":"5.3.0"},"updates":["57e54d8b685f7c19007fba4a","57ea8b6b802e4a17009181a9"],"next":{"pages":[],"description":""},"createdAt":"2015-12-30T03:37:19.887Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":4,"body":"[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Important\",\n  \"body\": \"In order to run these examples, please make sure you've followed the [Quick Start Guide](doc:quick-start-guide). You'll need to have signed up with <a target=\\\"_blank\\\" href=\\\"https://dashboard.nanoscale.io/#/registration\\\">nanoscale.io</a> and downloaded the <a target=\\\"_blank\\\" href=\\\"http://downloads.justapis.com/v5.0.0/samples.json\\\">Sample APIs</a> we provide.\\n\\nThe instructions in this tutorial refer to the sample Proxy Endpoint called “Multiple remote endpoints - parallel calls” in the sample API that you imported in the Quick Start Guide steps, and assumes you have a nanoscale.io account. If you have not imported the sample API or signed up for <a target=\\\"_blank\\\" href=\\\"https://dashboard.nanoscale.io/#/registration\\\">nanoscale.io</a>, please do so now.\"\n}\n[/block]\nThis tutorial shows how to create a Proxy Endpoint with multiple Remote Endpoints called in parallel. This example demonstrates a workflow that simultaneously retrieves product data with one call and ratings with another call, and then merges the related information in a single response.\n \n## Step 1\n\nThis Proxy Endpoint requires you to first define two Remote Endpoints: “Products - GET” and “Get Ratings”.\n\nWithin the sample APIs Remote Endpoint list, find “Products - GET”, which returns a list of products, and click the row.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/xOONFLDORc6Ufc2Tawqe_samples-and-tutorials-multiple-remote-endpoints-in-parallel-step1.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-parallel-step1.png\",\n        \"3360\",\n        \"1808\",\n        \"#5389c5\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n## Step 2\n\nWhen you are in edit mode, you will see that this Remote Endpoint has a few properties that were specified when it was created.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/EkCXK29AQyS4qFcvezBM_samples-and-tutorials-multiple-remote-endpoints-in-parallel-step2.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-parallel-step2.png\",\n        \"3360\",\n        \"1770\",\n        \"#8d9b54\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n1. The Remote Endpoint’s **name** is: “Products - GET”, which is used for display purposes in the Admin Web App.\n\n2. The Remote Endpoint’s **codename** is: “getProducts”, which determines how the Remote Endpoint Request and Response Objects are referred to within JavaScript logic blocks.\n\n3. The Remote Endpoint **URL**: <a target=\"_blank\" href=\"http://justapis-mock.herokuapp.com/v1/products\">http://justapis-mock.herokuapp.com/v1/products</a>. This is the URL of the external API endpoint that you are calling.\n\n4. The **Method** of external API endpoint to validate token is GET.\n\n5. There are no **headers**, **query parameters** and **environment data** required to be set for this Remote Endpoint.\n\n## Step 3\n\nNow find the second Remote Endpoint called “Get Ratings”, which returns product ratings, and click the row.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/o0EhUWjLQPiEGTaCe455_samples-and-tutorials-multiple-remote-endpoints-in-parallel-step3.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-parallel-step3.png\",\n        \"3360\",\n        \"1470\",\n        \"#5389c5\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n## Step 4\n\nWhen you are in edit mode, you will see that this Remote Endpoint has a few properties that were specified when it was created.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/DLlzhpmUTDqGothAEip5_samples-and-tutorials-multiple-remote-endpoints-in-parallel-step4.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-parallel-step4.png\",\n        \"3332\",\n        \"1778\",\n        \"#909a52\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n1. The Remote Endpoint’s **name** is: “Get Ratings”, which is used for display purposes in the Admin Web App.\n\n2. The Remote Endpoint’s **codename** is: “getRatings”, which determines how the Remote Endpoint Request and Response Objects are referred to within JavaScript logic blocks.\n\n3. The Remote Endpoint **URL**: <a target=\"_blank\" href=\"http://justapis-mock.herokuapp.com/v1/ratings\">http://justapis-mock.herokuapp.com/v1/ratings</a>. This is the URL of the external API endpoint that you are calling.\n\n4. The **Method** of external API endpoint to validate token is GET.\n\n5. There are no **headers**, **query parameters**, and **environment data** required to be set for this Remote Endpoint.\n\n## Step 5\n\nNow you are ready to define the Proxy Endpoint that calls these two Remote Endpoints in parallel. Find the “Multiple remote endpoints - parallel calls”, which should appear in the list of Proxy Endpoints, and click the row.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/1v8Cs8iQQYih4tFzHIDi_samples-and-tutorials-multiple-remote-endpoints-in-parallel-step5.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-parallel-step5.png\",\n        \"3320\",\n        \"1124\",\n        \"#592f3d\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n## Step 6\n\nWhen you are in edit mode, you will see that this Proxy Endpoint has several properties that were specified when it was created.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/ph58W3JQEqHmTZLo1yoZ_samples-and-tutorials-multiple-remote-endpoints-in-parallel-step6.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-parallel-step6.png\",\n        \"3328\",\n        \"1654\",\n        \"#bc8e4c\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n1. The Proxy Endpoint’s **name** is: “Multiple remote endpoints - parallel calls”, which is used for display purposes in the Admin Web App.\n\n2. The **Active** status is set to on, which means that this Proxy Endpoint will run if the server receives a request for it.\n\n3. This Proxy Endpoint is assigned to the “Dev” **Environment**, and any environment variables used in will be populated from the “Dev” environment.\n\n4. This Proxy Endpoint is assigned to the “Multi-call” **Group**.\n\n5. There is one **Route** defined, which is a GET method as “/products”. This means that if the Host is organic-apparel-7319.justapis.io, the full URL for this Proxy Endpoint is <a target=\"_blank\" href=\"https://organic-apparel-7319.justapis.io/products\">https://organic-apparel-7319.justapis.io/products</a>.\n\nNote that there is one step in this Proxy Endpoint Workflow, which is a Multi-call Component, as denoted by the following icon.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/OBzzjS0TaCYE8zA39rJw_samples-and-tutorials-multiple-remote-endpoints-in-parallel-multi-call-component-icon.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-parallel-multi-call-component-icon.png\",\n        \"1280\",\n        \"70\",\n        \"#4c647c\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n## Step 7\n\nClick on the Multi-call Component icon to view the details of this step in the workflow.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/LbeeVywTUupwDaQt71oA_samples-and-tutorials-multiple-remote-endpoints-in-parallel-step7.png\",\n        \"samples-and-tutorials-multiple-remote-endpoints-in-parallel-step7.png\",\n        \"3320\",\n        \"1818\",\n        \"#4c6483\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nYou will see that this Multi-call component is comprised of several sub-steps.\n \n1. **Conditional** set to “If”, which means that any JavaScript logic specified in the Conditional logic block must evaluate to “true” to continue. Otherwise, the workflow will skip to the end.\n\n2. The Conditional logic block is empty.\n\n3. The Before Request logic block is empty.\n\n4. The Edit calls sections invokes two Remote Endpoints in parallel: “Products –GET” and “Get Ratings”.\n\n5. The After request logic block has sections of JavaScript code. This logic is executed to merge the responses from the individual Remote Endpoints into a single response.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"response.headers[\\\"Content-Type\\\"] = \\\"application/json\\\";\\nThis code sets the response to JSON string format\\n \\nproducts_response_data = JSON.parse(getProducts.response.body);\\nratings_response_data = JSON.parse(getRatings.response.body);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis code gets the responses from each Remote Endpoint using {RemoteEndpointcodeName}.response.body that was initialized when each Remote Endpoint was invoked successfully, and assigns them to variables.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var merged_response = [];\\nThis code creates a new variable for merged response\\n \\nratings_response_data.forEach(function(r){\\n  p =  _.findWhere(products_response_data, {\\\"id\\\": r.product_id});\\n  p.rating = r.rating;\\n  merged_response.push(p);\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis code merges the responses based on a match between a product and its corresponding ratings, using the product id property on each rating. The combined data is saved in a merged_response variable.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"response.body = JSON.stringify(merged_response);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis code sets the body of the final response to be the merged_response variable.\n\n## Step 8\n\nNow let us test this Proxy Endpoint. Open a new browser window or tab, and invoke the Proxy Endpoint using the following URLs (if you have changed the default Host, please use your new hostname instead).\n\n<a target=\"_blank\" href=\"https://organic-apparel-7319.justapis.io/products\">https://organic-apparel-7319.justapis.io/products</a>\n\nYou should see the resulting message in your browser window.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/UUo3vBZMRbKE3gvWI9bT_products.png\",\n        \"products.png\",\n        \"941\",\n        \"428\",\n        \"#378336\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nCongratulations, you have completed this tutorial.","excerpt":"","slug":"multiple-remote-endpoints-in-parallel","type":"basic","title":"Multiple Remote Endpoints in Parallel"}

Multiple Remote Endpoints in Parallel


[block:callout] { "type": "info", "title": "Important", "body": "In order to run these examples, please make sure you've followed the [Quick Start Guide](doc:quick-start-guide). You'll need to have signed up with <a target=\"_blank\" href=\"https://dashboard.nanoscale.io/#/registration\">nanoscale.io</a> and downloaded the <a target=\"_blank\" href=\"http://downloads.justapis.com/v5.0.0/samples.json\">Sample APIs</a> we provide.\n\nThe instructions in this tutorial refer to the sample Proxy Endpoint called “Multiple remote endpoints - parallel calls” in the sample API that you imported in the Quick Start Guide steps, and assumes you have a nanoscale.io account. If you have not imported the sample API or signed up for <a target=\"_blank\" href=\"https://dashboard.nanoscale.io/#/registration\">nanoscale.io</a>, please do so now." } [/block] This tutorial shows how to create a Proxy Endpoint with multiple Remote Endpoints called in parallel. This example demonstrates a workflow that simultaneously retrieves product data with one call and ratings with another call, and then merges the related information in a single response. ## Step 1 This Proxy Endpoint requires you to first define two Remote Endpoints: “Products - GET” and “Get Ratings”. Within the sample APIs Remote Endpoint list, find “Products - GET”, which returns a list of products, and click the row. [block:image] { "images": [ { "image": [ "https://files.readme.io/xOONFLDORc6Ufc2Tawqe_samples-and-tutorials-multiple-remote-endpoints-in-parallel-step1.png", "samples-and-tutorials-multiple-remote-endpoints-in-parallel-step1.png", "3360", "1808", "#5389c5", "" ] } ] } [/block] ## Step 2 When you are in edit mode, you will see that this Remote Endpoint has a few properties that were specified when it was created. [block:image] { "images": [ { "image": [ "https://files.readme.io/EkCXK29AQyS4qFcvezBM_samples-and-tutorials-multiple-remote-endpoints-in-parallel-step2.png", "samples-and-tutorials-multiple-remote-endpoints-in-parallel-step2.png", "3360", "1770", "#8d9b54", "" ] } ] } [/block] 1. The Remote Endpoint’s **name** is: “Products - GET”, which is used for display purposes in the Admin Web App. 2. The Remote Endpoint’s **codename** is: “getProducts”, which determines how the Remote Endpoint Request and Response Objects are referred to within JavaScript logic blocks. 3. The Remote Endpoint **URL**: <a target="_blank" href="http://justapis-mock.herokuapp.com/v1/products">http://justapis-mock.herokuapp.com/v1/products</a>. This is the URL of the external API endpoint that you are calling. 4. The **Method** of external API endpoint to validate token is GET. 5. There are no **headers**, **query parameters** and **environment data** required to be set for this Remote Endpoint. ## Step 3 Now find the second Remote Endpoint called “Get Ratings”, which returns product ratings, and click the row. [block:image] { "images": [ { "image": [ "https://files.readme.io/o0EhUWjLQPiEGTaCe455_samples-and-tutorials-multiple-remote-endpoints-in-parallel-step3.png", "samples-and-tutorials-multiple-remote-endpoints-in-parallel-step3.png", "3360", "1470", "#5389c5", "" ] } ] } [/block] ## Step 4 When you are in edit mode, you will see that this Remote Endpoint has a few properties that were specified when it was created. [block:image] { "images": [ { "image": [ "https://files.readme.io/DLlzhpmUTDqGothAEip5_samples-and-tutorials-multiple-remote-endpoints-in-parallel-step4.png", "samples-and-tutorials-multiple-remote-endpoints-in-parallel-step4.png", "3332", "1778", "#909a52", "" ] } ] } [/block] 1. The Remote Endpoint’s **name** is: “Get Ratings”, which is used for display purposes in the Admin Web App. 2. The Remote Endpoint’s **codename** is: “getRatings”, which determines how the Remote Endpoint Request and Response Objects are referred to within JavaScript logic blocks. 3. The Remote Endpoint **URL**: <a target="_blank" href="http://justapis-mock.herokuapp.com/v1/ratings">http://justapis-mock.herokuapp.com/v1/ratings</a>. This is the URL of the external API endpoint that you are calling. 4. The **Method** of external API endpoint to validate token is GET. 5. There are no **headers**, **query parameters**, and **environment data** required to be set for this Remote Endpoint. ## Step 5 Now you are ready to define the Proxy Endpoint that calls these two Remote Endpoints in parallel. Find the “Multiple remote endpoints - parallel calls”, which should appear in the list of Proxy Endpoints, and click the row. [block:image] { "images": [ { "image": [ "https://files.readme.io/1v8Cs8iQQYih4tFzHIDi_samples-and-tutorials-multiple-remote-endpoints-in-parallel-step5.png", "samples-and-tutorials-multiple-remote-endpoints-in-parallel-step5.png", "3320", "1124", "#592f3d", "" ] } ] } [/block] ## Step 6 When you are in edit mode, you will see that this Proxy Endpoint has several properties that were specified when it was created. [block:image] { "images": [ { "image": [ "https://files.readme.io/ph58W3JQEqHmTZLo1yoZ_samples-and-tutorials-multiple-remote-endpoints-in-parallel-step6.png", "samples-and-tutorials-multiple-remote-endpoints-in-parallel-step6.png", "3328", "1654", "#bc8e4c", "" ] } ] } [/block] 1. The Proxy Endpoint’s **name** is: “Multiple remote endpoints - parallel calls”, which is used for display purposes in the Admin Web App. 2. The **Active** status is set to on, which means that this Proxy Endpoint will run if the server receives a request for it. 3. This Proxy Endpoint is assigned to the “Dev” **Environment**, and any environment variables used in will be populated from the “Dev” environment. 4. This Proxy Endpoint is assigned to the “Multi-call” **Group**. 5. There is one **Route** defined, which is a GET method as “/products”. This means that if the Host is organic-apparel-7319.justapis.io, the full URL for this Proxy Endpoint is <a target="_blank" href="https://organic-apparel-7319.justapis.io/products">https://organic-apparel-7319.justapis.io/products</a>. Note that there is one step in this Proxy Endpoint Workflow, which is a Multi-call Component, as denoted by the following icon. [block:image] { "images": [ { "image": [ "https://files.readme.io/OBzzjS0TaCYE8zA39rJw_samples-and-tutorials-multiple-remote-endpoints-in-parallel-multi-call-component-icon.png", "samples-and-tutorials-multiple-remote-endpoints-in-parallel-multi-call-component-icon.png", "1280", "70", "#4c647c", "" ] } ] } [/block] ## Step 7 Click on the Multi-call Component icon to view the details of this step in the workflow. [block:image] { "images": [ { "image": [ "https://files.readme.io/LbeeVywTUupwDaQt71oA_samples-and-tutorials-multiple-remote-endpoints-in-parallel-step7.png", "samples-and-tutorials-multiple-remote-endpoints-in-parallel-step7.png", "3320", "1818", "#4c6483", "" ] } ] } [/block] You will see that this Multi-call component is comprised of several sub-steps. 1. **Conditional** set to “If”, which means that any JavaScript logic specified in the Conditional logic block must evaluate to “true” to continue. Otherwise, the workflow will skip to the end. 2. The Conditional logic block is empty. 3. The Before Request logic block is empty. 4. The Edit calls sections invokes two Remote Endpoints in parallel: “Products –GET” and “Get Ratings”. 5. The After request logic block has sections of JavaScript code. This logic is executed to merge the responses from the individual Remote Endpoints into a single response. [block:code] { "codes": [ { "code": "response.headers[\"Content-Type\"] = \"application/json\";\nThis code sets the response to JSON string format\n \nproducts_response_data = JSON.parse(getProducts.response.body);\nratings_response_data = JSON.parse(getRatings.response.body);", "language": "javascript" } ] } [/block] This code gets the responses from each Remote Endpoint using {RemoteEndpointcodeName}.response.body that was initialized when each Remote Endpoint was invoked successfully, and assigns them to variables. [block:code] { "codes": [ { "code": "var merged_response = [];\nThis code creates a new variable for merged response\n \nratings_response_data.forEach(function(r){\n p = _.findWhere(products_response_data, {\"id\": r.product_id});\n p.rating = r.rating;\n merged_response.push(p);\n});", "language": "javascript" } ] } [/block] This code merges the responses based on a match between a product and its corresponding ratings, using the product id property on each rating. The combined data is saved in a merged_response variable. [block:code] { "codes": [ { "code": "response.body = JSON.stringify(merged_response);", "language": "javascript" } ] } [/block] This code sets the body of the final response to be the merged_response variable. ## Step 8 Now let us test this Proxy Endpoint. Open a new browser window or tab, and invoke the Proxy Endpoint using the following URLs (if you have changed the default Host, please use your new hostname instead). <a target="_blank" href="https://organic-apparel-7319.justapis.io/products">https://organic-apparel-7319.justapis.io/products</a> You should see the resulting message in your browser window. [block:image] { "images": [ { "image": [ "https://files.readme.io/UUo3vBZMRbKE3gvWI9bT_products.png", "products.png", "941", "428", "#378336", "" ] } ] } [/block] Congratulations, you have completed this tutorial.