Pushr (master) :  summary log tree commit diff
path: root/tests/get_full_token.m blob: 392268be5cf8f14940993cc4a6dd0cea3bfc0c4b
1#import <Foundation/Foundation.h>
2
3#include "common.h"
4
5NSDictionary *getFullToken(NSString *miniToken)
6{
7 NSArray *keys = [NSArray arrayWithObjects: @"api_key", @"method", @"mini_token", nil];
8 NSArray *vals = [NSArray arrayWithObjects: PUSHR_API_KEY, FLICKR_GET_TOKEN, miniToken, nil];
9 NSDictionary *params = [NSDictionary dictionaryWithObjects: vals forKeys: keys];
10
11 NSURL *url = signedURL(params);
12 NSData *responseData = [NSData dataWithContentsOfURL: url];
13 NSError *err = nil;
14
15 id responseDoc = [[NSClassFromString(@"NSXMLDocument") alloc] initWithData: responseData options: 0 error: &err];
16
17 NSXMLNode *rsp = [[responseDoc children] objectAtIndex: 0];
18#ifdef DEBUG_PARANOID
19 if (![[rsp name] isEqualToString: @"rsp"]) {
20 NSLog(@"This is not an <rsp> tag! Bailing out.");
21 return [NSArray array];
22 }
23#endif
24
25 NSLog(@"So the response is valid...");
26
27 id e = [[NSClassFromString(@"NSXMLElement") alloc] initWithXMLString: [rsp XMLString] error: &err];
28 if (![[[e attributeForName:@"stat"] stringValue] isEqualToString: @"ok"]) {
29 NSLog(@"The status is not 'ok', and we have no error handling!");
30 return [NSArray array];
31 } else {
32 NSLog(@"Status is 'ok'");
33 }
34
35 NSMutableDictionary *flickrDict = [NSMutableDictionary dictionaryWithCapacity: 3];
36 NSArray *nodes = [[[e children] lastObject] children];
37 NSEnumerator *chain = [nodes objectEnumerator];
38 NSXMLNode *node = nil;
39
40 while ((node = [chain nextObject])) {
41 if ([[node name] isEqualToString: @"token"]) {
42 NSLog(@"Token: %@", [node stringValue]);
43 [flickrDict setObject: [node stringValue] forKey: @"token"];
44 } else if ([[node name] isEqualToString: @"user"]) {
45 id element = [[NSClassFromString(@"NSXMLElement") alloc] initWithXMLString: [node XMLString] error: &err];
46 NSLog(@"Username: %@", [element attributeForName: @"username"]);
47 [flickrDict setObject: [[element attributeForName: @"username"] stringValue] forKey: @"username"];
48 NSLog(@"NSID: %@", [element attributeForName: @"nsid"]);
49 [flickrDict setObject: [[element attributeForName: @"nsid"] stringValue] forKey: @"nsid"];
50 }
51 }
52
53 return [NSDictionary dictionaryWithDictionary: flickrDict];
54}
55
56int main(int a, char **b)
57{
58 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
59
60 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
61 NSDictionary *args = [defaults dictionaryRepresentation];
62
63 if (![[args allKeys] containsObject: @"minitoken"]) {
64 fprintf(stderr, "You need to supply a '-minitoken' argument.\n");
65 [pool release];
66 return -1;
67 }
68
69 NSString *mt = [defaults stringForKey: @"minitoken"];
70
71 NSLog(@"Result of getFullToken: %@", getFullToken(mt));
72
73 [pool release];
74 return 0;
75}
76