Revision 8fb25a1b
b/asi-http-request-with-pithos/Classes/Pithos/ASIPithos.h | ||
---|---|---|
37 | 37 |
#import <Foundation/Foundation.h> |
38 | 38 |
|
39 | 39 |
@interface ASIPithos : NSObject { |
40 |
NSString *authURL;
|
|
40 |
NSString *authToken;
|
|
41 | 41 |
NSString *authUser; |
42 |
NSString *authKey; |
|
43 | 42 |
|
44 |
NSString *storageURLPrefix; |
|
45 |
NSString *storageURL; |
|
46 |
NSString *authToken; |
|
43 |
BOOL ignoreSSLErrors; |
|
47 | 44 |
|
45 |
NSString *tokensURL; |
|
46 |
NSString *storageURLPrefix; |
|
47 |
NSString *userCatalogURL; |
|
48 | 48 |
NSString *publicURLPrefix; |
49 | 49 |
|
50 |
NSString *userCatalogURL;
|
|
50 |
NSString *storageURL;
|
|
51 | 51 |
} |
52 | 52 |
|
53 |
@property (strong) NSString *authURL;
|
|
53 |
@property (strong) NSString *authToken;
|
|
54 | 54 |
@property (strong) NSString *authUser; |
55 |
@property (strong) NSString *authKey; |
|
56 | 55 |
|
57 |
@property (strong) NSString *storageURLPrefix; |
|
58 |
@property (strong, nonatomic) NSString *storageURL; |
|
59 |
@property (strong) NSString *authToken; |
|
56 |
@property (assign) BOOL ignoreSSLErrors; |
|
60 | 57 |
|
58 |
@property (strong) NSString *tokensURL; |
|
59 |
@property (strong) NSString *storageURLPrefix; |
|
60 |
@property (strong) NSString *userCatalogURL; |
|
61 | 61 |
@property (strong) NSString *publicURLPrefix; |
62 | 62 |
|
63 |
@property (strong) NSString *userCatalogURL;
|
|
63 |
@property (readonly) NSString *storageURL;
|
|
64 | 64 |
|
65 | 65 |
+ (id)pithos; |
66 | 66 |
|
b/asi-http-request-with-pithos/Classes/Pithos/ASIPithos.m | ||
---|---|---|
38 | 38 |
#import "ASIPithosRequest.h" |
39 | 39 |
|
40 | 40 |
@implementation ASIPithos |
41 |
@synthesize authURL, authUser, authKey, storageURLPrefix, storageURL, authToken, publicURLPrefix, userCatalogURL;
|
|
41 |
@synthesize authToken, authUser, ignoreSSLErrors, tokensURL, storageURLPrefix, userCatalogURL, publicURLPrefix, storageURL;
|
|
42 | 42 |
|
43 | 43 |
#pragma mark - Object Lifecycle |
44 | 44 |
|
... | ... | |
48 | 48 |
} |
49 | 49 |
|
50 | 50 |
- (NSString *)description { |
51 |
return [NSString stringWithFormat:@"authURL: %@, authUser: %@, authKey: %@, storageURLPrefix: %@, storageURL: %@, authToken: %@, publicURLPrefix: %@, userCatalogURL: %@",
|
|
52 |
authURL, authUser, authKey, storageURLPrefix, storageURL, authToken, publicURLPrefix, userCatalogURL];
|
|
51 |
return [NSString stringWithFormat:@"authToken: %@, authUser: %@, ignoreSSLErrors: %d, tokensURL: %@, storageURLPrefix: %@, userCatalogURL: %@, publicURLPrefix: %@",
|
|
52 |
authToken, authUser, ignoreSSLErrors, tokensURL, storageURLPrefix, userCatalogURL, publicURLPrefix];
|
|
53 | 53 |
} |
54 | 54 |
|
55 |
|
|
56 | 55 |
#pragma mark - Properties |
57 | 56 |
|
58 |
- (NSString *)storageURL { |
|
59 |
if (storageURL) |
|
60 |
return storageURL; |
|
61 |
else if (storageURLPrefix && authUser) |
|
62 |
return [NSString stringWithFormat:@"%@/%@", storageURLPrefix, [ASIPithosRequest encodeToPercentEscape:authUser]]; |
|
63 |
return nil; |
|
64 |
} |
|
65 |
|
|
66 | 57 |
- (NSString *)storageURLWithAuthUser:(NSString *)anAuthUser { |
67 |
if (storageURLPrefix) |
|
58 |
if (storageURLPrefix && anAuthUser)
|
|
68 | 59 |
return [NSString stringWithFormat:@"%@/%@", storageURLPrefix, [ASIPithosRequest encodeToPercentEscape:anAuthUser]]; |
69 | 60 |
return nil; |
70 | 61 |
} |
71 | 62 |
|
63 |
- (NSString *)storageURL { |
|
64 |
return [self storageURLWithAuthUser:authUser]; |
|
65 |
} |
|
66 |
|
|
72 | 67 |
@end |
b/asi-http-request-with-pithos/Classes/Pithos/ASIPithosAccountRequest.m | ||
---|---|---|
46 | 46 |
#pragma mark Constructors |
47 | 47 |
|
48 | 48 |
+ (id)storageRequestWithMethod:(NSString *)method pithos:(ASIPithos *)pithos queryString:(NSString *)queryString { |
49 |
NSString *urlString = [NSString stringWithString:pithos.storageURL];
|
|
49 |
NSString *urlString = [[pithos.storageURL copy] autorelease];
|
|
50 | 50 |
if (queryString) |
51 | 51 |
urlString = [urlString stringByAppendingString:queryString]; |
52 | 52 |
|
53 |
ASIPithosAccountRequest *request = [[[self alloc] initWithURL:[NSURL URLWithString:urlString]] autorelease];
|
|
53 |
ASIPithosAccountRequest *request = [self requestWithURL:[NSURL URLWithString:urlString]];
|
|
54 | 54 |
[request setRequestMethod:method]; |
55 | 55 |
[request addRequestHeader:@"X-Auth-Token" value:pithos.authToken]; |
56 |
request.validatesSecureCertificate = !pithos.ignoreSSLErrors; |
|
56 | 57 |
return request; |
57 | 58 |
} |
58 | 59 |
|
b/asi-http-request-with-pithos/Classes/Pithos/ASIPithosContainerRequest.m | ||
---|---|---|
54 | 54 |
if (queryString) |
55 | 55 |
urlString = [urlString stringByAppendingString:queryString]; |
56 | 56 |
|
57 |
ASIPithosContainerRequest *request = [[[self alloc] initWithURL:[NSURL URLWithString:urlString]] autorelease];
|
|
57 |
ASIPithosContainerRequest *request = [self requestWithURL:[NSURL URLWithString:urlString]];
|
|
58 | 58 |
[request setRequestMethod:method]; |
59 | 59 |
[request addRequestHeader:@"X-Auth-Token" value:pithos.authToken]; |
60 | 60 |
request.containerName = containerName; |
61 |
request.validatesSecureCertificate = !pithos.ignoreSSLErrors; |
|
61 | 62 |
return request; |
62 | 63 |
} |
63 | 64 |
|
b/asi-http-request-with-pithos/Classes/Pithos/ASIPithosObjectRequest.m | ||
---|---|---|
60 | 60 |
|
61 | 61 |
+ (id)storageRequestWithMethod:(NSString *)method pithos:(ASIPithos *)pithos containerName:(NSString *)containerName objectName:(NSString *)objectName |
62 | 62 |
queryString:(NSString *)queryString { |
63 |
NSString *urlString = [NSString stringWithFormat:@"%@/%@/%@",
|
|
64 |
pithos.storageURL,
|
|
65 |
[self encodeToPercentEscape:containerName],
|
|
66 |
[((NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
|
|
67 |
(CFStringRef)objectName,
|
|
68 |
NULL,
|
|
69 |
(CFStringRef)@"!*'();:@&=+$,?%#[]",
|
|
63 |
NSString *urlString = [NSString stringWithFormat:@"%@/%@/%@", |
|
64 |
pithos.storageURL, |
|
65 |
[self encodeToPercentEscape:containerName], |
|
66 |
[((NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, |
|
67 |
(CFStringRef)objectName, |
|
68 |
NULL, |
|
69 |
(CFStringRef)@"!*'();:@&=+$,?%#[]", |
|
70 | 70 |
kCFStringEncodingUTF8)) autorelease]]; |
71 | 71 |
if (queryString) |
72 | 72 |
urlString = [urlString stringByAppendingString:queryString]; |
73 | 73 |
|
74 |
ASIPithosObjectRequest *request = [[[self alloc] initWithURL:[NSURL URLWithString:urlString]] autorelease];
|
|
74 |
ASIPithosObjectRequest *request = [self requestWithURL:[NSURL URLWithString:urlString]];
|
|
75 | 75 |
[request setRequestMethod:method]; |
76 | 76 |
[request addRequestHeader:@"X-Auth-Token" value:pithos.authToken]; |
77 | 77 |
request.containerName = containerName; |
78 | 78 |
request.objectName = objectName; |
79 |
request.validatesSecureCertificate = !pithos.ignoreSSLErrors; |
|
79 | 80 |
return request; |
80 | 81 |
} |
81 | 82 |
|
b/asi-http-request-with-pithos/Classes/Pithos/ASIPithosRequest.h | ||
---|---|---|
58 | 58 |
@property (retain) NSString *currentContent; |
59 | 59 |
@property (retain) ASIPithosAccount *currentAccount; |
60 | 60 |
|
61 |
+ (id)authenticationRequestWithPithos:(ASIPithos *)pithos; |
|
62 |
+ (ASIPithos *)authenticateWithPithos:(ASIPithos *)pithos error:(NSError **)error; |
|
61 |
// POST tokensURL |
|
62 |
+ (id)serviceCatalogRequestWithPithos:(ASIPithos *)pithos; |
|
63 |
|
|
64 |
- (NSDictionary *)access; |
|
65 |
- (NSDictionary *)token; |
|
66 |
- (NSArray *)serviceCatalog; |
|
67 |
- (NSArray *)user; |
|
63 | 68 |
|
64 | 69 |
// POST userCatalogURL |
65 | 70 |
+ (id)userCatalogRequestWithPithos:(ASIPithos *)pithos displaynames:(NSArray *)displaynames UUIDs:(NSArray *)UUIDs; |
... | ... | |
68 | 73 |
- (NSDictionary *)displaynameCatalog; |
69 | 74 |
- (NSDictionary *)UUIDCatalog; |
70 | 75 |
|
71 |
// GET authURL
|
|
76 |
// GET storageURLPrefix
|
|
72 | 77 |
+ (id)listSharingAccountsRequestWithPithos:(ASIPithos *)pithos; |
73 |
// GET authURL?[limit=limit]&[marker=marker]
|
|
78 |
// GET storageURLPrefix?[limit=limit]&[marker=marker]
|
|
74 | 79 |
+ (id)listSharingAccountsRequestWithPithos:(ASIPithos *)pithos limit:(NSUInteger)limit marker:(NSString *)marker; |
75 | 80 |
|
76 | 81 |
- (NSArray *)sharingAccounts; |
b/asi-http-request-with-pithos/Classes/Pithos/ASIPithosRequest.m | ||
---|---|---|
44 | 44 |
@synthesize currentElement, currentContent, currentAccount; |
45 | 45 |
|
46 | 46 |
#pragma mark - |
47 |
#pragma mark Constructors |
|
48 |
|
|
49 |
+ (id)authRequestWithMethod:(NSString *)method pithos:(ASIPithos *)pithos queryString:(NSString *)queryString useAuthToken:(BOOL)useAuthToken { |
|
50 |
NSString *urlString = [NSString stringWithString:pithos.authURL]; |
|
51 |
if (queryString) |
|
52 |
urlString = [urlString stringByAppendingString:queryString]; |
|
53 |
|
|
54 |
ASIPithosRequest *request = [self requestWithURL:[NSURL URLWithString:urlString]]; |
|
55 |
[request setRequestMethod:method]; |
|
56 |
if (useAuthToken) |
|
57 |
[request addRequestHeader:@"X-Auth-Token" value:pithos.authToken]; |
|
58 |
return request; |
|
59 |
} |
|
60 |
|
|
61 |
+ (id)authRequestWithMethod:(NSString *)method pithos:(ASIPithos *)pithos useAuthToken:(BOOL)useAuthToken { |
|
62 |
return [self authRequestWithMethod:method pithos:pithos queryString:nil useAuthToken:useAuthToken]; |
|
63 |
} |
|
64 |
|
|
65 |
#pragma mark - |
|
66 | 47 |
#pragma mark Memory Management |
67 | 48 |
|
68 | 49 |
- (void)dealloc { |
... | ... | |
74 | 55 |
} |
75 | 56 |
|
76 | 57 |
#pragma mark - |
77 |
#pragma mark Authentication
|
|
58 |
#pragma mark Service Catalog
|
|
78 | 59 |
|
79 |
+ (id)authenticationRequestWithPithos:(ASIPithos *)pithos { |
|
80 |
ASIPithosRequest *request = [self authRequestWithMethod:@"GET" pithos:pithos useAuthToken:NO]; |
|
81 |
[request addRequestHeader:@"X-Auth-User" value:pithos.authUser]; |
|
82 |
[request addRequestHeader:@"X-Auth-Key" value:pithos.authKey]; |
|
83 |
return request; |
|
60 |
// POST tokensURL |
|
61 |
+ (id)serviceCatalogRequestWithPithos:(ASIPithos *)pithos { |
|
62 |
ASIPithosRequest *request = [self requestWithURL:[NSURL URLWithString:pithos.tokensURL]]; |
|
63 |
[request setRequestMethod:@"POST"]; |
|
64 |
[request addRequestHeader:@"Content-Type" value:@"application/json"]; |
|
65 |
if (pithos.authToken.length) |
|
66 |
[request appendPostData:[[NSString stringWithFormat:@"{\"auth\":{\"token\":{\"id\":\"%@\"}}}", pithos.authToken] |
|
67 |
dataUsingEncoding:NSUTF8StringEncoding]]; |
|
68 |
request.validatesSecureCertificate = !pithos.ignoreSSLErrors; |
|
69 |
return request; |
|
84 | 70 |
} |
85 | 71 |
|
86 |
+ (ASIPithos *)authenticateWithPithos:(ASIPithos *)pithos error:(NSError **)error { |
|
87 |
ASIPithosRequest *request = [self authenticationRequestWithPithos:pithos]; |
|
88 |
[request startSynchronous]; |
|
89 |
// XXX start asynchronous with polling loop? |
|
90 |
|
|
91 |
if (error != NULL) { |
|
92 |
*error = [request error]; |
|
93 |
if (!*error) { |
|
94 |
NSDictionary *responseHeaders = [request responseHeaders]; |
|
95 |
pithos.authToken = [responseHeaders objectForKey:@"X-Auth-Token"]; |
|
96 |
pithos.storageURL = [responseHeaders objectForKey:@"X-Storage-Url"]; |
|
97 |
return pithos; |
|
98 |
} |
|
99 |
} |
|
100 |
return nil; |
|
72 |
- (NSDictionary *)access { |
|
73 |
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease]; |
|
74 |
NSDictionary *access = [[parser objectWithString:[self responseString]] objectForKey:@"access"]; |
|
75 |
return access; |
|
76 |
} |
|
77 |
|
|
78 |
- (NSDictionary *)token { |
|
79 |
return [[self access] objectForKey:@"token"]; |
|
80 |
} |
|
81 |
|
|
82 |
- (NSArray *)serviceCatalog { |
|
83 |
return [[self access] objectForKey:@"serviceCatalog"]; |
|
84 |
} |
|
85 |
|
|
86 |
- (NSArray *)user { |
|
87 |
return [[self access] objectForKey:@"user"]; |
|
101 | 88 |
} |
102 | 89 |
|
103 | 90 |
#pragma mark - |
... | ... | |
105 | 92 |
|
106 | 93 |
// POST userCatalogURL |
107 | 94 |
+ (id)userCatalogRequestWithPithos:(ASIPithos *)pithos displaynames:(NSArray *)displaynames UUIDs:(NSArray *)UUIDs { |
108 |
NSString *urlString = [NSString stringWithString:pithos.userCatalogURL]; |
|
109 |
|
|
110 |
ASIPithosRequest *request = [self requestWithURL:[NSURL URLWithString:urlString]]; |
|
95 |
ASIPithosRequest *request = [self requestWithURL:[NSURL URLWithString:pithos.userCatalogURL]]; |
|
111 | 96 |
[request setRequestMethod:@"POST"]; |
112 | 97 |
[request addRequestHeader:@"X-Auth-Token" value:pithos.authToken]; |
113 | 98 |
[request addRequestHeader:@"Content-Type" value:@"application/json"]; |
... | ... | |
130 | 115 |
[dataString appendFormat:@"]}"]; |
131 | 116 |
[request appendPostData:[dataString dataUsingEncoding:NSUTF8StringEncoding]]; |
132 | 117 |
|
118 |
request.validatesSecureCertificate = !pithos.ignoreSSLErrors; |
|
133 | 119 |
return request; |
134 | 120 |
} |
135 | 121 |
|
... | ... | |
150 | 136 |
#pragma mark - |
151 | 137 |
#pragma mark GET |
152 | 138 |
|
153 |
// GET authURL
|
|
139 |
// GET storageURLPrefix
|
|
154 | 140 |
+ (id)listSharingAccountsRequestWithPithos:(ASIPithos *)pithos { |
155 |
return [self authRequestWithMethod:@"GET" pithos:pithos queryString:@"?format=xml" useAuthToken:YES];
|
|
141 |
return [self listSharingAccountsRequestWithPithos:pithos limit:0 marker:nil];
|
|
156 | 142 |
} |
157 | 143 |
|
158 |
// GET authURL?[limit=limit]&[marker=marker]
|
|
144 |
// GET storageURLPrefix?[limit=limit]&[marker=marker]
|
|
159 | 145 |
+ (id)listSharingAccountsRequestWithPithos:(ASIPithos *)pithos limit:(NSUInteger)limit marker:(NSString *)marker { |
160 |
NSString *queryString = @"?format=xml";
|
|
146 |
NSString *urlString = [NSString stringWithFormat:@"%@/?format=xml", pithos.storageURLPrefix];
|
|
161 | 147 |
if (limit && (limit > 0)) |
162 |
queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&limit=%lu", limit]];
|
|
148 |
urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"&limit=%lu", limit]];
|
|
163 | 149 |
if (marker) |
164 |
queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&marker=%@", [self encodeToPercentEscape:marker]]]; |
|
165 |
|
|
166 |
return [self authRequestWithMethod:@"GET" pithos:pithos queryString:queryString useAuthToken:YES]; |
|
150 |
urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"&marker=%@", [self encodeToPercentEscape:marker]]]; |
|
151 |
ASIPithosRequest *request = [self requestWithURL:[NSURL URLWithString:urlString]]; |
|
152 |
[request setRequestMethod:@"GET"]; |
|
153 |
[request addRequestHeader:@"X-Auth-Token" value:pithos.authToken]; |
|
154 |
request.validatesSecureCertificate = !pithos.ignoreSSLErrors; |
|
155 |
return request; |
|
167 | 156 |
} |
168 | 157 |
|
169 | 158 |
- (NSArray *)sharingAccounts { |
... | ... | |
301 | 290 |
|
302 | 291 |
- (void)setRequestUserFromDefaultTo:(NSString *)newAuthUser withPithos:(ASIPithos *)pithos { |
303 | 292 |
NSString *urlString = [self.url description]; |
304 |
NSRange storageURLRange = [urlString rangeOfString:pithos.storageURL];
|
|
293 |
NSRange storageURLRange = [urlString rangeOfString:[pithos.storageURL description]];
|
|
305 | 294 |
if (storageURLRange.length && (storageURLRange.location == 0)) |
306 |
self.url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",
|
|
307 |
[pithos storageURLWithAuthUser:newAuthUser],
|
|
295 |
self.url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", |
|
296 |
[pithos storageURLWithAuthUser:newAuthUser], |
|
308 | 297 |
[urlString substringFromIndex:storageURLRange.length]]]; |
309 | 298 |
} |
310 | 299 |
|
311 | 300 |
- (void)changeRequestUserFrom:(NSString *)oldAuthUser To:(NSString *)newAuthUser withPithos:(ASIPithos *)pithos { |
312 | 301 |
NSString *urlString = [self.url description]; |
313 |
NSRange storageURLRange = [urlString rangeOfString:[pithos storageURLWithAuthUser:oldAuthUser]];
|
|
302 |
NSRange storageURLRange = [urlString rangeOfString:[[pithos storageURLWithAuthUser:oldAuthUser] description]];
|
|
314 | 303 |
if (storageURLRange.length && (storageURLRange.location == 0)) |
315 |
self.url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",
|
|
316 |
[pithos storageURLWithAuthUser:newAuthUser],
|
|
304 |
self.url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", |
|
305 |
[pithos storageURLWithAuthUser:newAuthUser], |
|
317 | 306 |
[urlString substringFromIndex:storageURLRange.length]]]; |
318 | 307 |
} |
319 | 308 |
|
Also available in: Unified diff