Last active
December 17, 2015 16:49
-
-
Save Ergin008/5641352 to your computer and use it in GitHub Desktop.
Get info on envelope documents and download docs
This file contains hidden or 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
// | |
// API Walkthrough 6 - Get envelope documents info and download documents | |
// | |
// To run this sample: | |
// 1. Copy the below code into your iOS project | |
// 2. Enter your email, password, integrator key and envelopeId and save | |
// 3. Run the code | |
// | |
- (void)getDocumentInfoAndDownloadDocuments | |
{ | |
/////////////////////////////////////////////////////////////////////////////////////// | |
// STEP 1 - Login (retrieves accountId and baseUrl) | |
/////////////////////////////////////////////////////////////////////////////////////// | |
NSString *loginURL = @"https://demo.docusign.net/restapi/v2/login_information"; | |
NSMutableURLRequest *loginRequest = [[NSMutableURLRequest alloc] init]; | |
[loginRequest setHTTPMethod:@"GET"]; | |
[loginRequest setURL:[NSURL URLWithString:loginURL]]; | |
// set JSON formatted X-DocuSign-Authentication header (XML also accepted) | |
NSDictionary *authenticationHeader = @{@"Username": email, @"Password" : password, @"IntegratorKey" : integratorKey}; | |
// jsonStringFromObject() function defined further below... | |
[loginRequest setValue:[self jsonStringFromObject:authenticationHeader] forHTTPHeaderField:@"X-DocuSign-Authentication"]; | |
// also set the Content-Type header (other accepted type is application/xml) | |
[loginRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; | |
[NSURLConnection sendAsynchronousRequest:loginRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *loginResponse, NSData *loginData, NSError *loginError) { | |
if (loginError) { // succesful GET returns status 200 | |
NSLog(@"Error sending request %@. Got Response %@ Error is: %@", loginRequest, loginResponse, loginError); | |
return; | |
} | |
// we use NSJSONSerialization to parse the JSON formatted response | |
NSError *jsonError = nil; | |
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:loginData options:kNilOptions error:&jsonError]; | |
NSArray *loginArray = responseDictionary[@"loginAccounts"]; | |
// parse the accountId and baseUrl from the response (other data included) | |
NSString *accountId = loginArray[0][@"accountId"]; | |
NSString *baseUrl = loginArray[0][@"baseUrl"]; | |
//--- display results | |
NSLog(@"\naccountId = %@\nbaseUrl = %@\n", accountId, baseUrl); | |
/////////////////////////////////////////////////////////////////////////////////////// | |
// STEP 2 - Get Document Info for specified envelope | |
/////////////////////////////////////////////////////////////////////////////////////// | |
// append /envelopes/{envelopeId}/documents URI to baseUrl and use as endpoint for next request | |
NSString *documentsURL = [NSMutableString stringWithFormat:@"%@/envelopes/%@/documents", baseUrl, envelopeId]; | |
NSMutableURLRequest *documentsRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:documentsURL]]; | |
[documentsRequest setHTTPMethod:@"GET"]; | |
[documentsRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; | |
[documentsRequest setValue:[self jsonStringFromObject:authenticationHeader] forHTTPHeaderField:@"X-DocuSign-Authentication"]; | |
[NSURLConnection sendAsynchronousRequest:documentsRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *documentsResponse, NSData *documentsData, NSError *documentsError) { | |
NSError *documentsJSONError = nil; | |
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:documentsData options:kNilOptions error:&documentsJSONError]; | |
if (documentsError){ | |
NSLog(@"Error sending request: %@. Got response: %@", documentsRequest, documentsResponse); | |
NSLog( @"Response = %@", documentsResponse ); | |
return; | |
} | |
NSLog( @"Documents info for envelope is:\n%@", jsonResponse); | |
NSError *jsonError = nil; | |
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:documentsData options:kNilOptions error:&jsonError]; | |
// grab documents info for the next step... | |
NSArray *documentsArray = responseDictionary[@"envelopeDocuments"]; | |
/////////////////////////////////////////////////////////////////////////////////////// | |
// STEP 3 - Download each envelope document | |
/////////////////////////////////////////////////////////////////////////////////////// | |
NSMutableString *docUri; | |
NSMutableString *docName; | |
NSMutableString *docURL; | |
// loop through each document uri and download each doc (including the envelope's certificate) | |
for (int i = 0; i < [documentsArray count]; i++) | |
{ | |
docUri = [documentsArray[i] objectForKey:@"uri"]; | |
docName = [documentsArray[i] objectForKey:@"name"]; | |
docURL = [NSMutableString stringWithFormat: @"%@/%@", baseUrl, docUri]; | |
[documentsRequest setHTTPMethod:@"GET"]; | |
[documentsRequest setURL:[NSURL URLWithString:docURL]]; | |
[documentsRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; | |
[documentsRequest setValue:[self jsonStringFromObject:authenticationHeader] forHTTPHeaderField:@"X-DocuSign-Authentication"]; | |
NSError *error = [[NSError alloc] init]; | |
NSHTTPURLResponse *responseCode = nil; | |
NSData *oResponseData = [NSURLConnection sendSynchronousRequest:documentsRequest returningResponse:&responseCode error:&error]; | |
NSMutableString *jsonResponse = [[NSMutableString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding]; | |
if([responseCode statusCode] != 200){ | |
NSLog(@"Error sending %@ request to %@\nHTTP status code = %i", [documentsRequest HTTPMethod], docURL, (int)[responseCode statusCode]); | |
NSLog( @"Response = %@", jsonResponse ); | |
return; | |
} | |
// download the document to the same directory as this app | |
NSString *appDirectory = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent]; | |
NSMutableString *filePath = [NSMutableString stringWithFormat:@"%@/%@", appDirectory, docName]; | |
[oResponseData writeToFile:filePath atomically:YES]; | |
NSLog(@"Envelope document - %@ - has been downloaded to %@\n", docName, filePath); | |
} // end for | |
}]; | |
}]; | |
} | |
//*********************************************************************************************** | |
// --- 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