1111'use strict'
1212
1313var util = require ( 'util' )
14- var objcHelpers = require ( './helpers' )
14+ var helpers = require ( './helpers' )
1515var CodeBuilder = require ( '../../helpers/code-builder' )
1616
1717module . exports = function ( source , options ) {
@@ -21,7 +21,6 @@ module.exports = function (source, options) {
2121 pretty : true
2222 } , options )
2323
24- var indent = opts . indent
2524 var code = new CodeBuilder ( opts . indent )
2625 // Markers for headers to be created as litteral objects and later be set on the NSURLRequest if exist
2726 var req = {
@@ -34,20 +33,20 @@ module.exports = function (source, options) {
3433
3534 if ( Object . keys ( source . allHeaders ) . length ) {
3635 req . hasHeaders = true
37- code . push ( null )
38- code . push ( objcHelpers . nsDeclaration ( 'NSDictionary' , 'headers' , source . allHeaders , opts . pretty ) )
36+ code . blank ( )
37+ . push ( helpers . nsDeclaration ( 'NSDictionary' , 'headers' , source . allHeaders , opts . pretty ) )
3938 }
4039
4140 if ( source . postData . text || source . postData . jsonObj || source . postData . params ) {
4241 req . hasBody = true
4342
4443 switch ( source . postData . mimeType ) {
4544 case 'application/x-www-form-urlencoded' :
46- code . push ( null )
4745 // By appending parameters one by one in the resulting snippet,
4846 // we make it easier for the user to edit it according to his or her needs after pasting.
4947 // The user can just add/remove lines adding/removing body parameters.
50- code . push ( util . format ( 'NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];' ,
48+ code . blank ( )
49+ . push ( util . format ( 'NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];' ,
5150 source . postData . params [ 0 ] . name , source . postData . params [ 0 ] . value ) )
5251 for ( var i = 1 , len = source . postData . params . length ; i < len ; i ++ ) {
5352 code . push ( util . format ( '[postData appendData:[@"&%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];' ,
@@ -57,48 +56,51 @@ module.exports = function (source, options) {
5756
5857 case 'application/json' :
5958 if ( source . postData . jsonObj ) {
60- code . push ( objcHelpers . nsDeclaration ( 'NSDictionary' , 'parameters' , source . postData . jsonObj , opts . pretty ) )
61- code . push ( null )
62- code . push ( 'NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];' )
59+ code . push ( helpers . nsDeclaration ( 'NSDictionary' , 'parameters' , source . postData . jsonObj , opts . pretty ) )
60+ . blank ( )
61+ . push ( 'NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];' )
6362 }
6463 break
6564
6665 case 'multipart/form-data' :
67- code . push ( objcHelpers . nsDeclaration ( 'NSArray' , 'parameters' , source . postData . params , opts . pretty ) )
68- code . push ( util . format ( 'NSString *boundary = @"%s";' , source . postData . boundary ) )
69- code . push ( null )
70- code . push ( 'NSError *error;' )
71- code . push ( 'NSMutableString *body = [NSMutableString string];' )
72- code . push ( 'for (NSDictionary *param in parameters) {' )
73- code . push ( indent + '[body appendFormat:@"--%@\\r\\n", boundary];' )
74- code . push ( indent + 'if (param[@"fileName"]) {' )
75- code . push ( indent + indent + '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"; filename=\\"%@\\"\\r\\n", param[@"name"], param[@"fileName"]];' )
76- code . push ( indent + indent + '[body appendFormat:@"Content-Type: %@\\r\\n\\r\\n", param[@"contentType"]];' )
77- code . push ( indent + indent + '[body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];' )
78- code . push ( indent + indent + 'if (error) {' )
79- code . push ( indent + indent + indent + 'NSLog(@"%@", error);' )
80- code . push ( indent + indent + '}' )
81- code . push ( indent + '} else {' )
82- code . push ( indent + indent + '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"\\r\\n\\r\\n", param[@"name"]];' )
83- code . push ( indent + indent + '[body appendFormat:@"%@", param[@"value"]];' )
84- code . push ( indent + '}' )
85- code . push ( '}' )
86- code . push ( '[body appendFormat:@"\\r\\n--%@--\\r\\n", boundary];' )
87- code . push ( 'NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];' )
66+ // By appending multipart parameters one by one in the resulting snippet,
67+ // we make it easier for the user to edit it according to his or her needs after pasting.
68+ // The user can just edit the parameters NSDictionary or put this part of a snippet in a multipart builder method.
69+ code . push ( helpers . nsDeclaration ( 'NSArray' , 'parameters' , source . postData . params , opts . pretty ) )
70+ . push ( util . format ( 'NSString *boundary = @"%s";' , source . postData . boundary ) )
71+ . blank ( )
72+ . push ( 'NSError *error;' )
73+ . push ( 'NSMutableString *body = [NSMutableString string];' )
74+ . push ( 'for (NSDictionary *param in parameters) {' )
75+ . push ( 1 , '[body appendFormat:@"--%@\\r\\n", boundary];' )
76+ . push ( 1 , 'if (param[@"fileName"]) {' )
77+ . push ( 2 , '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"; filename=\\"%@\\"\\r\\n", param[@"name"], param[@"fileName"]];' )
78+ . push ( 2 , '[body appendFormat:@"Content-Type: %@\\r\\n\\r\\n", param[@"contentType"]];' )
79+ . push ( 2 , '[body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];' )
80+ . push ( 2 , 'if (error) {' )
81+ . push ( 3 , 'NSLog(@"%@", error);' )
82+ . push ( 2 , '}' )
83+ . push ( 1 , '} else {' )
84+ . push ( 2 , '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"\\r\\n\\r\\n", param[@"name"]];' )
85+ . push ( 2 , '[body appendFormat:@"%@", param[@"value"]];' )
86+ . push ( 1 , '}' )
87+ . push ( '}' )
88+ . push ( '[body appendFormat:@"\\r\\n--%@--\\r\\n", boundary];' )
89+ . push ( 'NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];' )
8890 break
8991
9092 default :
91- code . push ( null )
92- code . push ( 'NSData *postData = [[NSData alloc] initWithData:[@"' + source . postData . text + '" dataUsingEncoding:NSUTF8StringEncoding]];' )
93+ code . blank ( )
94+ . push ( 'NSData *postData = [[NSData alloc] initWithData:[@"' + source . postData . text + '" dataUsingEncoding:NSUTF8StringEncoding]];' )
9395 }
9496 }
9597
96- code . push ( null )
97- code . push ( 'NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"' + source . fullUrl + '"]' )
98- // NSURLRequestUseProtocolCachePolicy is the default policy, let's just always set it to avoid confusion.
99- code . push ( ' cachePolicy:NSURLRequestUseProtocolCachePolicy' )
100- code . push ( ' timeoutInterval:' + parseInt ( opts . timeout , 10 ) . toFixed ( 1 ) + '];' )
101- code . push ( '[request setHTTPMethod:@"' + source . method + '"];' )
98+ code . blank ( )
99+ . push ( 'NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"' + source . fullUrl + '"]' )
100+ // NSURLRequestUseProtocolCachePolicy is the default policy, let's just always set it to avoid confusion.
101+ . push ( ' cachePolicy:NSURLRequestUseProtocolCachePolicy' )
102+ . push ( ' timeoutInterval:' + parseInt ( opts . timeout , 10 ) . toFixed ( 1 ) + '];' )
103+ . push ( '[request setHTTPMethod:@"' + source . method + '"];' )
102104
103105 if ( req . hasHeaders ) {
104106 code . push ( '[request setAllHTTPHeaderFields:headers];' )
@@ -108,20 +110,20 @@ module.exports = function (source, options) {
108110 code . push ( '[request setHTTPBody:postData];' )
109111 }
110112
111- code . push ( null )
112- // Retrieving the shared session will be less verbose than creating a new one.
113- code . push ( 'NSURLSession *session = [NSURLSession sharedSession];' )
114- code . push ( 'NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request' )
115- code . push ( ' completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {' )
116- code . push ( ' ' + indent + ' if (error) {')
117- code . push ( ' ' + indent + indent + ' NSLog(@"%@", error);')
118- code . push ( ' ' + indent + ' } else {')
119- // Casting the NSURLResponse to NSHTTPURLResponse so the user can see the status code .
120- code . push ( ' ' + indent + indent + ' NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;')
121- code . push ( ' ' + indent + indent + ' NSLog(@"%@", httpResponse);')
122- code . push ( ' ' + indent + ' }')
123- code . push ( ' }];' )
124- code . push ( '[dataTask resume];' )
113+ code . blank ( )
114+ // Retrieving the shared session will be less verbose than creating a new one.
115+ . push ( 'NSURLSession *session = [NSURLSession sharedSession];' )
116+ . push ( 'NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request' )
117+ . push ( ' completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {' )
118+ . push ( 1 , ' if (error) {')
119+ . push ( 2 , ' NSLog(@"%@", error);')
120+ . push ( 1 , ' } else {')
121+ // Casting the NSURLResponse to NSHTTPURLResponse so the user can see the status .
122+ . push ( 2 , ' NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;')
123+ . push ( 2 , ' NSLog(@"%@", httpResponse);')
124+ . push ( 1 , ' }')
125+ . push ( ' }];' )
126+ . push ( '[dataTask resume];' )
125127
126128 return code . join ( )
127129}
0 commit comments