Skip to content

Instantly share code, notes, and snippets.

@peterknolle
Created December 4, 2014 01:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save peterknolle/bf9d1811b1256651f8d0 to your computer and use it in GitHub Desktop.
Save peterknolle/bf9d1811b1256651f8d0 to your computer and use it in GitHub Desktop.
<aura:component controller="paura.RestRequestController">
<aura:attribute name="path" type="String" required="true" />
<aura:attribute name="method" type="String" default="GET"/>
<aura:attribute name="responseFormat" type="String" default="application/json"/>
<aura:attribute name="bodyContent" type="Object" />
<aura:attribute name="bodyContentType" type="String" default="application/json"/>
<aura:handler name="init" value="{!this}" action="{!c.makeRequest}"/>
<aura:registerEvent name="restResponseEvent" type="paura:restResponseEvent"/>
</aura:component>
public class RestRequestController {
@AuraEnabled
public static Response service(String path, String method, String responseFormat, String bodyContent, String bodyContentType) {
HttpRequest request = buildRequest(path, method, responseFormat, bodyContent, bodyContentType);
HttpResponse httpRes = sendRequest(request);
Response restRes = buildResponse(httpRes);
return restRes;
}
private static HttpRequest buildRequest(String path, String method, String responseFormat, String bodyContent, String bodyContentType) {
HttpRequest request = new HttpRequest();
request.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + path);
request.setMethod(method);
if (bodyContent != null) {
request.setBody(bodyContent);
request.setHeader('Content-Type', bodyContentType);
}
request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
request.setHeader('ACCEPT', responseFormat);
return request;
}
private static HttpResponse sendRequest(HttpRequest request) {
return new Http().send(request);
}
private static Response buildResponse(HttpResponse httpRes) {
Response restRes = new Response();
restRes.status = httpRes.getStatus();
restRes.statusCode = httpRes.getStatusCode();
restRes.body = httpRes.getBody();
return restRes;
}
public class Response {
@AuraEnabled
public String status { get; set; }
@AuraEnabled
public Integer statusCode { get; set; }
@AuraEnabled
public String body { get; set; }
}
}
({
makeRequest : function(component, event, helper) {
var action = component.get("c.service");
action.setParams({
path: component.get("v.path"),
method: component.get("v.method"),
responseFormat: component.get("v.responseFormat"),
bodyContent: component.get("v.bodyContent"),
bodyContentType: component.get("v.bodyContentType")
});
action.setCallback(this, function(a) {
component.getEvent("restResponseEvent").setParams({
response: a.getReturnValue()
}).fire();
});
$A.enqueueAction(action);
}
})
<aura:event type="COMPONENT" description="Event fired when a response from a restRequest is available">
<aura:attribute name="response" type="Object" description="The response" />
</aura:event>
<aura:application>
<!-- DYNAMIC INVOCATION
<select aura:id="reportId" onchange="{!c.setPath}">
<option value="00Oi0000006qd21">Owners</option>
<option value="00Oi000007sq320">Web Leads per Day</option>
</select>
-->
<paura:restRequest aura:id="restCmp"
path="/services/data/v32.0/analytics/reports/00Oi0000006qd21/describe"
restResponseEvent="{!c.handleResponse}"
/>
<!-- INSERT
<paura:restRequest
path="/services/data/v32.0/sobjects/Account/"
method="POST"
bodyContent="{ &quot;Name&quot; : &quot;A new Account&quot; }"
restResponseEvent="{!c.handleResponse}"
/>
-->
<!-- DYNAMIC COMPONENT CREATION
<ui:button label="Describe the report" press="{!c.describeTheReport}"/>
-->
</aura:application>
({
handleResponse: function(component, event, helper) {
var response = event.getParam("response");
var status = response.status;
var statusCode = response.statusCode;
var body = response.body;
// do something interesting...
//
console.log(body);
},
setPath: function(component, event, helper) {
var selectElem = component.find("reportId").getElement();
var reptId = selectElem.options[selectElem.selectedIndex].value;
var restCmp = component.find("restCmp");
restCmp.set("v.path", "/services/data/v32.0/analytics/reports/" + reptId + "/describe");
var a = restCmp.get("c.makeRequest");
$A.enqueueAction(a);
},
describeTheReport: function(component, event, helper) {
var componentConfig = {
componentDef : "markup://paura:restRequest",
attributes : {
values : {
path : "/services/data/v32.0/analytics/reports/00Oi0000006qd21/describe",
}
}
};
$A.componentService.newComponentAsync(
this,
function(newCmp) {
newCmp.addHandler("restResponseEvent", component, "c.handleResponse");
var a = newCmp.get("c.makeRequest");
$A.enqueueAction(a);
},
componentConfig
);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment