Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 84 lines (74 sloc) 3.109 kB
76d2a5b @mstroeck
mstroeck authored
1 //
2 // BitlyAPIHelper.h
3 // Vienna
4 //
5 // Created by Michael Stroeck on 15.01.10.
6 // Copyright 2010 Michael Stroeck. All rights reserved.
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20
21 #import "BitlyAPIHelper.h"
22 #import "XMLParser.h"
23 #import "HelperFunctions.h"
24
25 // bit.ly defaults to delivering results via JSON, but we want to use XML here, that's why whe use &format=xml
26 static NSString * BitlyApiBaseUrl = @"http://api.bit.ly/%@?version=2.0.1&login=%@&apiKey=%@&format=xml&";
27
28 @implementation BitlyAPIHelper
29
30 /* initWithLoginName:
31 * Designated initialiser. We always use Vienna's API key and login at the moment, but I'm leaving it
32 * like this to be flexible. We may want to provide a way to for users to use their own bit.ly login.
33 */
34 -(BitlyAPIHelper*)initWithLogin: (NSString*) bitlyLogin andAPIKey: (NSString*) bitlyApiKey
35 {
36 login = [bitlyLogin copy];
37 apiKey = [bitlyApiKey copy];
38 return self;
39 }
40
41 /* shortenURL:
42 * Uses the bit.ly API to shorten long URLs, especially for use in sharing-plugins.
43 */
44 - (NSString*)shortenURL: (NSString*)longURL
45 {
46 // The next few lines incrementally build the request...
47 NSString * requestURLString = [NSString stringWithFormat:BitlyApiBaseUrl, @"shorten", login, apiKey];
48 longURL = [cleanedUpAndEscapedUrlFromString(longURL) absoluteString];
49 NSString * parameters = [NSString stringWithFormat:@"longUrl=%@", longURL];
50 requestURLString = [requestURLString stringByAppendingString:parameters];
51 NSURL *finishedRequestURL = [NSURL URLWithString:requestURLString];
52 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:finishedRequestURL];
53
54 // ... which is then finally sent to bit.ly's servers.
55 NSHTTPURLResponse* urlResponse = nil;
56 NSError *error = [[[NSError alloc] init] autorelease];
57 NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
58
59 // If the response is OK, use Vienna's XML parser stuff to get the data we need.
60 if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300)
61 {
62 //TODO: Robust error-handling.
63 XMLParser * responseParser = [[XMLParser alloc] init];
64 [responseParser setData:data];
65
66 XMLParser * subtree;
67 if ((subtree = [responseParser treeByPath:@"bitly/results/nodeKeyVal/shortUrl"]) != nil)
68 {
69 [responseParser release];
25894bf @mstroeck Fix some issues brought up by the clang static analyzer.
mstroeck authored
70 [request release];
76d2a5b @mstroeck
mstroeck authored
71 return [subtree valueOfElement];
72 }
25894bf @mstroeck Fix some issues brought up by the clang static analyzer.
mstroeck authored
73 [request release];
76d2a5b @mstroeck
mstroeck authored
74 [responseParser release];
75 }
25894bf @mstroeck Fix some issues brought up by the clang static analyzer.
mstroeck authored
76 [request release];
76d2a5b @mstroeck
mstroeck authored
77 // TODO: Present error-message to the user?
78 NSLog(@"URL shortening with bit.ly failed!");
79 NSBeep();
80 return nil;
81 }
82
83 @end
Something went wrong with that request. Please try again.