Created
August 7, 2015 03:23
-
-
Save jkentjnr/c66e5d98bb5e295a58e8 to your computer and use it in GitHub Desktop.
Replicon - Execute Report via Web Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GET TIMESHEET REPORT METHOD - executes a predefined export report --------------------------------- | |
public String GetTimesheetReportExportForLast30DaysAsCSV(String[] projectUris) { | |
// Create the request body. | |
RepliconMessage.ExecuteReportRequest requestBody = new RepliconMessage.ExecuteReportRequest(); | |
requestBody.reportUri = 'urn:replicon-tenant:jamesn-monroe:report:5107f417-8bff-444b-aed9-5d5aa9bbfb1c'; | |
// Add the 30 day filter | |
requestBody.filterValues.add( | |
new RepliconMessage.ReportFilterValue('urn:replicon-tenant:jamesn-monroe:report-filter:354f94f3ac1d46518414c6c8affc3608;timesheetstartdaterangefilter', '16') | |
); | |
for (String projectUri : projectUris) { | |
requestBody.filterValues.add( | |
new RepliconMessage.ReportFilterValue('urn:replicon-tenant:jamesn-monroe:report-filter:354f94f3ac1d46518414c6c8affc3608;projectfilter', | |
projectUri.substringAfterLast(':') | |
) | |
); | |
} | |
String content = JSON.serialize(requestBody); | |
System.debug('===> REQUEST BODY: ' + content); | |
// Get GetAllProjectTeamMembers Data from Replicon | |
HttpResponse res = this.ExecuteRepliconHttpRequest( | |
Utility_Settings.getValue('ReportServiceURL') + '/GenerateReport', | |
content | |
); | |
String body = res.getBody(); | |
System.debug('===> RESPONSE BODY: ' + body); | |
RepliconMessage.ExecuteReportResponse responseData = | |
(RepliconMessage.ExecuteReportResponse)System.JSON.deserialize(res.getBody(), RepliconMessage.ExecuteReportResponse.class); | |
if (responseData != null && responseData.d != null && responseData.d.payload != null) | |
return responseData.d.payload; | |
return null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private HttpResponse ExecuteRepliconHttpRequest(String endpoint, String content) { | |
String username = Utility_Settings.getValue('Username'); // 'jamesandmonroe\\jamapi'; | |
String password = Utility_Settings.getValue('Password'); // 'FDS@34hui'; | |
// Get GetAllProjectTeamMembers Data from Replicon | |
Http http = new Http(); | |
HttpRequest req = new HttpRequest(); | |
req.setTimeout(120000); // 2 mins. | |
Blob headerValue = Blob.valueOf(username + ':' + password); | |
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue); | |
System.Debug('authorizationHeader: ' + authorizationHeader); | |
req.setHeader('Authorization', authorizationHeader); | |
req.setHeader('Content-Type', 'application/json'); | |
req.setHeader('X-Replicon-Security-Context', 'User'); | |
req.setMethod('POST'); | |
req.setEndpoint(endpoint); | |
if (content != null) { | |
System.Debug('BODY: ' + content); | |
req.setBody(content); | |
} | |
HttpResponse res = http.send(req); | |
System.Debug('res.getStatusCode(): ' + res.getStatusCode()); | |
System.Debug('res.getBody(): ' + res.getBody()); | |
if (res.getBody() == null || res.getBody().length() == 0) | |
throw new RepliconServiceException('The Replicon Web Service API returned no data. Endpoint: ' + endpoint); | |
if (res.getStatusCode() == 503) { | |
System.Debug('Service Unavailable: ' + res.getBody()); | |
throw new RepliconServiceException('A Service Unavailable error occurred accessing the Replicon Web Service API. Endpoint: ' + endpoint); | |
} | |
if (res.getStatusCode() == 500) { | |
System.Debug('InternalServerError: ' + res.getBody()); | |
throw new RepliconServiceException('An internal error occurred accessing the Replicon Web Service API. Endpoint: ' + endpoint); | |
} | |
if (res.getStatusCode() == 401) { | |
RepliconMessage.ErrorDetail ex = GetErrorDetail(res.getBody()); | |
throw new RepliconServiceException('An unauthorized error occurred accessing the Replicon Web Service API. Reason: ' + ex.reason + ' / Endpoint: ' + endpoint); | |
} | |
if (res.getStatusCode() == 400) { | |
RepliconMessage.ErrorDetail ex = GetErrorDetail(res.getBody()); | |
throw new RepliconBadUriException('An error occurred accessing the Replicon Web Service API. Reason: ' + ex.reason + ' / Endpoint: ' + endpoint, ex.details.uri); | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you left your username and password in here brother