浏览代码

Refactored reddit api querying

Thomas Buck 11 年前
父节点
当前提交
dcb5564dcd
共有 3 个文件被更改,包括 55 次插入39 次删除
  1. 4
    0
      RedditBar/Reddit.h
  2. 50
    38
      RedditBar/Reddit.m
  3. 1
    1
      RedditBar/RedditBar-Info.plist

+ 4
- 0
RedditBar/Reddit.h 查看文件

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

+ 50
- 38
RedditBar/Reddit.m 查看文件

@@ -16,26 +16,6 @@ NSString *appName = @"RedditBar";
16 16
 
17 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 19
 -(id)initWithUsername:(NSString *)name Modhash:(NSString *)hash {
40 20
     self = [super init];
41 21
     if (self) {
@@ -60,30 +40,18 @@ NSString *appName = @"RedditBar";
60 40
     NSMutableString *stringData = [NSMutableString stringWithString:@"api_type=json"];
61 41
     [stringData appendFormat:@"&user=%@", [self urlencode: username]];
62 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 47
         return nil;
78 48
     } else {
79
-        NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response;
80
-        long code = [resp statusCode];
49
+        long code = [response statusCode];
81 50
         if (code == 200) {
51
+            NSError *error;
82 52
             id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
83
-            
84 53
             if (error)
85 54
                 return nil;
86
-            
87 55
             if([object isKindOfClass:[NSDictionary class]]) {
88 56
                 NSDictionary *results = object;
89 57
                 NSDictionary *json = [results valueForKey:@"json"];
@@ -102,8 +70,52 @@ NSString *appName = @"RedditBar";
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 92
 -(BOOL)isAuthenticated {
106 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 121
 @end

+ 1
- 1
RedditBar/RedditBar-Info.plist 查看文件

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

正在加载...
取消
保存