forked from jdg/oauthconsumer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OACall.m
170 lines (146 loc) · 4.11 KB
/
OACall.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// OACall.m
// OAuthConsumer
//
// Created by Alberto García Hierro on 04/09/08.
// Copyright 2008 Alberto García Hierro. All rights reserved.
// bynotes.com
#import "OAConsumer.h"
#import "OAToken.h"
#import "OAProblem.h"
#import "OADataFetcher.h"
#import "OAServiceTicket.h"
#import "OAMutableURLRequest.h"
#import "OACall.h"
@interface OACall (Private)
- (void)callFinished:(OAServiceTicket *)ticket withData:(NSData *)data;
- (void)callFailed:(OAServiceTicket *)ticket withError:(NSError *)error;
@end
@implementation OACall
@synthesize url, method, parameters, files, ticket;
- (id)init {
return [self initWithURL:nil
method:nil
parameters:nil
files:nil];
}
- (id)initWithURL:(NSURL *)aURL {
return [self initWithURL:aURL
method:nil
parameters:nil
files:nil];
}
- (id)initWithURL:(NSURL *)aURL method:(NSString *)aMethod {
return [self initWithURL:aURL
method:aMethod
parameters:nil
files:nil];
}
- (id)initWithURL:(NSURL *)aURL parameters:(NSArray *)theParameters {
return [self initWithURL:aURL
method:nil
parameters:theParameters];
}
- (id)initWithURL:(NSURL *)aURL method:(NSString *)aMethod parameters:(NSArray *)theParameters {
return [self initWithURL:aURL
method:aMethod
parameters:theParameters
files:nil];
}
- (id)initWithURL:(NSURL *)aURL parameters:(NSArray *)theParameters files:(NSDictionary*)theFiles {
return [self initWithURL:aURL
method:@"POST"
parameters:theParameters
files:theFiles];
}
- (id)initWithURL:(NSURL *)aURL
method:(NSString *)aMethod
parameters:(NSArray *)theParameters
files:(NSDictionary*)theFiles {
if ((self = [super init])) {
url = [aURL retain];
method = [aMethod retain];
parameters = [theParameters retain];
files = [theFiles retain];
fetcher = nil;
request = nil;
}
return self;
}
- (void)dealloc {
[url release];
[method release];
[parameters release];
[files release];
[fetcher release];
[request release];
[ticket release];
[super dealloc];
}
- (void)callFailed:(OAServiceTicket *)aTicket withError:(NSError *)error {
NSLog(@"error body: %@", aTicket.body);
self.ticket = aTicket;
[aTicket release];
OAProblem *problem = [OAProblem problemWithResponseBody:ticket.body];
if (problem) {
[delegate call:self failedWithProblem:problem];
} else {
[delegate call:self failedWithError:error];
}
}
- (void)callFinished:(OAServiceTicket *)aTicket withData:(NSData *)data {
self.ticket = aTicket;
[aTicket release];
if (ticket.didSucceed) {
// NSLog(@"Call body: %@", ticket.body);
[delegate performSelector:finishedSelector withObject:self withObject:ticket.body];
} else {
// NSLog(@"Failed call body: %@", ticket.body);
[self callFailed:[ticket retain] withError:nil];
}
}
- (void)perform:(OAConsumer *)consumer
token:(OAToken *)token
realm:(NSString *)realm
delegate:(NSObject <OACallDelegate> *)aDelegate
didFinish:(SEL)finished
{
delegate = aDelegate;
finishedSelector = finished;
request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:consumer
token:token
realm:realm
signatureProvider:nil];
if(method) {
[request setHTTPMethod:method];
}
if (self.parameters) {
[request setParameters:self.parameters];
}
// if (self.files) {
// for (NSString *key in self.files) {
// [request attachFileWithName:@"file" filename:NSLocalizedString(@"Photo.jpg", @"") data:[self.files objectForKey:key]];
// }
// }
fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:@selector(callFinished:withData:)
didFailSelector:@selector(callFailed:withError:)];
}
/*- (BOOL)isEqual:(id)object {
if ([object isKindOfClass:[self class]]) {
return [self isEqualToCall:(OACall *)object];
}
return NO;
}
- (BOOL)isEqualToCall:(OACall *)aCall {
return (delegate == aCall->delegate
&& finishedSelector == aCall->finishedSelector
&& [url isEqualTo:aCall.url]
&& [method isEqualToString:aCall.method]
&& [parameters isEqualToArray:aCall.parameters]
&& [files isEqualToDictionary:aCall.files]);
}*/
@end