Swiftgram/submodules/MtProtoKit/MTProtoKit/MTHttpRequestOperation.m
Peter 373769682e Add 'submodules/MtProtoKit/' from commit '14ab734b977fd4f1686a2a13415f6a4c9b9fdd6d'
git-subtree-dir: submodules/MtProtoKit
git-subtree-mainline: 3b155750f5a4894ff3dedf1860a37e94e0ea9571
git-subtree-split: 14ab734b977fd4f1686a2a13415f6a4c9b9fdd6d
2019-06-11 18:55:34 +01:00

54 lines
2.0 KiB
Objective-C

#import "MTHttpRequestOperation.h"
#import "../thirdparty/AFNetworking/AFHTTPRequestOperation.h"
#if defined(MtProtoKitDynamicFramework)
# import <MTProtoKitDynamic/MTDisposable.h>
# import <MTProtoKitDynamic/MTSignal.h>
#elif defined(MtProtoKitMacFramework)
# import <MTProtoKitMac/MTDisposable.h>
# import <MTProtoKitMac/MTSignal.h>
#else
# import <MtProtoKit/MTDisposable.h>
# import <MtProtoKit/MTSignal.h>
#endif
@implementation MTHttpRequestOperation
+ (MTSignal *)dataForHttpUrl:(NSURL *)url {
return [self dataForHttpUrl:url headers:nil];
}
+ (MTSignal *)dataForHttpUrl:(NSURL *)url headers:(NSDictionary *)headers {
return [[MTSignal alloc] initWithGenerator:^id<MTDisposable>(MTSubscriber *subscriber) {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, __unused BOOL *stop) {
[request setValue:value forHTTPHeaderField:key];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setSuccessCallbackQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
[operation setFailureCallbackQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
[operation setCompletionBlockWithSuccess:^(__unused NSOperation *operation, __unused id responseObject)
{
[subscriber putNext:[(AFHTTPRequestOperation *)operation responseData]];
[subscriber putCompletion];
} failure:^(__unused NSOperation *operation, __unused NSError *error)
{
[subscriber putError:nil];
}];
[operation start];
__weak AFHTTPRequestOperation *weakOperation = operation;
return [[MTBlockDisposable alloc] initWithBlock:^
{
__strong AFHTTPRequestOperation *strongOperation = weakOperation;
[strongOperation cancel];
}];
}];
}
@end