Skip to content

Commit

Permalink
Merge pull request #13 from hansemannn/expose-data-task
Browse files Browse the repository at this point in the history
Implement data-task API (NSURLSessionDataTask)
  • Loading branch information
hansemannn authored Feb 1, 2018
2 parents d371312 + f0851f3 commit 0352d96
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 16 deletions.
24 changes: 22 additions & 2 deletions apidoc/Session.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,27 @@ methods:
parameters:
- name: args
summary: An object representing the arguments to add a new upload task.
type: UploadTaskType
type: UploadDataTaskType
returns:
summary: Task's identifier number.
type: String

- name: dataTask
summary: |
Creates a data task for the specified URL, within the provided session
object and local data.
description: |
An data task does not provide any additional functionality over a usual
session task and its presence is merely to provide lexical differentiation
from download and upload tasks.
Once this function is called, the task starts automatically. Once finished,
the data task will call the `sessioncompleted` event containing infos about
the response.
parameters:
- name: args
summary: An object representing the arguments to add a new task task.
type: UploadDataTaskType
returns:
summary: Task's identifier number.
type: String
Expand Down Expand Up @@ -107,7 +127,7 @@ properties:
type: String

---
name: UploadTaskType
name: UploadDataTaskType
summary: The parameter for [uploadTask](Modules.URLSession.Session.uploadTask) method.
properties:
- name: url
Expand Down
2 changes: 2 additions & 0 deletions ios/Classes/ComAppceleratorUrlSessionSessionProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

- (id)downloadTask:(id)args;

- (id)dataTask:(id)args;

- (void)finishTasksAndInvalidate:(id)unused;

- (void)invalidateAndCancel:(id)unused;
Expand Down
79 changes: 66 additions & 13 deletions ios/Classes/ComAppceleratorUrlSessionSessionProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ - (id)uploadTask:(id)args
NSString *method = nil;
NSURL *fileURL = nil;
NSDictionary *headers = nil;
id data = [args objectForKey:@"data"];;
id data = [args objectForKey:@"data"];

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

[task resume];

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

return nil;
}

- (id)downloadTask:(id)args
{
ENSURE_SINGLE_ARG(args, NSDictionary);
ENSURE_SINGLE_ARG(args, NSDictionary);

NSString *url = [TiUtils stringValue:@"url" properties:args];

if ([url length]) {
NSURLSessionDownloadTask *task = [_session downloadTaskWithURL:[NSURL URLWithString:url]];
[task resume];

NSString *url = [TiUtils stringValue:@"url" properties:args];
return NUMINTEGER([task taskIdentifier]);
} else {
NSLog(@"[ERROR] Ti.URLSession: The specified url for download task is empty. Please provide a proper url.");
}

return nil;
}


- (id)dataTask:(id)args
{
ENSURE_SINGLE_ARG(args, NSDictionary);

NSString *url = nil;
NSString *method = nil;
NSDictionary *headers = nil;
id data = [args objectForKey:@"data"];

ENSURE_ARG_FOR_KEY(url, args, @"url", NSString);
ENSURE_ARG_OR_NIL_FOR_KEY(method, args, @"method", NSString);
ENSURE_ARG_OR_NIL_FOR_KEY(headers, args, @"requestHeaders", NSDictionary);

NSURL *nativeURL = [TiUtils toURL:url proxy:self];

if (nativeURL == nil) {
NSLog(@"[ERROR] Ti.URLSession: The specified URL for this data task is empty. Please provide a valid URL.");
return nil;
}

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nativeURL];

// HTTP method
[request setHTTPMethod:(method ?: @"POST")];

// Optional request headers
if (headers) {
for (id key in headers) {
ENSURE_TYPE(key, NSString);
ENSURE_TYPE([headers objectForKey:key], NSString);

[request setValue:[headers objectForKey:key] forHTTPHeaderField:key];
}
}

if (data != nil) {
NSError *error = nil;
NSData *postData = [NSJSONSerialization dataWithJSONObject:data options:0 error:&error];

if ([url length]) {
NSURLSessionDownloadTask *task = [_session downloadTaskWithURL:[NSURL URLWithString:url]];
[task resume];

return NUMINTEGER([task taskIdentifier]);
if (error == nil) {
[request setHTTPBody:postData];
} else {
NSLog(@"[ERROR] Ti.URLSession: The specified url for background download task is empty. Please provide a proper url.");
DebugLog(@"[ERROR] Could not append data: %@", error.localizedDescription);
}

return nil;
}

NSURLSessionDataTask *task = [_session dataTaskWithRequest:request];
[task resume];

return NUMINTEGER([task taskIdentifier]);
}

- (void)finishTasksAndInvalidate:(id)unused
Expand Down
2 changes: 1 addition & 1 deletion ios/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 2.1.0
version: 2.2.0
apiversion: 2
architectures: armv7 arm64 i386 x86_64
description: ti.urlsession
Expand Down

0 comments on commit 0352d96

Please sign in to comment.