PageRenderTime 24ms CodeModel.GetById 9ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Mobile/iPhone/StoffiRemote/StoffiRemote/RESTClient.m

http://yet-another-music-application.googlecode.com/
Objective C | 63 lines | 39 code | 15 blank | 9 comment | 2 complexity | 20eb6b5e2a1de445f5f1933b93089a6b MD5 | raw file
 1//
 2//  RESTClient.m
 3//  StoffiRemote
 4//
 5//  Created by Fredrik Gadnell on 9/18/11.
 6//  Copyright 2011 __MyCompanyName__. All rights reserved.
 7//
 8
 9#import "RESTClient.h"
10#import "RESTClientSimulator.h"
11
12
13@implementation RESTClient
14
15@synthesize baseURL, shouldSimulateResponse;
16
17- (id)init {
18    self = [super init];
19    
20    if (self) {
21        baseURL = @"";
22        shouldSimulateResponse = NO;
23    }
24    return self;
25}
26
27+ (RESTClient *)sharedClient
28{
29    static dispatch_once_t once;
30    static RESTClient *sharedClient;
31    dispatch_once(&once, ^ {sharedClient = [[RESTClient alloc] init];});
32    return sharedClient;
33}
34
35- (RESTRequest *)get:(NSString *)path delegate:(id<RestRequestDelegate>)delegate {
36    
37    return [self requestWithPath:path httpMethod:@"GET" delegate:delegate];
38}
39
40- (RESTRequest *)post:(NSString *)path delegate:(id<RestRequestDelegate>)delegate {
41    return [self requestWithPath:path httpMethod:@"POST" delegate:delegate];
42}
43
44- (RESTRequest *)put:(NSString *)path delegate:(id<RestRequestDelegate>)delegate {
45    return [self requestWithPath:path httpMethod:@"PUT" delegate:delegate];
46}
47
48- (RESTRequest *)del:(NSString *)path delegate:(id<RestRequestDelegate>)delegate {
49    return [self requestWithPath:path httpMethod:@"DELETE" delegate:delegate];
50}
51
52- (RESTRequest *)requestWithPath:(NSString *)path httpMethod:(NSString *)method delegate:(id<RestRequestDelegate>)delegate {
53    // Simulated request
54    if (shouldSimulateResponse) {
55        return [[RESTClientSimulator sharedClientSimulator] simulatedRequestWithPath:path httpMethod:method delegate:delegate];
56    }
57    
58    // Normal request
59    NSString *url = [baseURL stringByAppendingString:path];
60    return [RESTRequest restRequestWithURL:url method:method delegate:delegate];
61}
62
63@end