|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var util = require('util'); |
| 4 | + |
| 5 | +module.exports = function (options) { |
| 6 | + var code = []; |
| 7 | + |
| 8 | + // Dependencies |
| 9 | + code.push('#import <Foundation/Foundation.h>'); |
| 10 | + code.push(null); |
| 11 | + code.push('NSURLSession *session = [NSURLSession sharedSession];'); |
| 12 | + code.push(null); |
| 13 | + // Create request object |
| 14 | + code.push('NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"' + this.source.fullUrl + '"]'); |
| 15 | + code.push(' cachePolicy:NSURLRequestUseProtocolCachePolicy'); |
| 16 | + code.push(' timeoutInterval:10.0];'); |
| 17 | + code.push('[request setHTTPMethod:@"' + this.source.method + '"];'); |
| 18 | + |
| 19 | + // Set headers |
| 20 | + this.source.headers.forEach(function (header) { |
| 21 | + code.push('[request setValue:@"' + header.value + '" forHTTPHeaderField:@"' + header.name + '"];'); |
| 22 | + }); |
| 23 | + |
| 24 | + var cookies = this.source.cookies.map(function (cookie) { |
| 25 | + return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value); |
| 26 | + }); |
| 27 | + |
| 28 | + if (cookies.length) { |
| 29 | + code.push('[request setValue:@"' + cookies.join('; ') + '" forHTTPHeaderField:@"Cookie"];'); |
| 30 | + } |
| 31 | + |
| 32 | + // Set request body |
| 33 | + if (this.source.postData && this.source.postData.params || this.source.postData.text) { |
| 34 | + code.push(null); |
| 35 | + |
| 36 | + if (this.source.postData.mimeType === 'application/x-www-form-urlencoded' && this.source.postData.params) { |
| 37 | + var params = this.source.postData.params; |
| 38 | + code.push('NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"' + params[0].name + '=' + params[0].value + '" dataUsingEncoding:NSUTF8StringEncoding]];'); |
| 39 | + for (var i = 1, len = params.length; i < len; i++) { |
| 40 | + code.push('[postData appendData:[@"&' + params[i].name + '=' + params[i].value + '" dataUsingEncoding:NSUTF8StringEncoding]];'); |
| 41 | + } |
| 42 | + } else if (this.source.postData.mimeType === 'application/json' && this.source.postData.text) { |
| 43 | + code.push('NSData *postData = [[NSData alloc] initWithData:[@' + JSON.stringify(this.source.postData.text) + ' dataUsingEncoding:NSUTF8StringEncoding]];'); |
| 44 | + } else if (this.source.postData.text) { |
| 45 | + code.push('NSData *postData = [[NSData alloc] initWithData:[@"' + this.source.postData.text + '" dataUsingEncoding:NSUTF8StringEncoding]];'); |
| 46 | + } |
| 47 | + |
| 48 | + code.push("[request setHTTPBody:postData];"); |
| 49 | + } |
| 50 | + |
| 51 | + // Set completion block |
| 52 | + code.push(null); |
| 53 | + code.push('NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request'); |
| 54 | + code.push(' completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {'); |
| 55 | + code.push(null); |
| 56 | + code.push('}];'); |
| 57 | + |
| 58 | + // Start request |
| 59 | + code.push(null); |
| 60 | + code.push('[dataTask resume];'); |
| 61 | + |
| 62 | + return code.join('\n'); |
| 63 | +}; |
| 64 | + |
| 65 | +module.exports.info = function () { |
| 66 | + return { |
| 67 | + family: 'objc', |
| 68 | + key: 'native', |
| 69 | + title: 'NSURLSession', |
| 70 | + link: 'https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html', |
| 71 | + description: 'Foundation\'s NSURLSession request' |
| 72 | + }; |
| 73 | +}; |
0 commit comments