-
Notifications
You must be signed in to change notification settings - Fork 0
/
TGClient.m
127 lines (98 loc) · 3.93 KB
/
TGClient.m
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
116
117
118
119
120
121
122
123
124
125
126
127
//
// TGClient.m
// https://github.com/tagyro/TGS
//
#import "TGClient.h"
@implementation BasicEvent
@end
@implementation LoginEvent
@end
@implementation TGSession
+(JSONKeyMapper*)keyMapper
{
return [[JSONKeyMapper alloc] initWithDictionary:@{@"sessionId":@"id"}];
}
@end
@implementation TGClient
static NSString *API_KEY = nil;
//
static NSString *API_ROOT_SERVER = @"https://api.tapglue.com/0.2/";
static NSString *API_DATE_FORMAT = @"2015-03-20T18:15:40.8+01:00";
+ (void)setAPIKey:(NSString *)apiKey {
NSAssert(API_KEY == nil, @"API key should be set only once");
API_KEY = apiKey;
}
static TGClient * _sharedClient = nil;
+ (instancetype)sharedClient {
if (!_sharedClient) {
NSAssert(API_KEY != nil, @"setAPIKey: should be called first");
_sharedClient = [[TGClient alloc] init];
}
return _sharedClient;
}
- (instancetype)init {
self = [super init];
if (self) {
NSAssert(API_KEY != nil, @"setAPIKey: should be called first");
// __weak typeof(self) ws = self;
}
return self;
}
- (void)loginUser:(TGUser*)user {
NSString *authStr = [NSString stringWithFormat:@"%@:%@", API_KEY, @""];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64authData = [authData base64EncodedStringWithOptions:0];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", base64authData];
[[JSONHTTPClient requestHeaders] setValue:authValue forKey:@"authorization"];
[[JSONHTTPClient requestHeaders] setValue:@"application/json" forKey:@"Content-Type"];
[[JSONHTTPClient requestHeaders] setValue:@"application/json" forKey:@"accept"];
//
[JSONHTTPClient postJSONFromURLWithString:[API_ROOT_SERVER stringByAppendingString:[API_ROOT_SERVER stringByAppendingString:@"user/login"]]
bodyData:[user toJSONData]
completion:^(id json, JSONModelError *err) {
if (err) {
NSLog(@"error: %@",err);
} else {
NSLog(@"json: %@",json);
}
}];
}
- (void)createUser:(TGUser *)user {
//
NSString *authStr = [NSString stringWithFormat:@"%@:%@", API_KEY, @""];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64authData = [authData base64EncodedStringWithOptions:0];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", base64authData];
[[JSONHTTPClient requestHeaders] setValue:authValue forKey:@"authorization"];
[[JSONHTTPClient requestHeaders] setValue:@"application/json" forKey:@"Content-Type"];
[[JSONHTTPClient requestHeaders] setValue:@"application/json" forKey:@"accept"];
//
[JSONHTTPClient postJSONFromURLWithString:[API_ROOT_SERVER stringByAppendingString:@"users"]
bodyData:[user toJSONData]
completion:^(id json, JSONModelError *err) {
if (err) {
LoginEvent *le = [LoginEvent new];
le.error = err;
PUBLISH(le);
} else {
self.me = [[TGUser alloc] initWithDictionary:json error:nil];
//
PUBLISH([LoginEvent new]);
}
}];
}
@end
@implementation TGUser
+(JSONKeyMapper*)keyMapper
{
return [[JSONKeyMapper alloc] initWithDictionary:@{@"user_id":@"id"}];
}
@end
@implementation TGConnection
@end
@implementation TGDisplay
@end
@implementation TGObject
@end
@implementation TGEvent
@end