-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebAPI.m
118 lines (109 loc) · 4.49 KB
/
WebAPI.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
/**
* ©2022, SUZUKI PLAN
* License: https://github.com/suzukiplan/tohovgs4-ios/blob/master/LICENSE.txt
*/
#import "WebAPI.h"
#import "../ServerSettings.h"
#import "../model/SongList.h"
#include <CommonCrypto/CommonDigest.h>
#include <pthread.h>
#define REQUEST_TIMEDOUT_SECONDS 5
@interface WebAPI() <NSURLSessionDelegate>
@property (nonatomic) NSURLSession* session;
@end
@implementation WebAPI
- (instancetype)init
{
if (self = [super init]) {
NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.timeoutIntervalForRequest = REQUEST_TIMEDOUT_SECONDS;
config.timeoutIntervalForResource = 20;
_session = [NSURLSession sessionWithConfiguration:config
delegate:nil
delegateQueue:nil];
}
return self;
}
- (void)checkUpdateWithCurrentVersion:(NSString*)version
done:(void(^)(NSError* _Nullable, BOOL))done
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"badge"]) {
done(nil, YES);
return;
}
[self _httpGet:@"songlist.ver" done:^(NSError* error, NSString* response) {
if (error || !response) {
NSLog(@"checkUpdateWithCurrentSHA1 failed: %@", error);
done(error, NO);
} else {
NSLog(@"client version: %@", version);
NSLog(@"server version: %@", response);
done(nil, strncmp(version.UTF8String, response.UTF8String, 10) < 0);
}
}];
}
- (void)acquireSongList:(void(^)(NSError* _Nullable, SongList* _Nullable))done
{
[self _httpGet:@"songlist.json" done:^(NSError* error, NSString* response) {
if (error || !response) {
NSLog(@"acquireSongList failed: %@", error);
done(error, nil);
} else {
done(nil, [SongList fromJsonString:response]);
}
}];
}
- (void)acquireMmlWithSong:(Song*)song
done:(void(^)(NSError* _Nullable, NSString* _Nullable))done
{
NSString* mml = [NSString stringWithFormat:@"%@.mml", song.mml];
[self _httpGet:mml done:^(NSError* error, NSString* response) {
if (error || !response) {
NSLog(@"acquireSongList failed: %@", error);
done(error, nil);
} else {
NSLog(@"download mml succeed: %ld bytes", response.length);
done(nil, response);
}
}];
}
- (void)_httpGet:(NSString*)path
done:(void(^)(NSError* _Nullable error, NSString* _Nullable response))done
{
NSLog(@"get %@", path);
__weak WebAPI* weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString* urlString = [NSString stringWithFormat:@"%@%@", API_SERVER_BASE_URL, path];
NSURL* url = [NSURL URLWithString:urlString];
NSURLRequest* request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:REQUEST_TIMEDOUT_SECONDS];
NSURLSessionDataTask* task;
task = [weakSelf.session dataTaskWithRequest:request
completionHandler:^(NSData* _Nullable data,
NSURLResponse* _Nullable response,
NSError* _Nullable error) {
if (error || !data || !response || ![response isKindOfClass:[NSHTTPURLResponse class]]) {
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
done([NSError errorWithDomain:NSNetServicesErrorDomain
code:httpResponse.statusCode
userInfo:nil], nil);
} else {
done(error, nil);
}
return;
}
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
if (200 != httpResponse.statusCode) {
done([NSError errorWithDomain:NSNetServicesErrorDomain
code:httpResponse.statusCode
userInfo:nil], nil);
} else {
done(nil, [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
}];
[task resume];
});
}
@end