This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
BGLastFmWebServiceCaller.m
69 lines (57 loc) · 2.61 KB
/
BGLastFmWebServiceCaller.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
//
// BGLastFmWebServiceCaller.m
// ApiHubTester
//
// Created by Ben Gummer on 18/07/08.
// Copyright 2008 Ben Gummer. All rights reserved.
//
#import "BGLastFmWebServiceCaller.h"
#import "HubStrings.h"
@implementation BGLastFmWebServiceCaller
-(BGLastFmWebServiceResponse *)callWithParameters:(BGLastFmWebServiceParameterList *)parameterList usingPostMethod:(BOOL)postBool usingAuthentication:(BOOL)needAuthentication {
BGLastFmWebServiceResponse *responseObject;
BOOL callWasSent = NO;
if (!postBool || (postBool && [parameterList parameterForKey:@"sk"]!=nil && [parameterList parameterForKey:@"sk"].length>0)) {
NSString *postString = [NSString stringWithFormat:@"%@%@",
[parameterList concatenatedParametersString],
(needAuthentication ? [NSString stringWithFormat:@"&api_sig=%@", [parameterList methodSignature]] : @"" )
];
NSURL *postURL;
if (postBool) {
postURL = [NSURL URLWithString:@"http://ws.audioscrobbler.com/2.0/"];
} else {
postURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://ws.audioscrobbler.com/2.0/?%@",postString]];
}
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:postURL];
[request setHTTPMethod:(postBool ? @"POST" : @"GET")];
if (postBool) {
NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
}
[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
[request setTimeoutInterval:10.00];// timeout scrobble posting after 20 seconds
NSError *postingError;
NSHTTPURLResponse *response = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&postingError];
[request release];
int responseStatusCode = [response statusCode];
if (responseData!=nil && [postingError code]!=-1001 && (responseStatusCode==200 || responseStatusCode==403) ) {
responseObject = [[BGLastFmWebServiceResponse alloc] initWithData:responseData];
callWasSent = YES;
} else {
NSLog(@"Got HTTP Status Code %d and did not continue.",responseStatusCode);
}
} else {
NSLog(@"Could not complete API POST request: no session key provided");
}
if (callWasSent == NO) {
NSLog(@"-- Completed API call but nothing returned");
responseObject = [[BGLastFmWebServiceResponse alloc] initWithData:nil];
}
return [responseObject autorelease];
}
@end