summaryrefslogtreecommitdiff
path: root/Juick/APIClient.m
blob: ecaf5362da156a9d900849fcb45f18785cfb1a73 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
//  APIClient.m
//  Juick
//
//  Created by Vitaly Takmazov on 22/08/16.
//  Copyright © 2016 com.juick. All rights reserved.
//
#import "APIClient.h"
#import "PDKeychainBindings.h"
#import "Message.h"

@interface APIClient()
@property(nonatomic, strong) NSOperationQueue *backgroundQueue;
@end

@implementation APIClient

+(APIClient *) sharedClient {
    static APIClient *sharedAPIClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedAPIClient = [[self alloc] init];
    });
    return sharedAPIClient;
}

-(id)init {
    if (self = [super init]) {
        self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.juick.com"]];
        self.manager.requestSerializer = [AFJSONRequestSerializer new];
        self.backgroundQueue = [NSOperationQueue new];
    }
    return self;
}

-(void) pullNextFromPath:(NSString *)path  params:(NSDictionary *) params callback:(void(^)(NSArray *, NSError *))callback {
    AFHTTPSessionManager *manager = [APIClient sharedClient].manager;
    [self.backgroundQueue addOperationWithBlock:^{
        [manager GET:path parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSMutableArray *messages = [NSMutableArray new];
            [((NSArray *)responseObject) enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [messages addObject:[Message yy_modelWithJSON:obj]];
            }];
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                callback(messages, nil);
            }];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"REST Error: %@", error);
            NSInteger statusCode = ((NSHTTPURLResponse *)task.response).statusCode;
            if (statusCode == 401) {
                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    callback(nil, [[NSError alloc] initWithDomain:@"JuickErrorDomain" code:401 userInfo:nil]);
                }];
            }
        }];
    }];
}

-(void) postMessage:(NSString *)text result:(void (^)(Message *, NSError *))callback {
    AFHTTPSessionManager *manager = [APIClient sharedClient].manager;
    [self.backgroundQueue addOperationWithBlock:^{
        [manager POST:@"/post" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            [formData appendPartWithFormData:[text dataUsingEncoding:NSUTF8StringEncoding] name:@"body"];
        } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"Success!");
            Message *result = [Message yy_modelWithJSON:responseObject];
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                callback(result, nil);
            }];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"Error: %@", [error localizedDescription]);
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                callback(nil, error);
            }];
        }];
    }];
}

-(void) postReplyToThread:(NSNumber *)mid inReplyTo:(NSNumber *)rid text:(NSString *)text result:(void(^)(Message *, NSError *))callback {
    AFHTTPSessionManager *manager = [APIClient sharedClient].manager;
    [self.backgroundQueue addOperationWithBlock:^{
        [manager POST:@"/comment" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            [formData appendPartWithFormData:[[mid stringValue] dataUsingEncoding:NSUTF8StringEncoding] name:@"mid"];
            [formData appendPartWithFormData:[[rid stringValue] dataUsingEncoding:NSUTF8StringEncoding] name:@"rid"];
            [formData appendPartWithFormData:[text dataUsingEncoding:NSUTF8StringEncoding] name:@"body"];
        } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"Success!");
            Message *reply = [Message yy_modelWithJSON:responseObject];
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                callback(reply, nil);
            }];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"Error: %@", [error localizedDescription]);
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                callback(nil, error);
            }];
        }];
    }];
}


+(NSString *) messagesUrl {
    return @"/messages";
}

+(NSString *) threadUrl {
    return @"/thread";
}

+(NSString *) feedUrl {
    return @"/home";
}

@end