Skip to content

Commit 0352d96

Browse files
authored
Merge pull request #13 from hansemannn/expose-data-task
Implement data-task API (NSURLSessionDataTask)
2 parents d371312 + f0851f3 commit 0352d96

File tree

4 files changed

+91
-16
lines changed

4 files changed

+91
-16
lines changed

apidoc/Session.yml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,27 @@ methods:
5959
parameters:
6060
- name: args
6161
summary: An object representing the arguments to add a new upload task.
62-
type: UploadTaskType
62+
type: UploadDataTaskType
63+
returns:
64+
summary: Task's identifier number.
65+
type: String
66+
67+
- name: dataTask
68+
summary: |
69+
Creates a data task for the specified URL, within the provided session
70+
object and local data.
71+
description: |
72+
An data task does not provide any additional functionality over a usual
73+
session task and its presence is merely to provide lexical differentiation
74+
from download and upload tasks.
75+
76+
Once this function is called, the task starts automatically. Once finished,
77+
the data task will call the `sessioncompleted` event containing infos about
78+
the response.
79+
parameters:
80+
- name: args
81+
summary: An object representing the arguments to add a new task task.
82+
type: UploadDataTaskType
6383
returns:
6484
summary: Task's identifier number.
6585
type: String
@@ -107,7 +127,7 @@ properties:
107127
type: String
108128

109129
---
110-
name: UploadTaskType
130+
name: UploadDataTaskType
111131
summary: The parameter for [uploadTask](Modules.URLSession.Session.uploadTask) method.
112132
properties:
113133
- name: url

ios/Classes/ComAppceleratorUrlSessionSessionProxy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
- (id)downloadTask:(id)args;
2727

28+
- (id)dataTask:(id)args;
29+
2830
- (void)finishTasksAndInvalidate:(id)unused;
2931

3032
- (void)invalidateAndCancel:(id)unused;

ios/Classes/ComAppceleratorUrlSessionSessionProxy.m

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ - (id)uploadTask:(id)args
4747
NSString *method = nil;
4848
NSURL *fileURL = nil;
4949
NSDictionary *headers = nil;
50-
id data = [args objectForKey:@"data"];;
50+
id data = [args objectForKey:@"data"];
5151

5252
ENSURE_ARG_FOR_KEY(url, args, @"url", NSString);
5353
ENSURE_ARG_OR_NIL_FOR_KEY(method, args, @"method", NSString);
@@ -75,36 +75,89 @@ - (id)uploadTask:(id)args
7575
} else if ([data isMemberOfClass:[TiBlob class]]) {
7676
task = [_session uploadTaskWithRequest:request fromData:[data data]];
7777
} else {
78-
NSLog(@"[ERROR] Ti.URLSession: The specified data for background upload task is incorrect. Please provide a file path or a blob.");
78+
NSLog(@"[ERROR] Ti.URLSession: The specified data for upload task is incorrect. Please provide a file path or a blob.");
7979
return [NSNull null];
8080
}
8181

8282
[task resume];
8383

8484
return NUMINTEGER([task taskIdentifier]);
8585
} else {
86-
NSLog(@"[ERROR] Ti.URLSession: The specified URL for this background upload task is empty. Please provide a valid URL.");
86+
NSLog(@"[ERROR] Ti.URLSession: The specified URL for this upload task is empty. Please provide a valid URL.");
8787
}
8888

8989
return nil;
9090
}
9191

9292
- (id)downloadTask:(id)args
9393
{
94-
ENSURE_SINGLE_ARG(args, NSDictionary);
94+
ENSURE_SINGLE_ARG(args, NSDictionary);
95+
96+
NSString *url = [TiUtils stringValue:@"url" properties:args];
97+
98+
if ([url length]) {
99+
NSURLSessionDownloadTask *task = [_session downloadTaskWithURL:[NSURL URLWithString:url]];
100+
[task resume];
95101

96-
NSString *url = [TiUtils stringValue:@"url" properties:args];
102+
return NUMINTEGER([task taskIdentifier]);
103+
} else {
104+
NSLog(@"[ERROR] Ti.URLSession: The specified url for download task is empty. Please provide a proper url.");
105+
}
106+
107+
return nil;
108+
}
109+
110+
111+
- (id)dataTask:(id)args
112+
{
113+
ENSURE_SINGLE_ARG(args, NSDictionary);
114+
115+
NSString *url = nil;
116+
NSString *method = nil;
117+
NSDictionary *headers = nil;
118+
id data = [args objectForKey:@"data"];
119+
120+
ENSURE_ARG_FOR_KEY(url, args, @"url", NSString);
121+
ENSURE_ARG_OR_NIL_FOR_KEY(method, args, @"method", NSString);
122+
ENSURE_ARG_OR_NIL_FOR_KEY(headers, args, @"requestHeaders", NSDictionary);
123+
124+
NSURL *nativeURL = [TiUtils toURL:url proxy:self];
125+
126+
if (nativeURL == nil) {
127+
NSLog(@"[ERROR] Ti.URLSession: The specified URL for this data task is empty. Please provide a valid URL.");
128+
return nil;
129+
}
130+
131+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nativeURL];
132+
133+
// HTTP method
134+
[request setHTTPMethod:(method ?: @"POST")];
135+
136+
// Optional request headers
137+
if (headers) {
138+
for (id key in headers) {
139+
ENSURE_TYPE(key, NSString);
140+
ENSURE_TYPE([headers objectForKey:key], NSString);
141+
142+
[request setValue:[headers objectForKey:key] forHTTPHeaderField:key];
143+
}
144+
}
145+
146+
if (data != nil) {
147+
NSError *error = nil;
148+
NSData *postData = [NSJSONSerialization dataWithJSONObject:data options:0 error:&error];
97149

98-
if ([url length]) {
99-
NSURLSessionDownloadTask *task = [_session downloadTaskWithURL:[NSURL URLWithString:url]];
100-
[task resume];
101-
102-
return NUMINTEGER([task taskIdentifier]);
150+
if (error == nil) {
151+
[request setHTTPBody:postData];
103152
} else {
104-
NSLog(@"[ERROR] Ti.URLSession: The specified url for background download task is empty. Please provide a proper url.");
153+
DebugLog(@"[ERROR] Could not append data: %@", error.localizedDescription);
105154
}
106-
107-
return nil;
155+
}
156+
157+
NSURLSessionDataTask *task = [_session dataTaskWithRequest:request];
158+
[task resume];
159+
160+
return NUMINTEGER([task taskIdentifier]);
108161
}
109162

110163
- (void)finishTasksAndInvalidate:(id)unused

ios/manifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# this is your module manifest and used by Titanium
33
# during compilation, packaging, distribution, etc.
44
#
5-
version: 2.1.0
5+
version: 2.2.0
66
apiversion: 2
77
architectures: armv7 arm64 i386 x86_64
88
description: ti.urlsession

0 commit comments

Comments
 (0)