Skip to content

Instantly share code, notes, and snippets.

@Ergin008
Last active December 17, 2015 17:19
Show Gist options
  • Save Ergin008/5645090 to your computer and use it in GitHub Desktop.
Save Ergin008/5645090 to your computer and use it in GitHub Desktop.
Get Status of a set of envelopes based on date and status filter
//
// API Walkthrough 5 - Get Status of Envelopes using Date and Status filter
//
// To run this sample:
// 1. Copy the below code into your iOS project
// 2. Enter your email, password, and integrator key and save
// 3. Run the code
//
// Enter your info:
NSString *email = @"<#email#>";
NSString *password = @"<#password#>";
NSString *integratorKey = @"<#integratorKey#>";
- (void)getStatusOfEnvelopes
{
///////////////////////////////////////////////////////////////////////////////////////
// STEP 1 - Login (retrieves accountId and baseUrl)
///////////////////////////////////////////////////////////////////////////////////////
// endpoint for the Login api
NSString *loginURL = @"https://demo.docusign.net/restapi/v2/login_information";
// initialize a request object with the following url, method, and body (no body for login api call)
NSMutableURLRequest *loginRequest = [self initializeRequest:loginURL setMethod:@"GET" setBody:nil];
// add content-type and authorization headers
[self addRequestHeaders:loginRequest];
//--> make an asynchronous web request
[NSURLConnection sendAsynchronousRequest:loginRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *loginResponse, NSData *loginData, NSError *loginError) {
// create NSDictionary out of JSON data that is returned
NSDictionary *responseDictionary = [self getHttpResponse:loginRequest response:loginResponse data:loginData error:loginError];
if( responseDictionary == nil)
return;
// parse the accountId and baseUrl from the response and use in the next request
NSString *accountId = responseDictionary[@"loginAccounts"][0][@"accountId"];
NSString *baseUrl = responseDictionary[@"loginAccounts"][0][@"baseUrl"];
//--- display results
NSLog(@"\naccountId = %@\nbaseUrl = %@\n", accountId, baseUrl);
///////////////////////////////////////////////////////////////////////////////////////
// STEP 2 - Get Status of Envelopes (using a date and status filter)
///////////////////////////////////////////////////////////////////////////////////////
// create a date url parameter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:-60*60*24*7]]; // 7 days ago
// append /envelopes URI to baseUrl, then append a date and status filter in following format:
// /envelopes?from_date=yyyy-MM-dd&status=created,sent,delivered,signed,completed
NSString *envelopesURL = [NSString stringWithFormat:@"%@/envelopes?from_date=%@&status=created,sent,delivered,signed,completed", baseUrl, dateString];
// initialize a request object with the following url, method, and body (no body for this api call)
NSMutableURLRequest *envelopesRequest = [self initializeRequest:envelopesURL setMethod:@"GET" setBody:nil];
// add content-type and authorization headers
[self addRequestHeaders:envelopesRequest];
[NSURLConnection sendAsynchronousRequest:envelopesRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *envelopesResponse, NSData *envelopesData, NSError *envelopesError) {
NSError *envelopesJSONError = nil;
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:envelopesData options:kNilOptions error:&envelopesJSONError];
if (envelopesError){
NSLog(@"Error sending request: %@. Got response: %@", envelopesRequest, envelopesResponse);
NSLog( @"Response = %@", jsonResponse );
return;
}
NSLog( @"Response is:\n%@", jsonResponse);
}];
}];
}
//***********************************************************************************************
// --- HELPER FUNCTIONS ---
//***********************************************************************************************
- (NSMutableURLRequest *) initializeRequest:(NSString *) url setMethod:(NSString *) method setBody:(NSData *) body
{
// create a request using the passed parameters for url, method, and body
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:method];
[request setHTTPBody:body];
[request setURL:[NSURL URLWithString:url]];
return request;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void) addRequestHeaders:(NSMutableURLRequest *) request
{
// set JSON formatted X-DocuSign-Authentication header (XML format also accepted)
NSDictionary *authenticationHeader = @{ @"Username": email, @"Password" : password, @"IntegratorKey" : integratorKey };
// add the auth header to the request object
[request setValue:[self jsonStringFromObject:authenticationHeader] forHTTPHeaderField:@"X-DocuSign-Authentication"];
// also set the Content-Type header (other accepted type is application/xml)
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (NSDictionary *) getHttpResponse:(NSMutableURLRequest *) request response:(NSURLResponse *) response data:(NSData *) data error:(NSError *) error
{
if (error) {
NSLog(@"Error sending request %@\n. Got Response %@\n Error is: %@\n", request, response, error);
return nil;
}
// we use NSJSONSerialization to parse the JSON formatted response
NSError *jsonError = nil;
return [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString *)jsonStringFromObject:(id)object {
NSString *string = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:object options:0 error:nil] encoding:NSUTF8StringEncoding];
return string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment