Statistics
| Branch: | Revision:

root / asi-http-request-with-pithos / Classes / CloudFiles / ASICloudFilesRequest.m @ be116d22

History | View | Annotate | Download (3.4 kB)

1
//
2
//  ASICloudFilesRequest.m
3
//  Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest
4
//
5
//  Created by Michael Mayo on 22/12/09.
6
//  Copyright 2009 All-Seeing Interactive. All rights reserved.
7
//
8
// A class for accessing data stored on Rackspace's Cloud Files Service
9
// http://www.rackspacecloud.com/cloud_hosting_products/files
10
// 
11
// Cloud Files Developer Guide:
12
// http://docs.rackspacecloud.com/servers/api/cs-devguide-latest.pdf
13

    
14
#import "ASICloudFilesRequest.h"
15

    
16
static NSString *username = nil;
17
static NSString *apiKey = nil;
18
static NSString *authToken = nil;
19
static NSString *storageURL = nil;
20
static NSString *cdnManagementURL = nil;
21
static NSString *rackspaceCloudAuthURL = @"https://auth.api.rackspacecloud.com/v1.0";
22

    
23
static NSRecursiveLock *accessDetailsLock = nil;
24

    
25
@implementation ASICloudFilesRequest
26

    
27
+ (void)initialize
28
{
29
	if (self == [ASICloudFilesRequest class]) {
30
		accessDetailsLock = [[NSRecursiveLock alloc] init];
31
	}
32
}
33

    
34
#pragma mark -
35
#pragma mark Attributes and Service URLs
36

    
37
+ (NSString *)authToken {
38
	return authToken;
39
}
40

    
41
+ (NSString *)storageURL {
42
	return storageURL;
43
}
44

    
45
+ (NSString *)cdnManagementURL {
46
	return cdnManagementURL;
47
}
48

    
49
#pragma mark -
50
#pragma mark Authentication
51

    
52
+ (id)authenticationRequest
53
{
54
	[accessDetailsLock lock];
55
	ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:rackspaceCloudAuthURL]] autorelease];
56
	[request addRequestHeader:@"X-Auth-User" value:username];
57
	[request addRequestHeader:@"X-Auth-Key" value:apiKey];
58
	[accessDetailsLock unlock];
59
	return request;
60
}
61

    
62
+ (NSError *)authenticate
63
{
64
	[accessDetailsLock lock];
65
	ASIHTTPRequest *request = [ASICloudFilesRequest authenticationRequest];
66
	[request startSynchronous];
67
	
68
	if (![request error]) {
69
		NSDictionary *responseHeaders = [request responseHeaders];
70
		authToken = [responseHeaders objectForKey:@"X-Auth-Token"];
71
		storageURL = [responseHeaders objectForKey:@"X-Storage-Url"];
72
		cdnManagementURL = [responseHeaders objectForKey:@"X-Cdn-Management-Url"];
73
	}
74
	[accessDetailsLock unlock];
75
	return [request error];
76
}
77

    
78
+ (NSString *)username
79
{
80
	return username;
81
}
82

    
83
+ (void)setUsername:(NSString *)newUsername
84
{
85
	[accessDetailsLock lock];
86
	[username release];
87
	username = [newUsername retain];
88
	[accessDetailsLock unlock];
89
}
90

    
91
+ (NSString *)apiKey {
92
	return apiKey;
93
}
94

    
95
+ (void)setApiKey:(NSString *)newApiKey
96
{
97
	[accessDetailsLock lock];
98
	[apiKey release];
99
	apiKey = [newApiKey retain];
100
	[accessDetailsLock unlock];
101
}
102

    
103
#pragma mark -
104
#pragma mark Date Parser
105

    
106
-(NSDate *)dateFromString:(NSString *)dateString
107
{
108
	// We store our date formatter in the calling thread's dictionary
109
	// NSDateFormatter is not thread-safe, this approach ensures each formatter is only used on a single thread
110
	// This formatter can be reused many times in parsing a single response, so it would be expensive to keep creating new date formatters
111
	NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary];
112
	NSDateFormatter *dateFormatter = [threadDict objectForKey:@"ASICloudFilesResponseDateFormatter"];
113
	if (dateFormatter == nil) {
114
		dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
115
		[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
116
		// example: 2009-11-04T19:46:20.192723
117
		[dateFormatter setDateFormat:@"yyyy-MM-dd'T'H:mm:ss.SSSSSS"];
118
		[threadDict setObject:dateFormatter forKey:@"ASICloudFilesResponseDateFormatter"];
119
	}
120
	return [dateFormatter dateFromString:dateString];
121
}
122

    
123
@end