forked from afh/trundle
/
CCouchDBServer.m
309 lines (265 loc) · 11.3 KB
/
CCouchDBServer.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//
// CCouchDBServer.m
// CouchTest
//
// Created by Jonathan Wight on 02/16/10.
// Copyright 2010 toxicsoftware.com. All rights reserved.
//
#import "CCouchDBServer.h"
#import "Asserts.h"
#import "CCouchDBSession.h"
#import "CCouchDBDatabase.h"
#import "CouchDBClientConstants.h"
#import "CCouchDBURLOperation.h"
#import "NSData_Base64Extensions.h"
#import "CFilteringJSONSerializer.h"
@interface CCouchDBServer ()
@property (readonly, retain) NSMutableDictionary *databasesByName;
@end
#pragma mark -
@implementation CCouchDBServer
@synthesize session;
@synthesize URL;
@synthesize URLCredential;
@synthesize databasesByName;
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
{
if ([key isEqualToString:@"databases"])
return([NSSet setWithObjects:@"databasesByName", NULL]);
else
{
return(NULL);
}
}
- (id)init
{
if ((self = [self initWithSession:NULL URL:[NSURL URLWithString:@"http://localhost:5984/"]]) != NULL)
{
}
return(self);
}
- (id)initWithSession:(CCouchDBSession *)inSession URL:(NSURL *)inURL;
{
if ((self = [super init]) != NULL)
{
session = [inSession retain];
URL = [inURL retain];
}
return(self);
}
- (void)dealloc
{
session = NULL;
[URL release];
URL = NULL;
//
[databasesByName release];
databasesByName = NULL;
//
[super dealloc];
}
#pragma mark -
- (NSString *)description
{
return([NSString stringWithFormat:@"%@ (%@)", [super description], self.URL]);
}
#pragma mark -
- (CCouchDBSession *)session
{
if (session == NULL)
{
session = [[CCouchDBSession alloc] init];
}
return(session);
}
- (NSSet *)databases
{
return([NSSet setWithArray:[self.databasesByName allValues]]);
}
- (NSMutableDictionary *)databasesByName
{
@synchronized(self)
{
if (databasesByName == NULL)
{
databasesByName = [[NSMutableDictionary alloc] init];
}
return(databasesByName);
}
}
- (CCouchDBDatabase *)databaseNamed:(NSString *)inName
{
CCouchDBDatabase *theDatabase = [self.databasesByName objectForKey:inName];
if (theDatabase == NULL)
{
theDatabase = [[[CCouchDBDatabase alloc] initWithServer:self name:inName] autorelease];
[self.databasesByName setObject:theDatabase forKey:inName];
}
return(theDatabase);
}
- (NSMutableURLRequest *)requestWithURL:(NSURL *)inURL;
{
NSMutableURLRequest *theRequest = [self.session requestWithURL:inURL];
if (self.URLCredential)
{
if ([inURL.scheme isEqualToString:@"http"] == YES)
{
NSLog(@"Warning: Using basic auth over non-https connections is a bad idea.");
}
NSString *theValue = [NSString stringWithFormat:@"%@:%@", self.URLCredential.user, self.URLCredential.password];
NSData *theData = [theValue dataUsingEncoding:NSUTF8StringEncoding];
theValue = [theData asBase64EncodedString:0];
theValue = [NSString stringWithFormat:@"Basic %@", theValue];
[theRequest setValue:theValue forHTTPHeaderField:@"Authorization"];
}
return(theRequest);
}
#pragma mark -
- (CURLOperation *)operationToCreateDatabaseNamed:(NSString *)inName withSuccessHandler:(CouchDBSuccessHandler)inSuccessHandler failureHandler:(CouchDBFailureHandler)inFailureHandler;
{
CCouchDBDatabase *theRemoteDatabase = [[[CCouchDBDatabase alloc] initWithServer:self name:inName] autorelease];
NSURL *theURL = [self.URL URLByAppendingPathComponent:theRemoteDatabase.encodedName];
NSMutableURLRequest *theRequest = [self requestWithURL:theURL];
theRequest.HTTPMethod = @"PUT";
[theRequest setValue:kContentTypeJSON forHTTPHeaderField:@"Accept"];
CCouchDBURLOperation *theOperation = [self.session URLOperationWithRequest:theRequest];
theOperation.successHandler = ^(id inParameter) {
[self willChangeValueForKey:@"databasesByName"];
[self.databasesByName setObject:theRemoteDatabase forKey:inName];
[self didChangeValueForKey:@"databasesByName"];
if (inSuccessHandler)
inSuccessHandler(theRemoteDatabase);
};
theOperation.failureHandler = inFailureHandler;
return(theOperation);
}
- (CURLOperation *)operationToFetchDatabasesWithSuccessHandler:(CouchDBSuccessHandler)inSuccessHandler failureHandler:(CouchDBFailureHandler)inFailureHandler
{
NSURL *theURL = [self.URL URLByAppendingPathComponent:@"_all_dbs"];
NSMutableURLRequest *theRequest = [self requestWithURL:theURL];
theRequest.HTTPMethod = @"GET";
[theRequest setValue:kContentTypeJSON forHTTPHeaderField:@"Accept"];
CCouchDBURLOperation *theOperation = [self.session URLOperationWithRequest:theRequest];
theOperation.successHandler = ^(id inParameter) {
[self willChangeValueForKey:@"databases"];
for (NSString *theName in inParameter)
{
if ([self.databasesByName objectForKey:theName] == NULL)
{
CCouchDBDatabase *theDatabase = [[[CCouchDBDatabase alloc] initWithServer:self name:theName] autorelease];
[self willChangeValueForKey:@"databasesByName"];
[self.databasesByName setObject:theDatabase forKey:theName];
[self didChangeValueForKey:@"databasesByName"];
}
}
[self didChangeValueForKey:@"databases"];
if (inSuccessHandler)
inSuccessHandler([self.databasesByName allValues]);
};
theOperation.failureHandler = inFailureHandler;
return(theOperation);
}
- (CURLOperation *)operationToFetchDatabaseNamed:(NSString *)inName withSuccessHandler:(CouchDBSuccessHandler)inSuccessHandler failureHandler:(CouchDBFailureHandler)inFailureHandler;
{
CCouchDBDatabase *theRemoteDatabase = [[[CCouchDBDatabase alloc] initWithServer:self name:inName] autorelease];
NSURL *theURL = [self.URL URLByAppendingPathComponent:theRemoteDatabase.encodedName];
NSMutableURLRequest *theRequest = [self requestWithURL:theURL];
theRequest.HTTPMethod = @"GET";
[theRequest setValue:kContentTypeJSON forHTTPHeaderField:@"Accept"];
CCouchDBURLOperation *theOperation = [self.session URLOperationWithRequest:theRequest];
theOperation.successHandler = ^(id inParameter) {
[self willChangeValueForKey:@"databasesByName"];
[self.databasesByName setObject:theRemoteDatabase forKey:inName];
[self didChangeValueForKey:@"databasesByName"];
if (inSuccessHandler)
inSuccessHandler(theRemoteDatabase);
};
theOperation.failureHandler = inFailureHandler;
return(theOperation);
}
- (CURLOperation *)operationToDeleteDatabase:(CCouchDBDatabase *)inDatabase withSuccessHandler:(CouchDBSuccessHandler)inSuccessHandler failureHandler:(CouchDBFailureHandler)inFailureHandler;
{
NSURL *theURL = [self.URL URLByAppendingPathComponent:inDatabase.encodedName];
NSMutableURLRequest *theRequest = [self requestWithURL:theURL];
theRequest.HTTPMethod = @"DELETE";
[theRequest setValue:kContentTypeJSON forHTTPHeaderField:@"Accept"];
CCouchDBURLOperation *theOperation = [self.session URLOperationWithRequest:theRequest];
theOperation.successHandler = ^(id inParameter) {
[self willChangeValueForKey:@"databasesByName"];
[self.databasesByName removeObjectForKey:inDatabase.name];
[self didChangeValueForKey:@"databasesByName"];
if (inSuccessHandler)
inSuccessHandler(inDatabase);
};
theOperation.failureHandler = inFailureHandler;
return(theOperation);
}
- (CURLOperation *)operationToFetchSessionWithSuccessHandler:(CouchDBSuccessHandler)inSuccessHandler failureHandler:(CouchDBFailureHandler)inFailureHandler
{
NSURL *theURL = [self.URL URLByAppendingPathComponent:@"_session"];
NSMutableURLRequest *theRequest = [self requestWithURL:theURL];
theRequest.HTTPMethod = @"GET";
[theRequest setValue:kContentTypeJSON forHTTPHeaderField:@"Accept"];
CCouchDBURLOperation *theOperation = [self.session URLOperationWithRequest:theRequest];
theOperation.successHandler = inSuccessHandler;
theOperation.failureHandler = inFailureHandler;
return(theOperation);
}
- (CURLOperation *)operationToFetchConfigurationWithSuccessHandler:(CouchDBSuccessHandler)inSuccessHandler failureHandler:(CouchDBFailureHandler)inFailureHandler
{
NSURL *theURL = [self.URL URLByAppendingPathComponent:@"_config"];
NSMutableURLRequest *theRequest = [self requestWithURL:theURL];
theRequest.HTTPMethod = @"GET";
[theRequest setValue:kContentTypeJSON forHTTPHeaderField:@"Accept"];
CCouchDBURLOperation *theOperation = [self.session URLOperationWithRequest:theRequest];
theOperation.successHandler = inSuccessHandler;
theOperation.failureHandler = inFailureHandler;
return(theOperation);
}
- (CURLOperation *)operationToUpdateConfigurationKey:(NSString *)inConfigurationKey inSection:(NSString*)inConfigurationSection withValue:(id)inConfigurationValue withSuccessHandler:(CouchDBSuccessHandler)inSuccessHandler failureHandler:(CouchDBFailureHandler)inFailureHandler
{
NSString *thePath = [NSString stringWithFormat:@"_config/%@/%@", inConfigurationSection, inConfigurationKey];
NSURL *theURL = [self.URL URLByAppendingPathComponent:thePath];
NSMutableURLRequest *theRequest = [self requestWithURL:theURL];
theRequest.HTTPMethod = @"PUT";
[theRequest setValue:kContentTypeJSON forHTTPHeaderField:@"Accept"];
NSData *theData = [self.session.serializer serializeObject:inConfigurationValue error:NULL];
[theRequest setValue:kContentTypeJSON forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPBody:theData];
CCouchDBURLOperation *theOperation = [self.session URLOperationWithRequest:theRequest];
theOperation.successHandler = inSuccessHandler;
theOperation.failureHandler = inFailureHandler;
return(theOperation);
}
- (CURLOperation *)operationToDeleteConfigurationKey:(NSString *)inConfigurationKey inSection:(NSString*)inConfigurationSection withSuccessHandler:(CouchDBSuccessHandler)inSuccessHandler failureHandler:(CouchDBFailureHandler)inFailureHandler
{
NSString *thePath = [NSString stringWithFormat:@"_config/%@/%@", inConfigurationSection, inConfigurationKey];
NSURL *theURL = [self.URL URLByAppendingPathComponent:thePath];
NSMutableURLRequest *theRequest = [self requestWithURL:theURL];
theRequest.HTTPMethod = @"DELETE";
[theRequest setValue:kContentTypeJSON forHTTPHeaderField:@"Accept"];
CCouchDBURLOperation *theOperation = [self.session URLOperationWithRequest:theRequest];
theOperation.successHandler = inSuccessHandler;
theOperation.failureHandler = inFailureHandler;
return(theOperation);
}
- (CURLOperation *)operationToTriggerReplicationFromSource:(NSString *)inSource toTarget:(NSString *)inTarget withOptions:(NSDictionary *)inOptions successHandler:(CouchDBSuccessHandler)inSuccessHandler failureHandler:(CouchDBFailureHandler)inFailureHandler
{
NSURL *theURL = [self.URL URLByAppendingPathComponent:@"_replicate"];
NSMutableDictionary *replicationSettings = [[[NSMutableDictionary alloc] initWithObjectsAndKeys:inSource, @"source", inTarget, @"target", nil] autorelease];
if (inOptions.count > 0)
{
[replicationSettings addEntriesFromDictionary:inOptions];
}
NSMutableURLRequest *theRequest = [self requestWithURL:theURL];
theRequest.HTTPMethod = @"POST";
[theRequest setValue:kContentTypeJSON forHTTPHeaderField:@"Accept"];
NSData *theData = [self.session.serializer serializeObject:replicationSettings error:NULL];
[theRequest setValue:kContentTypeJSON forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPBody:theData];
CCouchDBURLOperation *theOperation = [self.session URLOperationWithRequest:theRequest];
theOperation.successHandler = inSuccessHandler;
theOperation.failureHandler = inFailureHandler;
return(theOperation);
}
@end