aboutsummaryrefslogtreecommitdiff
path: root/QSThingsPlugin/Helpers/ThingsHelper.m
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-09-10 22:48:37 +0200
committerRuben Beltran del Rio <git@r.bdr.sh>2025-09-10 22:48:37 +0200
commit1613c951d49e6d85aff52f312d57fa087b35c079 (patch)
treec31b74c56f32c3a60e628c187b6f7c236fffe36e /QSThingsPlugin/Helpers/ThingsHelper.m
parent7cf393095282077b0bbae3046826b67f4f20a8eb (diff)
Move things URL calls to a helper
Diffstat (limited to 'QSThingsPlugin/Helpers/ThingsHelper.m')
-rw-r--r--QSThingsPlugin/Helpers/ThingsHelper.m70
1 files changed, 70 insertions, 0 deletions
diff --git a/QSThingsPlugin/Helpers/ThingsHelper.m b/QSThingsPlugin/Helpers/ThingsHelper.m
new file mode 100644
index 0000000..cc7267b
--- /dev/null
+++ b/QSThingsPlugin/Helpers/ThingsHelper.m
@@ -0,0 +1,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