PageRenderTime 92ms CodeModel.GetById 16ms app.highlight 73ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/gdata-objectivec-client/Source/Clients/YouTube/GDataServiceGoogleYouTube.m

http://macfuse.googlecode.com/
Objective C | 256 lines | 144 code | 63 blank | 49 comment | 9 complexity | 62c65c2a24b15a2a74718a9e2475c997 MD5 | raw file
  1/* Copyright (c) 2008 Google Inc.
  2 *
  3 * Licensed under the Apache License, Version 2.0 (the "License");
  4 * you may not use this file except in compliance with the License.
  5 * You may obtain a copy of the License at
  6 *
  7 *     http://www.apache.org/licenses/LICENSE-2.0
  8 *
  9 * Unless required by applicable law or agreed to in writing, software
 10 * distributed under the License is distributed on an "AS IS" BASIS,
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License.
 14 */
 15
 16//
 17//  GDataServiceGoogleYouTube.m
 18//
 19
 20#if !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_YOUTUBE_SERVICE
 21
 22#define GDATASERVICEYOUTUBE_DEFINE_GLOBALS 1
 23#import "GDataServiceGoogleYouTube.h"
 24
 25#import "GDataYouTubeConstants.h"
 26#import "GDataQueryYouTube.h"
 27#import "GDataEntryYouTubeVideo.h"
 28#import "GDataEntryYouTubeUpload.h"
 29
 30@interface GDataServiceGoogle (PrivateMethods)
 31// The YouTube service will be overriding this private superclass method
 32- (GDataServiceTicket *)fetchAuthenticatedObjectWithURL:(NSURL *)objectURL
 33                                            objectClass:(Class)objectClass
 34                                           objectToPost:(GDataObject *)objectToPost
 35                                                   ETag:(NSString *)etag
 36                                             httpMethod:(NSString *)httpMethod
 37                                               delegate:(id)delegate
 38                                      didFinishSelector:(SEL)finishedSelector
 39                                      completionHandler:(GDataServiceGoogleCompletionHandler)completionHandler;
 40@end
 41
 42@implementation GDataServiceGoogleYouTube
 43
 44- (void)dealloc {
 45  [developerKey_ release];
 46  [super dealloc];
 47}
 48
 49+ (NSURL *)youTubeURLForFeedID:(NSString *)feedID {
 50
 51  // like
 52  //
 53  //   http://gdata.youtube.com/feeds/api/videos
 54  //
 55  // or
 56  //
 57  //   http://gdata.youtube.com/feeds/api/standardfeeds/feedid
 58  //
 59  // See http://code.google.com/apis/youtube/2.0/reference.html#Standard_feeds for feed IDs
 60
 61  NSString *endPart;
 62
 63  if (feedID == nil) {
 64    endPart = @"videos";
 65  } else {
 66    endPart = [NSString stringWithFormat:@"standardfeeds/%@", feedID];
 67  }
 68
 69  NSString *root = [self serviceRootURLString];
 70
 71  NSString *const templateStr = @"%@api/%@";
 72
 73  NSString *urlString = [NSString stringWithFormat:templateStr, root, endPart];
 74
 75  return [NSURL URLWithString:urlString];
 76}
 77
 78+ (NSURL *)youTubeURLForChannelsFeeds {
 79  // feed counterpart to http://www.youtube.com/channels
 80  //
 81  // this is most useful as part of a query searching for text
 82  NSString *root = [self serviceRootURLString];
 83
 84  NSString *const templateStr = @"%@api/channels";
 85
 86  NSString *urlString = [NSString stringWithFormat:templateStr, root];
 87
 88  return [NSURL URLWithString:urlString];
 89}
 90
 91+ (NSURL *)youTubeURLForChannelStandardFeedID:(NSString *)feedID {
 92  NSString *root = [self serviceRootURLString];
 93
 94  NSString *const templateStr = @"%@api/channelstandardfeeds/%@";
 95
 96  NSString *urlString = [NSString stringWithFormat:templateStr, root, feedID];
 97
 98  return [NSURL URLWithString:urlString];
 99}
100
101+ (NSURL *)youTubeURLForUserID:(NSString *)userID
102                    userFeedID:(NSString *)feedID {
103  // Make a URL like
104  //   http://gdata.youtube.com/feeds/api/users/username/favorites
105  //
106  // userID may be kGDataServiceDefaultUser
107
108  NSString *encodedUserID = [GDataUtilities stringByURLEncodingForURI:userID];
109  NSString *endPart;
110
111  if (feedID == nil) {
112    endPart = @"";
113  } else {
114    endPart = [NSString stringWithFormat:@"/%@", feedID];
115  }
116
117  NSString *root = [self serviceRootURLString];
118
119  NSString *const templateStr = @"%@api/users/%@%@";
120
121  NSString *urlString = [NSString stringWithFormat:templateStr, root,
122    encodedUserID, endPart];
123
124  return [NSURL URLWithString:urlString];
125}
126
127+ (NSURL *)youTubeActivityFeedURLForUserID:(NSString *)userID {
128
129  // Make a URL like
130  //   http://gdata.youtube.com/feeds/api/events?author=usernames
131  // (usernames can be a comma-separated list)
132  //
133  // For a friends activity feed, call youTubeURLForUserID:userFeedID:
134  // with kGDataServiceDefaultUser and kGDataYouTubeUserFeedIDFriendsActivity
135
136  NSString *encodedUserID = [GDataUtilities stringByURLEncodingStringParameter:userID];
137
138  NSString *root = [self serviceRootURLString];
139
140  NSString *const templateStr = @"%@api/events?author=%@";
141
142  NSString *urlString = [NSString stringWithFormat:templateStr,
143                         root, encodedUserID];
144
145  return [NSURL URLWithString:urlString];
146}
147
148+ (NSURL *)youTubeUploadURLForUserID:(NSString *)userID {
149  // Make a URL like
150  //   https://uploads.gdata.youtube.com/feeds/api/users/username/uploads
151  //
152  // userID may be "default" to indicate the currently authenticated user
153
154  NSString *encodedUserID = [GDataUtilities stringByURLEncodingForURI:userID];
155
156  NSString *root = [self serviceUploadRootURLString];
157
158  NSString *const templateStr = @"%@api/users/%@/uploads";
159
160  NSString *urlString = [NSString stringWithFormat:templateStr,
161                         root, encodedUserID];
162
163  return [NSURL URLWithString:urlString];
164}
165
166- (NSString *)youTubeDeveloperKey {
167  return developerKey_;
168}
169
170- (void)setYouTubeDeveloperKey:(NSString *)str {
171  [developerKey_ autorelease];
172  developerKey_ = [str copy];
173}
174
175#pragma mark -
176
177// overrides of the superclass
178
179- (NSMutableURLRequest *)requestForURL:(NSURL *)url
180                                  ETag:(NSString *)etag
181                            httpMethod:(NSString *)httpMethod
182                                ticket:(GDataServiceTicketBase *)ticket {
183
184  // if the request is for posting, add the developer key, if it's known
185  NSMutableURLRequest *request = [super requestForURL:url
186                                                 ETag:etag
187                                           httpMethod:httpMethod
188                                               ticket:ticket];
189
190  // set the developer key, if any
191  NSString *developerKey = [self youTubeDeveloperKey];
192  if ([developerKey length] > 0) {
193
194    NSString *value = [NSString stringWithFormat:@"key=%@", developerKey];
195    [request setValue:value forHTTPHeaderField:@"X-GData-Key"];
196  }
197
198  return request;
199}
200
201// when authenticating, add the Content-Type header required by YouTube
202- (NSDictionary *)customAuthenticationRequestHeaders {
203  return [NSDictionary dictionaryWithObject:@"application/x-www-form-urlencoded"
204                                     forKey:@"Content-Type"];
205}
206
207- (GDataServiceTicket *)fetchAuthenticatedObjectWithURL:(NSURL *)objectURL
208                                            objectClass:(Class)objectClass
209                                           objectToPost:(GDataObject *)objectToPost
210                                                   ETag:(NSString *)etag
211                                             httpMethod:(NSString *)httpMethod
212                                               delegate:(id)delegate
213                                      didFinishSelector:(SEL)finishedSelector
214                                      completionHandler:(GDataServiceGoogleCompletionHandler)completionHandler {
215  if ([objectClass isSubclassOfClass:[GDataEntryYouTubeUpload class]]) {
216    // when uploading, expect a full video entry back
217    objectClass = [GDataEntryYouTubeVideo class];
218  }
219
220  return [super fetchAuthenticatedObjectWithURL:objectURL
221                                    objectClass:objectClass
222                                   objectToPost:objectToPost
223                                           ETag:etag
224                                     httpMethod:httpMethod
225                                       delegate:delegate
226                              didFinishSelector:finishedSelector
227                              completionHandler:completionHandler];
228}
229
230+ (NSString *)serviceID {
231  return @"youtube";
232}
233
234+ (NSString *)serviceRootURLString {
235  return @"https://gdata.youtube.com/feeds/";
236}
237
238+ (NSString *)serviceUploadRootURLString {
239  return @"https://uploads.gdata.youtube.com/resumable/feeds/";
240}
241
242+ (NSString *)defaultServiceVersion {
243  return kGDataYouTubeDefaultServiceVersion;
244}
245
246+ (NSUInteger)defaultServiceUploadChunkSize {
247  return kGDataStandardUploadChunkSize;
248}
249
250+ (NSDictionary *)standardServiceNamespaces {
251  return [GDataYouTubeConstants youTubeNamespaces];
252}
253
254@end
255
256#endif // !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_YOUTUBE_SERVICE