blob: cc7267b93d39962ef54e656f3b17056fe407cf44 (
plain)
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
|
#import "ThingsHelper.h"
NS_ASSUME_NONNULL_BEGIN
ThingsURLRoute const ThingsURLRouteSearch = @"search";
ThingsURLRoute const ThingsURLRouteShow = @"show";
ThingsURLRoute const ThingsURLRouteUpdate = @"update";
ThingsURLRoute const ThingsURLRouteUpdateProject = @"update-project";
ThingsURLRoute const ThingsURLRouteAdd = @"add";
ThingsURLRoute const ThingsURLRouteAddProject = @"add-project";
@implementation ThingsHelper
+ (instancetype)sharedInstance {
static ThingsHelper *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
+ (BOOL)requiresAuthentication:(ThingsURLRoute)route {
if ([route isEqualToString:ThingsURLRouteSearch] || [route isEqualToString:ThingsURLRouteShow]) {
return NO;
}
return YES;
}
+ (BOOL)callRoute:(ThingsURLRoute)route withParameters:(NSDictionary *)parameters {
NSString *authenticationKey = [KeychainHelper authenticationKey];
if ([self requiresAuthentication:route] && !authenticationKey) {
NSLog(@"Attempted to call an authenticated things URL without an authentication key.");
return NO;
}
NSString *baseURL = [NSString stringWithFormat:@"things:///%@", route];
NSMutableArray *queryComponents = [NSMutableArray array];
if ([self requiresAuthentication:route]) {
[queryComponents addObject:[NSString stringWithFormat:@"auth-token=%@", authenticationKey]];
}
for (NSString *key in parameters) {
id value = parameters[key];
NSString *encodedKey = [self urlEncodeString:key];
NSString *encodedValue = [self urlEncodeString:[NSString stringWithFormat:@"%@", value]];
[queryComponents addObject:[NSString stringWithFormat:@"%@=%@", encodedKey, encodedValue]];
}
NSString *urlString = baseURL;
if ([queryComponents count] > 0) {
urlString = [NSString stringWithFormat:@"%@?%@", baseURL, [queryComponents componentsJoinedByString:@"&"]];
}
NSURL *url = [NSURL URLWithString:urlString];
[[NSWorkspace sharedWorkspace] openURL:url];
return YES;
}
+ (NSString *)urlEncodeString:(NSString *)string {
NSCharacterSet *allowedCharacters = [NSCharacterSet URLQueryAllowedCharacterSet];
return [string stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
}
@end
NS_ASSUME_NONNULL_END
|