The JavaScript HTTP client pattern

The JavaScript HTTP client pattern is one of several patterns that you can use to create server-side mashups with Apigee Edge. Apigee's JavaScript Object Model allows you to wire JavaScript code directly into an API proxy flow. The object model's httpClient object allows you to make calls to backend services and retrieve results using JavaScript.

The JavaScript HTTP client pattern is only one of several ways to create mashups with Apigee API proxies. If you are prefer editing XML files, policy composition may be the best technique for you. If you prefer writing server-side JavaScript code, you can develop a JavaScript client application or a Node.js application to achieve the same results.

The JavaScript HTTP client cookbook sample illustrates this API proxy pattern. The sample uses a JavaScript HTTP client to call multiple backend services. The returned results are then collected, mashed up, and returned as a single JSON object. As you will see, the JavaScript HTTP client is attached as a policy to the ProxyEndpoint. This pattern does not require you to use a TargetEndpoint; the JavaScript HTTP client handles all communication with the backend targets.

The policy composition pattern

Policy composition is an Apigee API proxy pattern that lets you combine results from multiple backend targets into a single response using policies. In effect, policy composition lets you create mashups using policies.

Policy composition is only one of several ways to create mashups with Apigee API proxies. If you prefer editing XML files to writing code, then policy composition may be the best technique for you. If you prefer writing server-side JavaScript code, you can develop a JavaScript client application or a Node.js application to achieve the same results.

At the heart of the policy composition pattern is the ServiceCallout policy. The ServiceCallout policy acts as an HTTP client to the backend API or service. To be completely functional, however, a ServiceCallout relies on other policies to enable meaningful interaction with a remote API or service. Other policies are used to generate the request message that is sent by the ServiceCallout and to parse the content in the response message from the remote API or service.

The policy composition cookbook example uses three policies: AssignMessage, ServiceCallout, and ExtractVariables. These policies encapsulate the request message(s) you wish to send to the backend, the client that routes the message to the backend, and a message parser that parses the response from each API and assigns variables for the response data. After all the calls have been made, the collected variables are combined and returned in a single JSON object.

ServiceCallout: References a message created by the AssignMessage policy, defines a target URL for the external call, and defines a name for the response object that the target service returns.

ExtractVariables: Typically defines a JSONPath or XPath expression that parses the message generated by the preceding ServiceCallout policy. The policy then sets variables containing the values parsed from the ServiceCallout response.