Use service catalog and update version
[pithos-ios] / Classes / GetObjectsRequest.m
1 //
2 //  GetObjectsRequest.m
3 //  OpenStack
4 //
5 //  Created by Mike Mayo on 12/24/10.
6 //  The OpenStack project is provided under the Apache 2.0 license.
7 //
8
9 #import "GetObjectsRequest.h"
10 #import "OpenStackAccount.h"
11 #import "Container.h"
12 #import "StorageObject.h"
13 #import "AccountManager.h"
14 #import "Folder.h"
15 #import "SBJSON.h"
16 #import "NSString+Conveniences.h"
17
18 @implementation GetObjectsRequest
19
20 @synthesize container;
21
22 #pragma mark - Constructors
23
24 + (id)request:(OpenStackAccount *)account method:(NSString *)method url:(NSURL *)url {
25         GetObjectsRequest *request = [[[self
26                                     alloc] initWithURL:url] autorelease];
27     request.account = account;
28         request.requestMethod = method;
29         [request addRequestHeader:@"X-Auth-Token" value:account.authToken];
30     [request addRequestHeader:@"Content-Type" value:@"application/json"];
31     request.timeOutSeconds = 60;
32     request.numberOfTimesToRetryOnTimeout = 5;
33     request.validatesSecureCertificate = !account.ignoreSSLErrors;
34         return request;
35 }
36
37 + (id)filesRequest:(OpenStackAccount *)account method:(NSString *)method path:(NSString *)path marker:(NSString *)marker {
38     NSString *urlString = [account.filesURL description];
39     if (account.sharingAccount) {
40         urlString = [NSString stringWithFormat:@"%@%@",
41                      [urlString substringToIndex:[urlString rangeOfString:account.username].location],
42                      account.sharingAccount];
43     }
44         NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@?format=json%@%@",
45                                        urlString,
46                                        path,
47                                        (account.shared ? @"&shared=" : @""),
48                                        (marker ? [NSString stringWithFormat:@"&marker=%@", marker] : @"")]];
49     return [self request:account method:method url:url];
50 }
51
52 + (id)filesRequest:(OpenStackAccount *)account method:(NSString *)method path:(NSString *)path {
53     return [self filesRequest:account method:method path:path marker:nil];
54 }
55
56 + (id)request:(OpenStackAccount *)account container:(Container *)container {
57     return [self request:account container:container marker:nil objectsBuffer:nil];
58 }
59
60 + (id)request:(OpenStackAccount *)account container:(Container *)container
61        marker:(NSString *)marker objectsBuffer:(NSMutableDictionary *)objectsBuffer {
62     GetObjectsRequest *request = [self filesRequest:account
63                                              method:@"GET"
64                                                path:[NSString stringWithFormat:@"/%@", [NSString encodeToPercentEscape:container.name]]
65                                              marker:[NSString encodeToPercentEscape:marker]];
66     request.container = container;
67     if (objectsBuffer == nil)
68         objectsBuffer = [NSMutableDictionary dictionary];
69     request.userInfo = [NSMutableDictionary dictionaryWithObject:objectsBuffer forKey:@"objectsBuffer"];
70     return request;
71 }
72
73 #pragma mark - ASIHTTPRequest Overrides
74
75 - (void)requestFinished {
76     if ([self isSuccess]) {
77         Container *aContainer = self.container;
78         NSMutableDictionary *objects = [self objects];
79         NSMutableDictionary *objectsBuffer = [self.userInfo objectForKey:@"objectsBuffer"];
80         [objectsBuffer addEntriesFromDictionary:objects];
81         if (objects.count < 10000) {
82             aContainer.rootFolder = [Folder folder];
83             aContainer.rootFolder.objects = objectsBuffer;
84             [self.account persist];
85             NSNotification *notification = [NSNotification notificationWithName:@"getObjectsSucceeded" object:self.container
86                                                                        userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
87                                                                                  aContainer, @"container",
88                                                                                  self, @"request",
89                                                                                  nil]];
90             [[NSNotificationCenter defaultCenter] postNotification:notification];
91         } else {
92             SBJSON *parser = [[[SBJSON alloc] init] autorelease];
93             NSArray *jsonObjects = [parser objectWithString:[self responseString]];
94             NSString *marker = [[jsonObjects lastObject] objectForKey:@"name"];
95             [self.account.manager getObjects:self.container afterMarker:marker objectsBuffer:objectsBuffer];
96         }        
97     } else {
98         NSNotification *notification = [NSNotification notificationWithName:@"getObjectsFailed" object:self.container userInfo:[NSDictionary dictionaryWithObject:self forKey:@"request"]];
99         [[NSNotificationCenter defaultCenter] postNotification:notification];
100     }
101     
102     [super requestFinished];
103 }
104
105 - (void)failWithError:(NSError *)theError {
106     NSNotification *notification = [NSNotification notificationWithName:@"getObjectsFailed" object:self.container userInfo:[NSDictionary dictionaryWithObject:self forKey:@"request"]];
107     [[NSNotificationCenter defaultCenter] postNotification:notification];
108     [super failWithError:theError];
109 }
110
111 #pragma mark - Memory Management
112
113 - (void)dealloc {
114     [container release];
115     [super dealloc];
116 }
117
118 @end