forked from afh/trundle
/
CCouchDBSession.m
111 lines (93 loc) · 2.78 KB
/
CCouchDBSession.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
//
// CCouchDBSession.m
// TouchMetricsTest
//
// Created by Jonathan Wight on 08/21/10.
// Copyright 2010 toxicsoftware.com. All rights reserved.
//
#import "CCouchDBSession.h"
#import "CCouchDBURLOperation.h"
#import "CFilteringJSONSerializer.h"
#import "NSDate_InternetDateExtensions.h"
#import "CJSONDeserializer.h"
#import "CJSONSerializedData.h"
#import "NSData_Base64Extensions.h"
@implementation CCouchDBSession
@synthesize operationQueue;
@synthesize URLOperationClass;
@synthesize serializer;
@synthesize deserializer;
- (void)dealloc
{
[operationQueue cancelAllOperations];
[operationQueue waitUntilAllOperationsAreFinished];
[operationQueue release];
operationQueue = NULL;
//
[serializer release];
serializer = NULL;
//
[deserializer release];
deserializer = NULL;
//
[super dealloc];
}
#pragma mark -
- (NSOperationQueue *)operationQueue
{
if (operationQueue == NULL)
{
operationQueue = [[NSOperationQueue alloc] init];
}
return(operationQueue);
}
- (Class)URLOperationClass
{
if (URLOperationClass == NULL)
{
return([CCouchDBURLOperation class]);
}
return(URLOperationClass);
}
- (CJSONSerializer *)serializer
{
if (serializer == NULL)
{
CFilteringJSONSerializer *theSerializer = [CFilteringJSONSerializer serializer];
theSerializer.convertersByName = [NSDictionary dictionaryWithObjectsAndKeys:
[[^(NSDate *inDate) { return((id)[inDate ISO8601String]); } copy] autorelease], @"date",
[[^(CJSONSerializedData *inObject) { return((id)inObject.data); } copy] autorelease], @"JSONSerializedData",
NULL];
theSerializer.tests = [NSSet setWithObjects:
[[^(id inObject) { return([inObject isKindOfClass:[NSDate class]] ? @"date" : NULL); } copy] autorelease],
[[^(id inObject) { return([inObject isKindOfClass:[CJSONSerializedData class]] ? @"JSONSerializedData" : NULL); } copy] autorelease],
NULL];
serializer = [theSerializer retain];
}
return(serializer);
}
- (CJSONDeserializer *)deserializer
{
if (deserializer == NULL)
{
CJSONDeserializer *theDeserializer = [CJSONDeserializer deserializer];
deserializer = [theDeserializer retain];
}
return(deserializer);
}
#pragma mark -
- (NSMutableURLRequest *)requestWithURL:(NSURL *)inURL
{
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:inURL];
// NSString *theValue = @"username:password";
// NSData *theData = [theValue dataUsingEncoding:NSUTF8StringEncoding];
// theValue = [theData asBase64EncodedString:0];
// theValue = [NSString stringWithFormat:@"Basic %@", theValue];
// [theRequest setValue:theValue forHTTPHeaderField:@"Authorization"];
return(theRequest);
}
- (id)URLOperationWithRequest:(NSURLRequest *)inURLRequest;
{
return([[[[self URLOperationClass] alloc] initWithSession:self request:inURLRequest] autorelease]);
}
@end