Add PromiseKit dependency
- Added PromiseKit dependency
This commit is contained in:
100
Carthage/Checkouts/PromiseKit/Extensions/OMGHTTPURLRQ/Sources/NSURLSession+OMG+AnyPromise.m
vendored
Normal file
100
Carthage/Checkouts/PromiseKit/Extensions/OMGHTTPURLRQ/Sources/NSURLSession+OMG+AnyPromise.m
vendored
Normal file
@@ -0,0 +1,100 @@
|
||||
#import <Foundation/NSJSONSerialization.h>
|
||||
#import <OMGHTTPURLRQ/OMGHTTPURLRQ.h>
|
||||
#import <Foundation/NSURLResponse.h>
|
||||
#import <CoreFoundation/CFString.h>
|
||||
#import <Foundation/NSOperation.h>
|
||||
#import <Foundation/NSURLError.h>
|
||||
#import <CoreFoundation/CFURL.h>
|
||||
#import <Foundation/NSThread.h>
|
||||
#import <Foundation/NSArray.h>
|
||||
#import <Foundation/NSError.h>
|
||||
#import <Foundation/NSURL.h>
|
||||
#import "PMKOMGHTTPURLRQ.h"
|
||||
|
||||
#if !COCOAPODS
|
||||
#import <PMKFoundation/NSURLSession+AnyPromise.h>
|
||||
#else
|
||||
#import "NSURLSession+AnyPromise.h"
|
||||
#endif
|
||||
|
||||
static id PMKURLRequestFromURLFormat(NSError **err, id urlFormat, ...);
|
||||
static id go(NSURLSession *, NSMutableURLRequest *);
|
||||
|
||||
|
||||
@implementation NSURLSession (PMKOMG)
|
||||
|
||||
- (AnyPromise *)GET:(id)urlFormat, ... {
|
||||
id err;
|
||||
id rq = PMKURLRequestFromURLFormat(&err, urlFormat);
|
||||
if (err) return [AnyPromise promiseWithValue:err];
|
||||
return go(self, rq);
|
||||
}
|
||||
|
||||
- (AnyPromise *)GET:(NSString *)url query:(NSDictionary *)params {
|
||||
id err;
|
||||
id rq = [OMGHTTPURLRQ GET:url:params error:&err];
|
||||
if (err) return [AnyPromise promiseWithValue:err];
|
||||
return go(self, rq);
|
||||
}
|
||||
|
||||
- (AnyPromise *)POST:(NSString *)url formURLEncodedParameters:(NSDictionary *)params {
|
||||
id err;
|
||||
id rq = [OMGHTTPURLRQ POST:url:params error:&err];
|
||||
if (err) return [AnyPromise promiseWithValue:err];
|
||||
return go(self, rq);
|
||||
}
|
||||
|
||||
- (AnyPromise *)POST:(NSString *)urlString JSON:(NSDictionary *)params {
|
||||
id err;
|
||||
id rq = [OMGHTTPURLRQ POST:urlString JSON:params error:&err];
|
||||
if (err) return [AnyPromise promiseWithValue:err];
|
||||
return go(self, rq);
|
||||
}
|
||||
|
||||
- (AnyPromise *)PUT:(NSString *)url formURLEncodedParameters:(NSDictionary *)params {
|
||||
id err;
|
||||
id rq = [OMGHTTPURLRQ PUT:url:params error:&err];
|
||||
if (err) return [AnyPromise promiseWithValue:err];
|
||||
return go(self, rq);
|
||||
|
||||
}
|
||||
|
||||
- (AnyPromise *)DELETE:(NSString *)url formURLEncodedParameters:(NSDictionary *)params {
|
||||
id err;
|
||||
id rq = [OMGHTTPURLRQ DELETE:url :params error:&err];
|
||||
if (err) return [AnyPromise promiseWithValue:err];
|
||||
return go(self, rq);
|
||||
}
|
||||
|
||||
- (AnyPromise *)PATCH:(NSString *)url JSON:(NSDictionary *)params {
|
||||
id err;
|
||||
id rq = [OMGHTTPURLRQ PATCH:url JSON:params error:&err];
|
||||
if (err) return [AnyPromise promiseWithValue:err];
|
||||
return go(self, rq);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
static id PMKURLRequestFromURLFormat(NSError **err, id urlFormat, ...) {
|
||||
if ([urlFormat isKindOfClass:[NSString class]]) {
|
||||
va_list arguments;
|
||||
va_start(arguments, urlFormat);
|
||||
urlFormat = [[NSString alloc] initWithFormat:urlFormat arguments:arguments];
|
||||
va_end(arguments);
|
||||
} else if ([urlFormat isKindOfClass:[NSURL class]]) {
|
||||
NSMutableURLRequest *rq = [[NSMutableURLRequest alloc] initWithURL:urlFormat];
|
||||
[rq setValue:OMGUserAgent() forHTTPHeaderField:@"User-Agent"];
|
||||
return rq;
|
||||
} else {
|
||||
urlFormat = [urlFormat description];
|
||||
}
|
||||
return [OMGHTTPURLRQ GET:urlFormat:nil error:err];
|
||||
}
|
||||
|
||||
static id go(NSURLSession *session, NSMutableURLRequest *rq) {
|
||||
if ([rq valueForHTTPHeaderField:@"User-Agent"] == nil) {
|
||||
[rq setValue:OMGUserAgent() forHTTPHeaderField:@"User-Agent"];
|
||||
}
|
||||
return [session promiseDataTaskWithRequest:rq];
|
||||
}
|
||||
170
Carthage/Checkouts/PromiseKit/Extensions/OMGHTTPURLRQ/Sources/NSURLSession+OMG+Promise.swift
vendored
Normal file
170
Carthage/Checkouts/PromiseKit/Extensions/OMGHTTPURLRQ/Sources/NSURLSession+OMG+Promise.swift
vendored
Normal file
@@ -0,0 +1,170 @@
|
||||
import OMGHTTPURLRQ
|
||||
import Foundation
|
||||
#if !PMKCocoaPods
|
||||
import PMKFoundation
|
||||
import PromiseKit
|
||||
#endif
|
||||
|
||||
/**
|
||||
To import the `OMGHTTPURLRQ` category:
|
||||
|
||||
use_frameworks!
|
||||
pod "PromiseKit/OMGHTTPURLRQ"
|
||||
|
||||
And then in your sources:
|
||||
|
||||
import PromiseKit
|
||||
|
||||
We provide convenience categories for the `URLSession.shared`, or
|
||||
an instance method `promise`. If you need more complicated behavior
|
||||
we recommend wrapping that usage in a `Promise` initializer.
|
||||
*/
|
||||
extension URLSession {
|
||||
/**
|
||||
Makes a **GET** request to the provided URL.
|
||||
|
||||
let p = URLSession.shared.GET("http://example.com", query: ["foo": "bar"])
|
||||
p.then { data -> Void in
|
||||
//…
|
||||
}
|
||||
p.asImage().then { image -> Void in
|
||||
//…
|
||||
}
|
||||
p.asDictionary().then { json -> Void in
|
||||
//…
|
||||
}
|
||||
|
||||
- Parameter url: The URL to request.
|
||||
- Parameter query: The parameters to be encoded as the query string for the GET request.
|
||||
- Returns: A promise that represents the GET request.
|
||||
*/
|
||||
public func GET(_ url: String, query: [String: Any]? = nil) -> Promise<(data: Data, response: URLResponse)> {
|
||||
return start(try OMGHTTPURLRQ.get(url, query) as URLRequest)
|
||||
}
|
||||
|
||||
/**
|
||||
Makes a POST request to the provided URL passing form URL encoded
|
||||
parameters.
|
||||
|
||||
Form URL-encoding is the standard way to POST on the Internet, so
|
||||
probably this is what you want. If it doesn’t work, try the `+POST:JSON`
|
||||
variant.
|
||||
|
||||
let url = "http://jsonplaceholder.typicode.com/posts"
|
||||
let params = ["title": "foo", "body": "bar", "userId": 1]
|
||||
URLSession.shared.POST(url, formData: params).asDictionary().then { json -> Void in
|
||||
//…
|
||||
}
|
||||
|
||||
- Parameter url: The URL to request.
|
||||
- Parameter formData: The parameters to be form URL-encoded and passed as the POST body.
|
||||
- Returns: A promise that represents the POST request.
|
||||
*/
|
||||
public func POST(_ url: String, formData: [String: Any]? = nil) -> Promise<(data: Data, response: URLResponse)> {
|
||||
return start(try OMGHTTPURLRQ.post(url, formData) as URLRequest)
|
||||
}
|
||||
|
||||
/**
|
||||
Makes a POST request to the provided URL passing multipart form-data.
|
||||
|
||||
let formData = OMGMultipartFormData()
|
||||
let imgData = Data(contentsOfFile: "image.png")
|
||||
formData.addFile(imgdata, parameterName: "file1", filename: "myimage1.png", contentType: "image/png")
|
||||
|
||||
URLSession.shared.POST(url, multipartFormData: formData).then { data in
|
||||
//…
|
||||
}
|
||||
|
||||
- Parameter url: The URL to request.
|
||||
- Parameter multipartFormData: The parameters to be multipart form-data encoded and passed as the POST body.
|
||||
- Returns: A promise that represents the POST request.
|
||||
- SeeAlso: [https://github.com/mxcl/OMGHTTPURLRQ](OMGHTTPURLRQ)
|
||||
*/
|
||||
public func POST(_ url: String, multipartFormData: OMGMultipartFormData) -> Promise<(data: Data, response: URLResponse)> {
|
||||
return start(try OMGHTTPURLRQ.post(url, multipartFormData) as URLRequest)
|
||||
}
|
||||
|
||||
/**
|
||||
Makes a POST request to the provided URL passing JSON encoded
|
||||
parameters.
|
||||
|
||||
Most web servers nowadays support POST with either JSON or form
|
||||
URL-encoding. If in doubt try form URL-encoded parameters first.
|
||||
|
||||
let url = "http://jsonplaceholder.typicode.com/posts"
|
||||
let params = ["title": "foo", "body": "bar", "userId": 1]
|
||||
URLSession.shared.POST(url, json: params).asDictionary().then { json -> Void in
|
||||
//…
|
||||
}
|
||||
|
||||
- Parameter url: The URL to request.
|
||||
- Parameter json: The parameters to be JSON-encoded and passed as the POST body.
|
||||
- Returns: A promise that represents the POST request.
|
||||
*/
|
||||
public func POST(_ url: String, json: [String: Any]? = nil) -> Promise<(data: Data, response: URLResponse)> {
|
||||
return start(try OMGHTTPURLRQ.post(url, json: json) as URLRequest)
|
||||
}
|
||||
|
||||
/**
|
||||
Makes a PUT request to the provided URL passing JSON encoded parameters.
|
||||
|
||||
let url = "http://jsonplaceholder.typicode.com/posts"
|
||||
let params = ["title": "foo", "body": "bar", "userId": 1]
|
||||
URLSession.shared.PUT(url, json: params).asDictionary().then { json -> Void in
|
||||
//…
|
||||
}
|
||||
|
||||
- Parameter url: The URL to request.
|
||||
- Parameter json: The parameters to be JSON-encoded and passed as the PUT body.
|
||||
- Returns: A promise that represents the PUT request.
|
||||
*/
|
||||
public func PUT(_ url: String, json: [String: Any]? = nil) -> Promise<(data: Data, response: URLResponse)> {
|
||||
return start(try OMGHTTPURLRQ.put(url, json: json) as URLRequest)
|
||||
}
|
||||
|
||||
/**
|
||||
Makes a DELETE request to the provided URL passing form URL-encoded
|
||||
parameters.
|
||||
|
||||
let url = "http://jsonplaceholder.typicode.com/posts/1"
|
||||
URLSession.shared.DELETE(url).then.asDictionary() { json -> Void in
|
||||
//…
|
||||
}
|
||||
|
||||
- Parameter url: The URL to request.
|
||||
- Returns: A promise that represents the PUT request.
|
||||
*/
|
||||
public func DELETE(_ url: String) -> Promise<(data: Data, response: URLResponse)> {
|
||||
return start(try OMGHTTPURLRQ.delete(url, nil) as URLRequest)
|
||||
}
|
||||
|
||||
/**
|
||||
Makes a PATCH request to the provided URL passing the provided JSON parameters.
|
||||
|
||||
let url = "http://jsonplaceholder.typicode.com/posts/1"
|
||||
let params = ["foo": "bar"]
|
||||
NSURLConnection.PATCH(url, json: params).asDictionary().then { json -> Void in
|
||||
//…
|
||||
}
|
||||
- Parameter url: The URL to request.
|
||||
- Parameter json: The JSON parameters to encode as the PATCH body.
|
||||
- Returns: A promise that represents the PUT request.
|
||||
*/
|
||||
public func PATCH(_ url: String, json: [String: Any]? = nil) -> Promise<(data: Data, response: URLResponse)> {
|
||||
return start(try OMGHTTPURLRQ.patch(url, json: json) as URLRequest)
|
||||
}
|
||||
|
||||
private func start(_ body: @autoclosure () throws -> URLRequest) -> Promise<(data: Data, response: URLResponse)> {
|
||||
do {
|
||||
var request = try body()
|
||||
|
||||
if request.value(forHTTPHeaderField: "User-Agent") == nil {
|
||||
request.setValue(OMGUserAgent(), forHTTPHeaderField: "User-Agent")
|
||||
}
|
||||
|
||||
return dataTask(.promise, with: request)
|
||||
} catch {
|
||||
return Promise(error: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
200
Carthage/Checkouts/PromiseKit/Extensions/OMGHTTPURLRQ/Sources/PMKOMGHTTPURLRQ.h
vendored
Normal file
200
Carthage/Checkouts/PromiseKit/Extensions/OMGHTTPURLRQ/Sources/PMKOMGHTTPURLRQ.h
vendored
Normal file
@@ -0,0 +1,200 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <PromiseKit/AnyPromise.h>
|
||||
|
||||
/**
|
||||
To import the `NSURLSession` category:
|
||||
|
||||
use_frameworks!
|
||||
pod "PromiseKit/Foundation"
|
||||
|
||||
Or `NSURLSession` is one of the categories imported by the umbrella pod:
|
||||
|
||||
use_frameworks!
|
||||
pod "PromiseKit"
|
||||
|
||||
And then in your sources:
|
||||
|
||||
#import <PromiseKit/PromiseKit.h>
|
||||
|
||||
PromiseKit automatically deserializes the raw HTTP data response into the
|
||||
appropriate rich data type based on the mime type the server provides.
|
||||
Thus if the response is JSON you will get the deserialized JSON response.
|
||||
PromiseKit supports decoding into strings, JSON and UIImages.
|
||||
|
||||
However if your server does not provide a rich content-type, you will
|
||||
just get `NSData`. This is rare, but a good example we came across was
|
||||
downloading files from Dropbox.
|
||||
|
||||
PromiseKit goes to quite some lengths to provide good `NSError` objects
|
||||
for error conditions at all stages of the HTTP to rich-data type
|
||||
pipeline. We provide the following additional `userInfo` keys as
|
||||
appropriate:
|
||||
|
||||
- `PMKURLErrorFailingDataKey`
|
||||
- `PMKURLErrorFailingStringKey`
|
||||
- `PMKURLErrorFailingURLResponseKey`
|
||||
|
||||
PromiseKit uses [OMGHTTPURLRQ](https://github.com/mxcl/OMGHTTPURLRQ) to
|
||||
make its HTTP requests. PromiseKit only provides a convenience layer
|
||||
above OMGHTTPURLRQ, thus if you need more power (eg. a multipartFormData
|
||||
POST), use OMGHTTPURLRQ to generate the `NSURLRequest` and then pass
|
||||
that request to `+promise:`.
|
||||
|
||||
@see https://github.com/mxcl/OMGHTTPURLRQ
|
||||
*/
|
||||
@interface NSURLSession (PMKOMG)
|
||||
|
||||
/**
|
||||
Makes a GET request to the provided URL.
|
||||
|
||||
[NSURLSession GET:@"http://placekitten.com/320/320"].then(^(UIImage *img){
|
||||
// PromiseKit decodes the image (if it’s an image)
|
||||
});
|
||||
|
||||
@param urlStringFormatOrURL The `NSURL` or string format to request.
|
||||
|
||||
@return A promise that fulfills with three parameters:
|
||||
|
||||
1) The deserialized data response.
|
||||
2) The `NSHTTPURLResponse`.
|
||||
3) The raw `NSData` response.
|
||||
*/
|
||||
- (AnyPromise *)GET:(id)urlStringFormatOrURL, ... NS_REFINED_FOR_SWIFT;
|
||||
|
||||
/**
|
||||
Makes a GET request with the provided query parameters.
|
||||
|
||||
id url = @"http://jsonplaceholder.typicode.com/comments";
|
||||
id params = @{@"postId": @1};
|
||||
[NSURLSession GET:url query:params].then(^(NSDictionary *jsonResponse){
|
||||
// PromiseKit decodes the JSON dictionary (if it’s JSON)
|
||||
});
|
||||
|
||||
@param urlString The `NSURL` or URL string format to request.
|
||||
|
||||
@param parameters The parameters to be encoded as the query string for the GET request.
|
||||
|
||||
@return A promise that fulfills with three parameters:
|
||||
|
||||
1) The deserialized data response.
|
||||
2) The `NSHTTPURLResponse`.
|
||||
3) The raw `NSData` response.
|
||||
*/
|
||||
- (AnyPromise *)GET:(NSString *)urlString query:(NSDictionary *)parameters NS_REFINED_FOR_SWIFT;
|
||||
|
||||
/**
|
||||
Makes a POST request to the provided URL passing form URL encoded
|
||||
parameters.
|
||||
|
||||
Form URL-encoding is the standard way to POST on the Internet, so
|
||||
probably this is what you want. If it doesn’t work, try the `+POST:JSON`
|
||||
variant.
|
||||
|
||||
id url = @"http://jsonplaceholder.typicode.com/posts";
|
||||
id params = @{@"title": @"foo", @"body": @"bar", @"userId": @1};
|
||||
[NSURLSession POST:url formURLEncodedParameters:params].then(^(NSDictionary *jsonResponse){
|
||||
// PromiseKit decodes the JSON dictionary (if it’s JSON)
|
||||
});
|
||||
|
||||
@param urlString The URL to request.
|
||||
|
||||
@param parameters The parameters to be form URL-encoded and passed as the POST body.
|
||||
|
||||
@return A promise that fulfills with three parameters:
|
||||
|
||||
1) The deserialized data response.
|
||||
2) The `NSHTTPURLResponse`.
|
||||
3) The raw `NSData` response.
|
||||
*/
|
||||
- (AnyPromise *)POST:(NSString *)urlString formURLEncodedParameters:(NSDictionary *)parameters NS_REFINED_FOR_SWIFT;
|
||||
|
||||
/**
|
||||
Makes a POST request to the provided URL passing JSON encoded parameters.
|
||||
|
||||
Most web servers nowadays support POST with either JSON or form
|
||||
URL-encoding. If in doubt try form URL-encoded parameters first.
|
||||
|
||||
id url = @"http://jsonplaceholder.typicode.com/posts";
|
||||
id params = @{@"title": @"foo", @"body": @"bar", @"userId": @1};
|
||||
[NSURLSession POST:url JSON:params].then(^(NSDictionary *jsonResponse){
|
||||
// PromiseKit decodes the JSON dictionary (if it’s JSON)
|
||||
});
|
||||
|
||||
@param urlString The URL to request.
|
||||
|
||||
@param JSONParameters The parameters to be JSON encoded and passed as the POST body.
|
||||
|
||||
@return A promise that fulfills with three parameters:
|
||||
|
||||
1) The deserialized data response.
|
||||
2) The `NSHTTPURLResponse`.
|
||||
3) The raw `NSData` response.
|
||||
*/
|
||||
- (AnyPromise *)POST:(NSString *)urlString JSON:(NSDictionary *)JSONParameters NS_REFINED_FOR_SWIFT;
|
||||
|
||||
/**
|
||||
Makes a PUT request to the provided URL passing form URL-encoded
|
||||
parameters.
|
||||
|
||||
id url = @"http://jsonplaceholder.typicode.com/posts/1";
|
||||
id params = @{@"id": @1, @"title": @"foo", @"body": @"bar", @"userId": @1};
|
||||
[NSURLSession PUT:url formURLEncodedParameters:params].then(^(NSDictionary *jsonResponse){
|
||||
// PromiseKit decodes the JSON dictionary (if it’s JSON)
|
||||
});
|
||||
|
||||
@param urlString The URL to request.
|
||||
|
||||
@param parameters The parameters to be form URL-encoded and passed as the HTTP body.
|
||||
|
||||
@return A promise that fulfills with three parameters:
|
||||
|
||||
1) The deserialized data response.
|
||||
2) The `NSHTTPURLResponse`.
|
||||
3) The raw `NSData` response.
|
||||
*/
|
||||
- (AnyPromise *)PUT:(NSString *)urlString formURLEncodedParameters:(NSDictionary *)parameters NS_REFINED_FOR_SWIFT;
|
||||
|
||||
/**
|
||||
Makes a DELETE request to the provided URL passing form URL-encoded
|
||||
parameters.
|
||||
|
||||
id url = @"http://jsonplaceholder.typicode.com/posts/1";
|
||||
id params = nil;
|
||||
[NSURLSession DELETE:url formURLEncodedParameters:params].then(^(NSDictionary *jsonResponse){
|
||||
// PromiseKit decodes the JSON dictionary (if it’s JSON)
|
||||
});
|
||||
|
||||
@param urlString The URL to request.
|
||||
|
||||
@param parameters The parameters to be form URL-encoded and passed as the HTTP body.
|
||||
|
||||
@return A promise that fulfills with three parameters:
|
||||
|
||||
1) The deserialized data response.
|
||||
2) The `NSHTTPURLResponse`.
|
||||
3) The raw `NSData` response.
|
||||
*/
|
||||
- (AnyPromise *)DELETE:(NSString *)urlString formURLEncodedParameters:(NSDictionary *)parameters NS_REFINED_FOR_SWIFT;
|
||||
|
||||
/**
|
||||
Makes a PATCH request to the provided URL passing the provided JSON parameters.
|
||||
|
||||
id url = @"http://jsonplaceholder.typicode.com/posts/1";
|
||||
id params = nil;
|
||||
[NSURLSession PATCH:url JSON:params].then(^(NSDictionary *jsonResponse){
|
||||
// PromiseKit decodes the JSON dictionary (if it’s JSON)
|
||||
});
|
||||
|
||||
@param urlString The URL to request.
|
||||
|
||||
@param JSONParameters The JSON parameters.
|
||||
|
||||
@return A promise that fulfills with three parameters:
|
||||
|
||||
1) The deserialized data response.
|
||||
2) The `NSHTTPURLResponse`.
|
||||
3) The raw `NSData` response.
|
||||
*/
|
||||
- (AnyPromise *)PATCH:(NSString *)urlString JSON:(NSDictionary *)JSONParameters NS_REFINED_FOR_SWIFT;
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user