Browse Source

Refactored reddit api querying

Thomas Buck 11 years ago
parent
commit
dcb5564dcd
3 changed files with 55 additions and 39 deletions
  1. 4
    0
      RedditBar/Reddit.h
  2. 50
    38
      RedditBar/Reddit.m
  3. 1
    1
      RedditBar/RedditBar-Info.plist

+ 4
- 0
RedditBar/Reddit.h View File

20
 -(id)initWithUsername:(NSString *)name Modhash:(NSString *)hash;
20
 -(id)initWithUsername:(NSString *)name Modhash:(NSString *)hash;
21
 -(BOOL)isAuthenticated;
21
 -(BOOL)isAuthenticated;
22
 
22
 
23
+-(NSData *)queryAPI:(NSString *)api withData:(NSString *)string andResponse:(NSHTTPURLResponse **)res;
24
+-(NSString *)urlencode:(NSString *)string;
25
+
26
+
23
 @end
27
 @end

+ 50
- 38
RedditBar/Reddit.m View File

16
 
16
 
17
 @synthesize username, modhash, password;
17
 @synthesize username, modhash, password;
18
 
18
 
19
--(NSString *)urlencode:(NSString *)string {
20
-    NSMutableString *output = [NSMutableString string];
21
-    const unsigned char *source = (const unsigned char *)[string UTF8String];
22
-    long sourceLen = strlen((const char *)source);
23
-    for (int i = 0; i < sourceLen; i++) {
24
-        const unsigned char thisChar = source[i];
25
-        if (thisChar == ' '){
26
-            [output appendString:@"+"];
27
-        } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||
28
-                   (thisChar >= 'a' && thisChar <= 'z') ||
29
-                   (thisChar >= 'A' && thisChar <= 'Z') ||
30
-                   (thisChar >= '0' && thisChar <= '9')) {
31
-            [output appendFormat:@"%c", thisChar];
32
-        } else {
33
-            [output appendFormat:@"%%%02X", thisChar];
34
-        }
35
-    }
36
-    return output;
37
-}
38
-
39
 -(id)initWithUsername:(NSString *)name Modhash:(NSString *)hash {
19
 -(id)initWithUsername:(NSString *)name Modhash:(NSString *)hash {
40
     self = [super init];
20
     self = [super init];
41
     if (self) {
21
     if (self) {
60
     NSMutableString *stringData = [NSMutableString stringWithString:@"api_type=json"];
40
     NSMutableString *stringData = [NSMutableString stringWithString:@"api_type=json"];
61
     [stringData appendFormat:@"&user=%@", [self urlencode: username]];
41
     [stringData appendFormat:@"&user=%@", [self urlencode: username]];
62
     [stringData appendFormat:@"&passwd=%@", [self urlencode: password]];
42
     [stringData appendFormat:@"&passwd=%@", [self urlencode: password]];
63
-    NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
64
-    NSString *requestBodyLength = [NSString stringWithFormat:@"%lu", (unsigned long)[requestBodyData length]];
65
-    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://ssl.reddit.com/api/login"]];
66
-    [request setTimeoutInterval:5.0];
67
-    [request setCachePolicy:NSURLCacheStorageNotAllowed];
68
-    [request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
69
-    [request setValue:requestBodyLength forHTTPHeaderField:@"Content-Length"];
70
-    [request setValue:[NSString stringWithFormat:@"%@/%@ by %@", appName, version, author] forHTTPHeaderField:@"User-Agent"];
71
-    [request setHTTPMethod:@"POST"];
72
-    [request setHTTPBody:requestBodyData];
73
-    NSURLResponse *response = nil;
74
-    NSError *error = nil;
75
-    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
76
-    if (error != nil) {
43
+    [stringData appendString:@"&rem=True"];
44
+    NSHTTPURLResponse *response;
45
+    NSData *data = [self queryAPI:@"api/login" withData:stringData andResponse:&response];
46
+    if (data == nil) {
77
         return nil;
47
         return nil;
78
     } else {
48
     } else {
79
-        NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response;
80
-        long code = [resp statusCode];
49
+        long code = [response statusCode];
81
         if (code == 200) {
50
         if (code == 200) {
51
+            NSError *error;
82
             id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
52
             id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
83
-            
84
             if (error)
53
             if (error)
85
                 return nil;
54
                 return nil;
86
-            
87
             if([object isKindOfClass:[NSDictionary class]]) {
55
             if([object isKindOfClass:[NSDictionary class]]) {
88
                 NSDictionary *results = object;
56
                 NSDictionary *results = object;
89
                 NSDictionary *json = [results valueForKey:@"json"];
57
                 NSDictionary *json = [results valueForKey:@"json"];
102
     }
70
     }
103
 }
71
 }
104
 
72
 
73
+-(NSData *)queryAPI:(NSString *)api withData:(NSString *)string andResponse:(NSHTTPURLResponse **)res {
74
+    NSData *requestBodyData = [string dataUsingEncoding:NSUTF8StringEncoding];
75
+    NSString *requestBodyLength = [NSString stringWithFormat:@"%lu", (unsigned long)[requestBodyData length]];
76
+    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self getAPIPoint:api]];
77
+    [request setTimeoutInterval:5.0];
78
+    [request setCachePolicy:NSURLCacheStorageNotAllowed];
79
+    [request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
80
+    [request setValue:requestBodyLength forHTTPHeaderField:@"Content-Length"];
81
+    [request setValue:[NSString stringWithFormat:@"%@/%@ by %@", appName, version, author] forHTTPHeaderField:@"User-Agent"];
82
+    [request setHTTPMethod:@"POST"];
83
+    [request setHTTPBody:requestBodyData];
84
+    NSError *error = nil;
85
+    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:res error:&error];
86
+    if (error)
87
+        return nil;
88
+    else
89
+        return data;
90
+}
91
+
105
 -(BOOL)isAuthenticated {
92
 -(BOOL)isAuthenticated {
106
     return FALSE;
93
     return FALSE;
107
 }
94
 }
108
 
95
 
96
+-(NSURL *)getAPIPoint:(NSString *)where {
97
+    NSString *url = @"https://ssl.reddit.com";
98
+    return [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", url, where]];
99
+}
100
+
101
+-(NSString *)urlencode:(NSString *)string {
102
+    NSMutableString *output = [NSMutableString string];
103
+    const unsigned char *source = (const unsigned char *)[string UTF8String];
104
+    long sourceLen = strlen((const char *)source);
105
+    for (int i = 0; i < sourceLen; i++) {
106
+        const unsigned char thisChar = source[i];
107
+        if (thisChar == ' '){
108
+            [output appendString:@"+"];
109
+        } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||
110
+                   (thisChar >= 'a' && thisChar <= 'z') ||
111
+                   (thisChar >= 'A' && thisChar <= 'Z') ||
112
+                   (thisChar >= '0' && thisChar <= '9')) {
113
+            [output appendFormat:@"%c", thisChar];
114
+        } else {
115
+            [output appendFormat:@"%%%02X", thisChar];
116
+        }
117
+    }
118
+    return output;
119
+}
120
+
109
 @end
121
 @end

+ 1
- 1
RedditBar/RedditBar-Info.plist View File

21
 	<key>CFBundleSignature</key>
21
 	<key>CFBundleSignature</key>
22
 	<string>????</string>
22
 	<string>????</string>
23
 	<key>CFBundleVersion</key>
23
 	<key>CFBundleVersion</key>
24
-	<string>113</string>
24
+	<string>115</string>
25
 	<key>LSApplicationCategoryType</key>
25
 	<key>LSApplicationCategoryType</key>
26
 	<string>public.app-category.utilities</string>
26
 	<string>public.app-category.utilities</string>
27
 	<key>LSMinimumSystemVersion</key>
27
 	<key>LSMinimumSystemVersion</key>

Loading…
Cancel
Save