This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
YQLQueryRequest.m
113 lines (79 loc) · 2.98 KB
/
YQLQueryRequest.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
//
// YOSQueryRequest.m
// YOSSocial
//
// Created by Zach Graves on 2/11/09.
// Copyright (c) 2009 Yahoo! Inc. All rights reserved.
//
// The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license.
//
#import "YQLQueryRequest.h"
static NSString *const kYQLBaseUrl = @"http://query.yahooapis.com";
static NSString *const kYQLOpenTables = @"http://datatables.org/alltables.env";
@implementation YQLQueryRequest
@synthesize environmentFile;
@synthesize diagnostics;
#pragma mark init
+ (id)requestWithSession:(YOSSession *)session
{
YOSUser *sessionedUser = [[YOSUser alloc] initWithSession:session];
YQLQueryRequest *request = [[YQLQueryRequest alloc] initWithYOSUser:sessionedUser];
[sessionedUser autorelease];
[request autorelease];
return request;
}
#pragma mark -
#pragma mark Public
- (void)query:(NSString *)aQuery withDelegate:(id)delegate
{
YOSRequestClient *client = [self generateRequest:aQuery];
[client setHTTPMethod:@"GET"];
[client setOauthParamsLocation:@"OAUTH_PARAMS_IN_QUERY_STRING"];
[client sendAsyncRequestWithDelegate:delegate];
}
- (YOSResponseData *)query:(NSString *)aQuery
{
YOSRequestClient *client = [self generateRequest:aQuery];
[client setHTTPMethod:@"GET"];
[client setOauthParamsLocation:@"OAUTH_PARAMS_IN_QUERY_STRING"];
YOSResponseData *response = [client sendSynchronousRequest];
if(!response.didSucceed) {
return nil;
}
return response;
}
- (BOOL)updateQuery:(NSString *)aQuery
{
YOSRequestClient *client = [self generateRequest:aQuery];
[client setHTTPMethod:@"PUT"];
[client setOauthParamsLocation:@"OAUTH_PARAMS_IN_QUERY_STRING"];
YOSResponseData *response = [client sendSynchronousRequest];
if (!response.didSucceed) {
return NO;
}
return YES;
}
#pragma mark -
#pragma mark private
- (YOSRequestClient *)generateRequest:(NSString *)aQuery
{
if(!self.environmentFile) [self setEnvironmentFile:kYQLOpenTables];
// If a consumer is not available, we can assume public tables are being used
// and we'll use the public yql endpoint.
NSString *requestUrl = ([self consumerForRequest] != nil)
? [NSString stringWithFormat:@"%@/%@/%@",kYQLBaseUrl,self.apiVersion,@"yql"]
: [NSString stringWithFormat:@"%@/%@/%@/%@",kYQLBaseUrl,self.apiVersion,@"public",@"yql"];
NSURL *url = [NSURL URLWithString:requestUrl];
NSMutableDictionary *requestParameters = [NSMutableDictionary dictionary];
[requestParameters setObject:aQuery forKey:@"q"];
[requestParameters setObject:self.format forKey:@"format"];
NSString *useDiagnostics = (self.diagnostics) ? @"true" : @"false";
[requestParameters setObject:useDiagnostics forKey:@"diagnostics"];
[requestParameters setObject:self.environmentFile forKey:@"env"];
YOSRequestClient *client = [[YOSRequestClient alloc] initWithConsumer:[self consumerForRequest]
andToken:[self tokenForRequest]];
[client setRequestUrl:url];
[client setRequestParameters:requestParameters];
return [client autorelease];
}
@end