Remove unnecessary code and resources
[pithos-ios] / Classes / APICallback.m
1 //
2 //  APICallback.h
3 //  OpenStack
4 //
5 //  Created by Mike Mayo on 03/23/11.
6 //  The OpenStack project is provided under the Apache 2.0 license.
7 //
8
9 #import "APICallback.h"
10 #import "NSObject+Conveniences.h"
11
12 @implementation APICallback
13
14 @synthesize uuid, url, verb, account, request;
15
16 - (id)initWithAccount:(OpenStackAccount *)anAccount url:(NSURL *)targetURL {
17     if ((self = [super init])) {
18         self.uuid = [APICallback stringWithUUID];
19         self.url = targetURL;
20         self.verb = @"GET";
21         self.account = anAccount;
22     }
23     return self;
24 }
25
26 - (id)initWithAccount:(OpenStackAccount *)anAccount url:(NSURL *)targetURL verb:(NSString *)requestVerb {
27     if ((self = [super init])) {
28         self.uuid = [APICallback stringWithUUID];
29         self.url = targetURL;
30         self.verb = requestVerb;
31         self.account = anAccount;
32     }
33     return self;
34 }
35
36 - (id)initWithAccount:(OpenStackAccount *)anAccount request:(OpenStackRequest *)openStackRequest {
37     if ((self = [super init])) {
38         self.uuid = [APICallback stringWithUUID];
39         self.url = openStackRequest.url;
40         self.verb = openStackRequest.requestMethod;
41         self.account = anAccount;
42         self.request = openStackRequest;
43     }
44     return self;
45 }
46
47 - (void)success:(APIResponseBlock)successBlock failure:(APIResponseBlock)failureBlock {
48
49     NSString *successName = [NSString stringWithFormat:@"SUCCESS %@ %@%@", self.verb, [self.url description], (self.request ? [NSString stringWithFormat:@" %@", self.uuid] : @"")];
50     successObserver = [[NSNotificationCenter defaultCenter] addObserverForName:successName object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notification)
51     {
52         successBlock([notification.userInfo objectForKey:@"response"]);
53         if (self.request) {
54             [[NSNotificationCenter defaultCenter] removeObserver:successObserver];
55             [[NSNotificationCenter defaultCenter] removeObserver:failureObserver];
56         }
57     }];
58     
59     NSString *failureName = [NSString stringWithFormat:@"FAILURE %@ %@%@", self.verb, [self.url description], (self.request ? [NSString stringWithFormat:@" %@", self.uuid] : @"")];
60     failureObserver = [[NSNotificationCenter defaultCenter] addObserverForName:failureName object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notification)
61     {
62         failureBlock([notification.userInfo objectForKey:@"response"]);
63         if (self.request) {
64             [[NSNotificationCenter defaultCenter] removeObserver:successObserver];
65             [[NSNotificationCenter defaultCenter] removeObserver:failureObserver];
66         }
67     }];    
68 }
69
70 - (void)dealloc {
71     [uuid release];
72     [url release];
73     [verb release];
74     [account release];
75     [request release];
76     [super dealloc];
77 }
78
79 @end