/RackspaceCloudFiles/Source/RSClient.h

https://github.com/rackerlabs/ios-cloudfiles · C Header · 277 lines · 67 code · 60 blank · 150 comment · 0 complexity · 9b914dc50eee8eb0aa9f7f903a69df3f MD5 · raw file

  1. //
  2. // RSClient.h
  3. // CloudFilesSDKDemo
  4. //
  5. // Created by Mike Mayo on 10/25/11.
  6. // Copyright (c) 2011 Rackspace. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "RSContainer.h"
  10. #import "RSCDNContainer.h"
  11. #import "RSStorageObject.h"
  12. #define kRSDefaultTTL 259200
  13. #define EAUTHFAILURE 1 /* Authentication failed */
  14. static NSString *RSErrorDomain = @"RSErrorDomain";
  15. /** Rackspace API provider types */
  16. typedef enum {
  17. RSProviderTypeRackspaceUS,
  18. RSProviderTypeRackspaceUK
  19. } RSProviderType;
  20. /**
  21. The RSClient class is the root class for the Rackspace Cloud Files SDK. You use it to authenticate
  22. with your account and work with containers and CDN containers. You must have a RSClient object available
  23. in your code to use the other classes.
  24. Your username is the username you use to login to http://manage.rackspacecloud.com, and your API Key
  25. is available in the My Account section of http://manage.rackspacecloud.com. If you are a UK user,
  26. pass `RSProviderTypeRackspaceUK` to the provider argument in the constructor. If you are using
  27. OpenStack Swift, you can provide your own authentication URL.
  28. Once you have created your object, you can optionally authenticate before performing any operations.
  29. If you do not authenticate, the client will authenticate for you before performing any other
  30. API operations.
  31. */
  32. @interface RSClient : RSModel
  33. #pragma mark - Properties
  34. /** The username for this account */
  35. @property (nonatomic, strong) NSString *username;
  36. /** The API Key for this account. For Rackspace Cloud accounts, this is available in the
  37. * My Account section of https://manage.rackspacecloud.com.
  38. */
  39. @property (nonatomic, strong) NSString *apiKey;
  40. /** The authentication URL for the API */
  41. @property (nonatomic, strong) NSURL *authURL;
  42. /** This property is set to YES when the client object has authenticated with the API. */
  43. @property (nonatomic) BOOL authenticated;
  44. /** The authentication token returned by the authentication API. The auth token is used
  45. * to authenticate for other API options, but it is ephemeral, so you still need to authenticate
  46. * at the beginning of a session with your username and API key.
  47. */
  48. @property (nonatomic, strong) NSString *authToken;
  49. /** Base URL string for the Cloud Files Storage API */
  50. @property (nonatomic, strong) NSString *storageURL;
  51. /** Base URL string for the Cloud Files CDN Management API */
  52. @property (nonatomic, strong) NSString *cdnManagementURL;
  53. /** The total number of containers in this account */
  54. @property (nonatomic) NSUInteger containerCount;
  55. /** The total number of bytes stored in this account */
  56. @property (nonatomic) NSUInteger totalBytesUsed;
  57. #pragma mark - Constructors
  58. /** Creates a RSClient object with the specified provider, username, and API key.
  59. * @provider The cloud provider to use
  60. * @param username Your username
  61. * @param apiKey Your API key, available in the My Account section of http://manage.rackspacecloud.com for Rackspace Cloud accounts
  62. */
  63. - (id)initWithProvider:(RSProviderType)provider username:(NSString *)username apiKey:(NSString *)apiKey;
  64. /** Creates a RSClient object with the specified auth URL, username, and API key.
  65. * @param authURL The URL to use for authentication
  66. * @param username Your username
  67. * @param apiKey Your API key, available in the My Account section of http://manage.rackspacecloud.com for Rackspace Cloud accounts
  68. */
  69. - (id)initWithAuthURL:(NSURL *)authURL username:(NSString *)username apiKey:(NSString *)apiKey;
  70. #pragma mark - Common
  71. /** Returns a request to the Storage API with the given path and HTTP method
  72. * @param path The path in the API. Example: /containers
  73. * @param httpMethod The HTTP method to use
  74. */
  75. - (NSMutableURLRequest *)storageRequest:(NSString *)path httpMethod:(NSString *)httpMethod;
  76. /** Returns a GET request to the Storage API with the given path
  77. * @param path The path in the API. Example: /containers
  78. */
  79. - (NSMutableURLRequest *)storageRequest:(NSString *)path;
  80. /** Returns a request to the CDN Management API with the given path and HTTP method
  81. * @param path The path in the API. Example: /containers
  82. * @param httpMethod The HTTP method to use
  83. */
  84. - (NSMutableURLRequest *)cdnRequest:(NSString *)path httpMethod:(NSString *)httpMethod;
  85. /** Returns a GET request to the CDN Management API with the given path
  86. * @param path The path in the API. Example: /containers
  87. */
  88. - (NSMutableURLRequest *)cdnRequest:(NSString *)path;
  89. /** Asynchronously sends a HTTP request with callbacks for success and failure responses.
  90. * @param requestSelector A selector to retrieve the HTTP request to send
  91. * @param object Optional argument for the requestSelector
  92. * @param sender The object that owns the requestSelector
  93. * @param successHandler A block that will be executed if the request is successfully executed and returns a HTTP response code in the 2xx block (200-299)
  94. * @param failureHandler A block that will be executed if the request is not successfully executed or returns a HTTP response code outside of the 2xx block (for example, a 404 Not Found response)
  95. */
  96. - (void)sendAsynchronousRequest:(SEL)requestSelector object:(id)object sender:(id)sender successHandler:(void (^)(NSHTTPURLResponse*, NSData*, NSError*))successHandler failureHandler:(void (^)(NSHTTPURLResponse*, NSData*, NSError*))failureHandler;
  97. /** Asynchronously sends a HTTP request with callbacks for success and failure responses.
  98. * @param requestSelector A selector to retrieve the HTTP request to send
  99. * @param sender The object that owns the requestSelector
  100. * @param successHandler A block that will be executed if the request is successfully executed and returns a HTTP response code in the 2xx block (200-299)
  101. * @param failureHandler A block that will be executed if the request is not successfully executed or returns a HTTP response code outside of the 2xx block (for example, a 404 Not Found response)
  102. */
  103. - (void)sendAsynchronousRequest:(SEL)requestSelector sender:(id)sender successHandler:(void (^)(NSHTTPURLResponse*, NSData*, NSError*))successHandler failureHandler:(void (^)(NSHTTPURLResponse*, NSData*, NSError*))failureHandler;
  104. #pragma mark - Authentication
  105. /** Returns a request object that represents an authentication request to the API */
  106. - (NSURLRequest *)authenticationRequest;
  107. /** Authenticates with the API. If you do not authenticate before performing any API operations,
  108. * RSClient will authenticate for you.
  109. * @param successHandler Executes if successful
  110. * @param failureHandler Executes if not successful
  111. */
  112. - (void)authenticate:(void (^)())successHandler failure:(void (^)(NSHTTPURLResponse*, NSData*, NSError*))failureHandler;
  113. #pragma mark - Storage Services
  114. #pragma mark Get Account Metadata
  115. /** Returns a request object that represents a request for account metadata */
  116. - (NSURLRequest *)getAccountMetadataRequest;
  117. /** Retrieves account metadata (number of containers and total bytes used) from the API.
  118. * @param successHandler Executes if successful
  119. * @param failureHandler Executes if not successful
  120. */
  121. - (void)getAccountMetadata:(void (^)())successHandler failure:(void (^)(NSHTTPURLResponse*, NSData*, NSError*))failureHandler;
  122. #pragma mark Get Containers
  123. /** Returns a request object that represents a request for an account's containers */
  124. - (NSURLRequest *)getContainersRequest;
  125. /** Returns a request object that represents a request for an account's containers.
  126. * @param limit The maximum number of containers to return
  127. * @param marker Pass a value here to return object names greater than the marker you specify
  128. */
  129. - (NSURLRequest *)getContainersRequestWithLimit:(NSUInteger)limit marker:(NSString *)marker;
  130. /** Retrieves a list of containers in your account.
  131. * @param successHandler Executes if successful
  132. * @param failureHandler Executes if not successful
  133. */
  134. - (void)getContainers:(void (^)(NSArray *containers, NSError *jsonError))successHandler failure:(void (^)(NSHTTPURLResponse*, NSData*, NSError*))failureHandler;
  135. #pragma mark Create Container
  136. /** Returns a request object that represents a request to create a container
  137. * @param container The container to create
  138. */
  139. - (NSURLRequest *)createContainerRequest:(RSContainer *)container;
  140. /** Creates a container in your account.
  141. * @param container The container to create
  142. * @param successHandler Executes if successful
  143. * @param failureHandler Executes if not successful
  144. */
  145. - (void)createContainer:(RSContainer *)container success:(void (^)())successHandler failure:(void (^)(NSHTTPURLResponse*, NSData*, NSError*))failureHandler;
  146. #pragma mark Delete Container
  147. /** Returns a request object that represents a request to delete a container
  148. * @param container The container to delete
  149. */
  150. - (NSURLRequest *)deleteContainerRequest:(RSContainer *)container;
  151. /** Deletes a container from your account.
  152. * @param container The container to delete
  153. * @param successHandler Executes if successful
  154. * @param failureHandler Executes if not successful
  155. */
  156. - (void)deleteContainer:(RSContainer *)container success:(void (^)())successHandler failure:(void (^)(NSHTTPURLResponse*, NSData*, NSError*))failureHandler;
  157. #pragma mark Get Container Metadata
  158. /** Returns a request object that represents a request to retrieve a container's metadata
  159. * @param container The container to get metadata
  160. */
  161. - (NSURLRequest *)getContainerMetadataRequest:(RSContainer *)container;
  162. /** Retrieves a container's metadata.
  163. * @param container The container to get metadata
  164. * @param successHandler Executes if successful
  165. * @param failureHandler Executes if not successful
  166. */
  167. - (void)getContainerMetadata:(RSContainer *)container success:(void (^)())successHandler failure:(void (^)(NSHTTPURLResponse*, NSData*, NSError*))failureHandler;
  168. #pragma mark - CDN Services
  169. #pragma mark CDN Enable Container
  170. /** Returns a request object that represents a request to CDN enable a container
  171. * @param container The container to CDN enable
  172. */
  173. - (NSURLRequest *)cdnEnableContainerRequest:(RSContainer *)container;
  174. /** CDN enables a container.
  175. * @param container The container to CDN enable
  176. * @param successHandler Executes if successful
  177. * @param failureHandler Executes if not successful
  178. */
  179. - (void)cdnEnableContainer:(RSContainer *)container success:(void (^)(RSCDNContainer *container))successHandler failure:(void (^)(NSHTTPURLResponse*, NSData*, NSError*))failureHandler;
  180. #pragma mark Get CDN Containers
  181. /** Returns a request object that represents a request to retrieve a list of CDN containers in your account */
  182. - (NSURLRequest *)getCDNContainersRequest;
  183. /** Returns a request object that represents a request to retrieve a list of CDN containers in your account
  184. * @param limit The maximum number of containers to return
  185. * @param marker Pass a value here to return object names greater than the marker you specify
  186. */
  187. - (NSURLRequest *)getCDNContainersRequestWithLimit:(NSUInteger)limit marker:(NSString *)marker;
  188. /** Retrieves a list of CDN containers in your account.
  189. * @param successHandler Executes if successful
  190. * @param failureHandler Executes if not successful
  191. */
  192. - (void)getCDNContainers:(void (^)(NSArray *containers, NSError *jsonError))successHandler failure:(void (^)(NSHTTPURLResponse*, NSData*, NSError*))failureHandler;
  193. #pragma mark Get CDN Container Metadata
  194. /** Returns a request object that represents a request to retrieve a CDN container's metadata
  195. * @param container The container to get metadata
  196. */
  197. - (NSURLRequest *)getCDNContainerMetadataRequest:(RSCDNContainer *)container;
  198. /** Retrieves a CDN container's metadata.
  199. * @param container The container to retrieve metadata
  200. * @param successHandler Executes if successful
  201. * @param failureHandler Executes if not successful
  202. */
  203. - (void)getCDNContainerMetadata:(RSCDNContainer *)container success:(void (^)())successHandler failure:(void (^)(NSHTTPURLResponse*, NSData*, NSError*))failureHandler;
  204. #pragma mark Update CDN Container
  205. /** Returns a request object that represents a request to update a container.
  206. * @param container The container to purge
  207. */
  208. - (NSURLRequest *)updateCDNContainerRequest:(RSCDNContainer *)container;
  209. /** Updates a CDN container. You can update TTL, Log Retention settings, and whether or not the container should still be CDN enabled.
  210. * @param container The container to update
  211. * @param successHandler Executes if successful
  212. * @param failureHandler Executes if not successful
  213. */
  214. - (void)updateCDNContainer:(RSCDNContainer *)container success:(void (^)())successHandler failure:(void (^)(NSHTTPURLResponse*, NSData*, NSError*))failureHandler;
  215. @end