|
1 | | -'use strict'; |
| 1 | +'use strict' |
2 | 2 |
|
3 | | -var util = require('util'); |
4 | | -var objcHelpers = require('./helpers'); |
| 3 | +var util = require('util') |
| 4 | +var objcHelpers = require('./helpers') |
5 | 5 |
|
6 | 6 | module.exports = function (source, options) { |
7 | 7 | var opts = util._extend({ |
8 | 8 | timeout: '10', |
9 | 9 | indent: ' ', |
10 | 10 | pretty: true |
11 | | - }, options); |
| 11 | + }, options) |
12 | 12 |
|
13 | | - var code = []; |
| 13 | + var code = [] |
14 | 14 |
|
15 | 15 | var req = { |
16 | 16 | hasHeaders: false, |
17 | 17 | hasBody: false |
18 | | - }; |
| 18 | + } |
| 19 | + |
| 20 | + var indent = opts.indent |
19 | 21 |
|
20 | | - //code.push('// Make sure Foundation is imported:'); |
21 | | - code.push('#import <Foundation/Foundation.h>'); |
| 22 | + code.push('#import <Foundation/Foundation.h>') |
22 | 23 |
|
23 | 24 | if (Object.keys(source.allHeaders).length) { |
24 | | - req.hasHeaders = true; |
25 | | - code.push(null); |
26 | | - code.push(objcHelpers.nsDictionaryBuilder('headers', source.allHeaders, opts.pretty)); |
| 25 | + req.hasHeaders = true |
| 26 | + code.push(null) |
| 27 | + code.push(objcHelpers.nsDictionaryBuilder('headers', source.allHeaders, opts.pretty)) |
27 | 28 | } |
28 | 29 |
|
29 | 30 | if (source.postData.text || source.postData.jsonObj || source.postData.params) { |
30 | | - req.hasBody = true; |
| 31 | + req.hasBody = true |
31 | 32 |
|
32 | 33 | switch (source.postData.mimeType) { |
33 | 34 | case 'application/x-www-form-urlencoded': |
34 | | - code.push(null); |
| 35 | + code.push(null) |
35 | 36 | // Makes it easier to implement logice than just putting the entire body string |
36 | | - code.push(util.format('NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];', source.postData.params[0].name, source.postData.params[0].value)); |
| 37 | + code.push(util.format('NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];', source.postData.params[0].name, source.postData.params[0].value)) |
37 | 38 | for (var i = 1, len = source.postData.params.length; i < len; i++) { |
38 | | - code.push(util.format('[postData appendData:[@"&%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];', source.postData.params[i].name, source.postData.params[i].value)); |
| 39 | + code.push(util.format('[postData appendData:[@"&%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];', source.postData.params[i].name, source.postData.params[i].value)) |
39 | 40 | } |
40 | | - break; |
| 41 | + break |
41 | 42 |
|
42 | 43 | case 'application/json': |
43 | 44 | if (source.postData.jsonObj) { |
44 | | - code.push(objcHelpers.nsDictionaryBuilder('parameters', source.postData.jsonObj, opts.pretty)); |
45 | | - code.push(null); |
46 | | - code.push('NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];'); |
| 45 | + code.push(objcHelpers.nsDictionaryBuilder('parameters', source.postData.jsonObj, opts.pretty)) |
| 46 | + code.push(null) |
| 47 | + code.push('NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];') |
47 | 48 | } |
48 | | - break; |
| 49 | + break |
49 | 50 |
|
50 | 51 | case 'multipart/form-data': |
51 | | - code.push(objcHelpers.nsArrayBuilder('parameters', source.postData.params, opts.pretty)); |
52 | | - code.push(util.format('NSString *boundary = @"%s";', source.postData.boundary)); |
53 | | - code.push(null); |
54 | | - code.push('NSError *error;'); |
55 | | - code.push('NSMutableString *body = [NSMutableString string];'); |
56 | | - code.push('for (NSDictionary *param in parameters) {'); |
57 | | - code.push(opts.indent + '[body appendFormat:@"--%@\\r\\n", boundary];'); |
58 | | - code.push(opts.indent + 'if (param[@"fileName"]) {'); |
59 | | - code.push(opts.indent + opts.indent + '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"; filename=\\"%@\\"\\r\\n", param[@"name"], param[@"fileName"]];'); |
60 | | - code.push(opts.indent + opts.indent + '[body appendFormat:@"Content-Type: %@\\r\\n\\r\\n", param[@"contentType"]];'); |
61 | | - code.push(opts.indent + opts.indent + '[body appendFormat:@"%@", [[NSString alloc] initWithContentsOfFile:param[@"fileName"]'); |
62 | | - code.push(opts.indent + opts.indent + ' encoding:NSUTF8StringEncoding error:&error]];'); |
63 | | - code.push(opts.indent + opts.indent + 'if (error) {'); |
64 | | - code.push(opts.indent + opts.indent + opts.indent + 'NSLog(@"%@", error);'); |
65 | | - code.push(opts.indent + opts.indent + '}'); |
66 | | - code.push(opts.indent + '} else {'); |
67 | | - code.push(opts.indent + opts.indent + '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"\\r\\n\\r\\n", param[@"name"]];'); |
68 | | - code.push(opts.indent + opts.indent + '[body appendFormat:@"%@", param[@"value"]];'); |
69 | | - code.push(opts.indent + '}'); |
70 | | - code.push('}'); |
71 | | - code.push('[body appendFormat:@"\\r\\n--%@--\\r\\n", boundary];'); |
72 | | - code.push('NSData *postData = [[NSData alloc] initWithData:[body dataUsingEncoding:NSUTF8StringEncoding]];'); |
73 | | - break; |
| 52 | + code.push(objcHelpers.nsArrayBuilder('parameters', source.postData.params, opts.pretty)) |
| 53 | + code.push(util.format('NSString *boundary = @"%s";', source.postData.boundary)) |
| 54 | + code.push(null) |
| 55 | + code.push('NSError *error;') |
| 56 | + code.push('NSMutableString *body = [NSMutableString string];') |
| 57 | + code.push('for (NSDictionary *param in parameters) {') |
| 58 | + code.push(indent + '[body appendFormat:@"--%@\\r\\n", boundary];') |
| 59 | + code.push(indent + 'if (param[@"fileName"]) {') |
| 60 | + code.push(indent + indent + '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"; filename=\\"%@\\"\\r\\n", param[@"name"], param[@"fileName"]];') |
| 61 | + code.push(indent + indent + '[body appendFormat:@"Content-Type: %@\\r\\n\\r\\n", param[@"contentType"]];') |
| 62 | + code.push(indent + indent + '[body appendFormat:@"%@", [[NSString alloc] initWithContentsOfFile:param[@"fileName"]') |
| 63 | + code.push(indent + indent + ' encoding:NSUTF8StringEncoding error:&error]];') |
| 64 | + code.push(indent + indent + 'if (error) {') |
| 65 | + code.push(indent + indent + indent + 'NSLog(@"%@", error);') |
| 66 | + code.push(indent + indent + '}') |
| 67 | + code.push(indent + '} else {') |
| 68 | + code.push(indent + indent + '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"\\r\\n\\r\\n", param[@"name"]];') |
| 69 | + code.push(indent + indent + '[body appendFormat:@"%@", param[@"value"]];') |
| 70 | + code.push(indent + '}') |
| 71 | + code.push('}') |
| 72 | + code.push('[body appendFormat:@"\\r\\n--%@--\\r\\n", boundary];') |
| 73 | + code.push('NSData *postData = [[NSData alloc] initWithData:[body dataUsingEncoding:NSUTF8StringEncoding]];') |
| 74 | + break |
74 | 75 |
|
75 | 76 | default: |
76 | | - code.push(null); |
77 | | - code.push('NSData *postData = [[NSData alloc] initWithData:[@"' + source.postData.text + '" dataUsingEncoding:NSUTF8StringEncoding]];'); |
| 77 | + code.push(null) |
| 78 | + code.push('NSData *postData = [[NSData alloc] initWithData:[@"' + source.postData.text + '" dataUsingEncoding:NSUTF8StringEncoding]];') |
78 | 79 | } |
79 | 80 | } |
80 | 81 |
|
81 | | - code.push(null); |
82 | | - code.push('NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"' + source.fullUrl + '"]'); |
83 | | - code.push(' cachePolicy:NSURLRequestUseProtocolCachePolicy'); |
84 | | - code.push(' timeoutInterval:' + parseInt(opts.timeout, 10).toFixed(1) + '];'); |
85 | | - code.push('[request setHTTPMethod:@"' + source.method + '"];'); |
| 82 | + code.push(null) |
| 83 | + code.push('NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"' + source.fullUrl + '"]') |
| 84 | + code.push(' cachePolicy:NSURLRequestUseProtocolCachePolicy') |
| 85 | + code.push(' timeoutInterval:' + parseInt(opts.timeout, 10).toFixed(1) + '];') |
| 86 | + code.push('[request setHTTPMethod:@"' + source.method + '"];') |
86 | 87 |
|
87 | 88 | if (req.hasHeaders) { |
88 | | - code.push('[request setAllHTTPHeaderFields:headers];'); |
| 89 | + code.push('[request setAllHTTPHeaderFields:headers];') |
89 | 90 | } |
90 | 91 |
|
91 | 92 | if (req.hasBody) { |
92 | | - code.push('[request setHTTPBody:postData];'); |
| 93 | + code.push('[request setHTTPBody:postData];') |
93 | 94 | } |
94 | 95 |
|
95 | | - code.push(null); |
96 | | - code.push('NSURLSession *session = [NSURLSession sharedSession];'); // Retrieve shared session for simplicity |
97 | | - code.push('NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request'); |
98 | | - code.push(' completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {'); |
99 | | - code.push(' ' + opts.indent + 'if (error) {'); |
100 | | - code.push(' ' + opts.indent + opts.indent + 'NSLog(@"%@", error);'); |
101 | | - code.push(' ' + opts.indent + '} else {'); |
102 | | - code.push(' ' + opts.indent + opts.indent + 'NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;'); |
103 | | - code.push(' ' + opts.indent + opts.indent + 'NSLog(@"%@", httpResponse);'); |
104 | | - code.push(' ' + opts.indent + '}'); |
105 | | - code.push(' }];'); |
106 | | - code.push('[dataTask resume];'); |
107 | | - |
108 | | - return code.join('\n'); |
109 | | -}; |
| 96 | + code.push(null) |
| 97 | + code.push('NSURLSession *session = [NSURLSession sharedSession];') // Retrieve shared session for simplicity |
| 98 | + code.push('NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request') |
| 99 | + code.push(' completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {') |
| 100 | + code.push(' ' + indent + 'if (error) {') |
| 101 | + code.push(' ' + indent + indent + 'NSLog(@"%@", error);') |
| 102 | + code.push(' ' + indent + '} else {') |
| 103 | + code.push(' ' + indent + indent + 'NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;') |
| 104 | + code.push(' ' + indent + indent + 'NSLog(@"%@", httpResponse);') |
| 105 | + code.push(' ' + indent + '}') |
| 106 | + code.push(' }];') |
| 107 | + code.push('[dataTask resume];') |
| 108 | + |
| 109 | + return code.join('\n') |
| 110 | +} |
110 | 111 |
|
111 | 112 | module.exports.info = { |
112 | 113 | key: 'native', |
113 | 114 | title: 'NSURLSession', |
114 | 115 | link: 'https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html', |
115 | 116 | description: 'Foundation\'s NSURLSession request' |
116 | | -}; |
| 117 | +} |
0 commit comments