/// <summary> /// For a given request token and verifier string return an access token. /// </summary> /// <param name="requestToken"></param> /// <param name="requestTokenSecret"></param> /// <param name="verifier"></param> /// <param name="callback"></param> public async Task <FlickrResult <OAuthAccessToken> > OAuthGetAccessTokenAsync(string requestToken, string requestTokenSecret, string verifier) { string url = "https://api.twitter.com/oauth/access_token"; Dictionary <string, string> parameters = OAuthGetBasicParameters(); parameters.Add("oauth_verifier", verifier); parameters.Add("oauth_token", requestToken); string sig = OAuthCalculateSignature("POST", url, parameters, requestTokenSecret); parameters.Add("oauth_signature", sig); FlickrResult <string> r = await TwitterResponder.GetDataResponseAsync(this, "POST", url, parameters); FlickrResult <OAuthAccessToken> result = new FlickrResult <OAuthAccessToken>(); if (!r.HasError) { result.Result = FlickrNet.OAuthAccessToken.ParseResponse(r.Result); } else { result.HasError = r.HasError; result.Error = r.Error; result.ErrorCode = r.ErrorCode; result.ErrorMessage = r.ErrorMessage; } return(result); }
/// <summary> /// Returns the location data for a give photo. /// </summary> /// <param name="photoId">The ID of the photo to return the location information for.</param> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public async Task <FlickrResult <PlaceInfo> > PhotosGeoGetLocationAsync(string photoId) { Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.photos.geo.getLocation"); parameters.Add("photo_id", photoId); var r = await GetResponseAsync <PhotoInfo>( parameters); FlickrResult <PlaceInfo> result = new FlickrResult <PlaceInfo>(); result.HasError = r.HasError; if (result.HasError) { if (result.ErrorCode == 2) { result.HasError = false; result.Result = null; result.Error = null; } else { result.Error = r.Error; } } else { result.Result = r.Result.Location; } return(result); }
public void AuthGetFrobAsync(Action <FlickrResult <string> > callback) { CheckSigned(); var parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.auth.getFrob"); GetResponseAsync <UnknownResponse>( parameters, r => { var result = new FlickrResult <string>(); result.HasError = r.HasError; if (r.HasError) { result.Error = r.Error; } else { result.Result = r.Result.GetElementValue("frob"); } callback(result); }); }
/// <summary> /// Returns a place based on the input latitude and longitude. /// </summary> /// <param name="latitude">The latitude, between -180 and 180.</param> /// <param name="longitude">The longitude, between -90 and 90.</param> /// <param name="accuracy">The level the locality will be for.</param> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public void PlacesFindByLatLonAsync(double latitude, double longitude, GeoAccuracy accuracy, Action <FlickrResult <Place> > callback) { Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.places.findByLatLon"); parameters.Add("lat", latitude.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)); parameters.Add("lon", longitude.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)); if (accuracy != GeoAccuracy.None) { parameters.Add("accuracy", ((int)accuracy).ToString(System.Globalization.NumberFormatInfo.InvariantInfo)); } GetResponseAsync <PlaceCollection>( parameters, r => { FlickrResult <Place> result = new FlickrResult <Place>(); result.Error = r.Error; if (!r.HasError) { result.Result = r.Result[0]; } callback(result); }); }
/// <summary> /// Add a note to a picture. /// </summary> /// <param name="photoId">The photo id to add the note to.</param> /// <param name="noteX">The X co-ordinate of the upper left corner of the note.</param> /// <param name="noteY">The Y co-ordinate of the upper left corner of the note.</param> /// <param name="noteWidth">The width of the note.</param> /// <param name="noteHeight">The height of the note.</param> /// <param name="noteText">The text in the note.</param> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public async Task <FlickrResult <string> > PhotosNotesAddAsync(string photoId, int noteX, int noteY, int noteWidth, int noteHeight, string noteText) { Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.photos.notes.add"); parameters.Add("photo_id", photoId); parameters.Add("note_x", noteX.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)); parameters.Add("note_y", noteY.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)); parameters.Add("note_w", noteWidth.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)); parameters.Add("note_h", noteHeight.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)); parameters.Add("note_text", noteText); var r = await GetResponseAsync <UnknownResponse>(parameters); FlickrResult <string> result = new FlickrResult <string>(); result.HasError = r.HasError; if (r.HasError) { result.Error = r.Error; } else { result.Result = r.Result.GetAttributeValue("*", "id"); } return(result); }
/// <summary> /// Returns the location data for a give photo. /// </summary> /// <param name="photoId">The ID of the photo to return the location information for.</param> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public void PhotosGeoGetLocationAsync(string photoId, Action <FlickrResult <PlaceInfo> > callback) { var parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.photos.geo.getLocation"); parameters.Add("photo_id", photoId); GetResponseAsync <PhotoInfo>( parameters, r => { var result = new FlickrResult <PlaceInfo>(); result.HasError = r.HasError; if (result.HasError) { if (result.ErrorCode == 2) { result.HasError = false; result.Result = null; result.Error = null; } else { result.Error = r.Error; } } else { result.Result = r.Result.Location; } callback(result); }); }
/// <summary> /// Get an <see cref="OAuthRequestToken"/> for the given callback URL. /// </summary> /// <remarks>Specify 'oob' as the callback url for no callback to be performed.</remarks> /// <param name="callbackUrl">The callback Uri, or 'oob' if no callback is to be performed.</param> /// <param name="callback"></param> async public Task <bool> OAuthGetRequestTokenAsync(string callbackUrl, Action <FlickrResult <OAuthRequestToken> > callback) { string url = "http://www.flickr.com/services/oauth/request_token"; Dictionary <string, string> parameters = OAuthGetBasicParameters(); parameters.Add("oauth_callback", callbackUrl); string sig = OAuthCalculateSignature("POST", url, parameters, null); parameters.Add("oauth_signature", sig); FlickrResponder.GetDataResponseAsync(this, url, parameters, (r) => { FlickrResult <OAuthRequestToken> result = new FlickrResult <OAuthRequestToken>(); if (r.Error != null) { if (r.Error is System.Net.WebException) { OAuthException ex = new OAuthException(r.Error); result.Error = ex; } else { result.Error = r.Error; } callback(result); return; } result.Result = FlickrNet.OAuthRequestToken.ParseResponse(r.Result); callback(result); }); return(true); }
/// <summary> /// For a given request token and verifier string return an access token. /// </summary> /// <param name="requestToken"></param> /// <param name="requestTokenSecret"></param> /// <param name="verifier"></param> /// <param name="callback"></param> public async Task <FlickrResult <OAuthAccessToken> > OAuthGetAccessTokenAsync(string requestToken, string requestTokenSecret, string verifier) { string url = "https://www.flickr.com/services/oauth/access_token"; Dictionary <string, string> parameters = OAuthGetBasicParameters(); parameters.Add("oauth_verifier", verifier); parameters.Add("oauth_token", requestToken); string sig = OAuthCalculateSignature("POST", url, parameters, requestTokenSecret); parameters.Add("oauth_signature", sig); var r = await FlickrResponder.GetDataResponseAsync(this, "", url, parameters); FlickrResult <OAuthAccessToken> result = new FlickrResult <OAuthAccessToken>(); if (r.Error != null) { if (r.Error is System.Net.WebException) { OAuthException ex = new OAuthException(r.Error); result.Error = ex; } else { result.Error = r.Error; } return(result); } result.Result = FlickrNet.OAuthAccessToken.ParseResponse(r.Result); return(result); }
/// <summary> /// Adds a new comment to a photoset. /// </summary> /// <param name="photosetId">The ID of the photoset to add the comment to.</param> /// <param name="commentText">The text of the comment. Can contain some HTML.</param> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public void PhotosetsCommentsAddCommentAsync(string photosetId, string commentText, Action <FlickrResult <string> > callback) { Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.photosets.comments.addComment"); parameters.Add("photoset_id", photosetId); parameters.Add("comment_text", commentText); GetResponseAsync <UnknownResponse>( parameters, r => { FlickrResult <string> result = new FlickrResult <string>(); result.HasError = r.HasError; if (r.HasError) { result.Error = r.Error; } else { result.Result = r.Result.GetAttributeValue("*", "id"); } callback(result); }); }
/// <summary> /// Get an <see cref="OAuthRequestToken"/> for the given callback URL. /// </summary> /// <remarks>Specify 'oob' as the callback url for no callback to be performed.</remarks> /// <param name="callbackUrl">The callback Uri, or 'oob' if no callback is to be performed.</param> /// <param name="callback"></param> public async Task <FlickrResult <OAuthRequestToken> > OAuthGetRequestTokenAsync(string callbackUrl) { string url = RequestTokenUrl; //"https://www.flickr.com/services/oauth/request_token"; Dictionary <string, string> parameters = OAuthGetBasicParameters(); parameters.Add("oauth_callback", callbackUrl); string sig = OAuthCalculateSignature("POST", url, parameters, null); parameters.Add("oauth_signature", sig); FlickrResult <string> r = await TwitterResponder.GetDataResponseAsync(this, "POST", url, parameters); FlickrResult <OAuthRequestToken> result = new FlickrResult <OAuthRequestToken>(); if (!r.HasError) { result.Result = FlickrNet.OAuthRequestToken.ParseResponse(r.Result); } else { result.HasError = r.HasError; result.Error = r.Error; result.ErrorCode = r.ErrorCode; result.ErrorMessage = r.ErrorMessage; } return(result); }
private static void DownloadDataAsync(string method, string baseUrl, string data, string contentType, string authHeader, Action <FlickrResult <string> > callback) { WebClient client = new WebClient(); client.Encoding = System.Text.Encoding.UTF8; if (!String.IsNullOrEmpty(contentType)) { client.Headers["Content-Type"] = contentType; } if (!String.IsNullOrEmpty(authHeader)) { client.Headers["Authorization"] = authHeader; } if (method == "POST") { client.UploadStringCompleted += delegate(object sender, UploadStringCompletedEventArgs e) { FlickrResult <string> result = new FlickrResult <string>(); if (e.Error != null) { result.Error = e.Error; callback(result); return; } result.Result = e.Result; callback(result); return; }; client.UploadStringAsync(new Uri(baseUrl), data); } else { client.DownloadStringCompleted += delegate(object sender, DownloadStringCompletedEventArgs e) { FlickrResult <string> result = new FlickrResult <string>(); if (e.Error != null) { result.Error = e.Error; callback(result); return; } result.Result = e.Result; callback(result); return; }; client.DownloadStringAsync(new Uri(baseUrl)); } }
/// <summary> /// Returns the url to a group's page. /// </summary> /// <param name="groupId">The NSID of the group to fetch the url for.</param> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public async Task <FlickrResult <string> > UrlsGetGroupAsync(string groupId) { Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.urls.getGroup"); parameters.Add("group_id", groupId); var r = await GetResponseAsync <UnknownResponse>(parameters); FlickrResult <string> result = new FlickrResult <string>(); result.Error = r.Error; if (!r.HasError) { result.Result = r.Result.GetAttributeValue("*", "url"); } return(result); }
/// <summary> /// Get the tag list for a given photo. /// </summary> /// <param name="photoId">The id of the photo to return tags for.</param> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public async Task <FlickrResult <Collection <PhotoInfoTag> > > TagsGetListPhotoAsync(string photoId) { Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.tags.getListPhoto"); parameters.Add("api_key", apiKey); parameters.Add("photo_id", photoId); var r = await GetResponseAsync <PhotoInfo>(parameters); FlickrResult <Collection <PhotoInfoTag> > result = new FlickrResult <Collection <PhotoInfoTag> >(); result.Error = r.Error; if (!r.HasError) { result.Result = r.Result.Tags; } return(result); }
/// <summary> /// Returns the default privacy level preference for the user. /// </summary> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public async Task <FlickrResult <PrivacyFilter> > PrefsGetPrivacyAsync() { CheckRequiresAuthentication(); Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.prefs.getPrivacy"); var r = await GetResponseAsync <UnknownResponse>(parameters); FlickrResult <PrivacyFilter> result = new FlickrResult <PrivacyFilter>(); result.Error = r.Error; if (!r.HasError) { result.Result = (PrivacyFilter)int.Parse(r.Result.GetAttributeValue("*", "privacy"), System.Globalization.NumberFormatInfo.InvariantInfo); } return(result); }
/// <summary> /// Returns a group NSID, given the url to a group's page or photo pool. /// </summary> /// <param name="urlToFind">The url to the group's page or photo pool.</param> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public async Task <FlickrResult <string> > UrlsLookupGroupAsync(string urlToFind) { Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.urls.lookupGroup"); parameters.Add("api_key", apiKey); parameters.Add("url", urlToFind); var r = await GetResponseAsync <UnknownResponse>(parameters); FlickrResult <string> result = new FlickrResult <string>(); result.Error = r.Error; if (!r.HasError) { result.Result = r.Result.GetAttributeValue("*", "id"); } return(result); }
/// <summary> /// Returns the url to a group's page. /// </summary> /// <param name="groupId">The NSID of the group to fetch the url for.</param> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public void UrlsGetGroupAsync(string groupId, Action <FlickrResult <string> > callback) { var parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.urls.getGroup"); parameters.Add("group_id", groupId); GetResponseAsync <UnknownResponse>( parameters, r => { var result = new FlickrResult <string>(); result.Error = r.Error; if (!r.HasError) { result.Result = r.Result.GetAttributeValue("*", "url"); } callback(result); }); }
/// <summary> /// For a given request token and verifier string return an access token. /// </summary> /// <param name="requestToken"></param> /// <param name="requestTokenSecret"></param> /// <param name="verifier"></param> /// <param name="callback"></param> public void OAuthGetAccessTokenAsync(string requestToken, string requestTokenSecret, string verifier, Action <FlickrResult <OAuthAccessToken> > callback) { CheckApiKey(); #if SILVERLIGHT string url = "https://api.flickr.com/services/oauth/access_token"; #else string url = "https://www.flickr.com/services/oauth/access_token"; #endif Dictionary <string, string> parameters = OAuthGetBasicParameters(); parameters.Add("oauth_verifier", verifier); parameters.Add("oauth_token", requestToken); string sig = OAuthCalculateSignature("POST", url, parameters, requestTokenSecret); parameters.Add("oauth_signature", sig); FlickrResponder.GetDataResponseAsync(this, url, parameters, (r) => { FlickrResult <OAuthAccessToken> result = new FlickrResult <OAuthAccessToken>(); if (r.Error != null) { if (r.Error is System.Net.WebException) { OAuthException ex = new OAuthException(r.Error); result.Error = ex; } else { result.Error = r.Error; } callback(result); return; } result.Result = FlickrNet.OAuthAccessToken.ParseResponse(r.Result); callback(result); }); }
/// <summary> /// Returns the url to a user's profile. /// </summary> /// <param name="userId">The NSID of the user to fetch the url for. If omitted, the calling user is assumed.</param> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public async Task <FlickrResult <string> > UrlsGetUserProfileAsync(string userId) { Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.urls.getUserProfile"); if (userId != null && userId.Length > 0) { parameters.Add("user_id", userId); } var r = await GetResponseAsync <UnknownResponse>(parameters); FlickrResult <string> result = new FlickrResult <string>(); result.Error = r.Error; if (!r.HasError) { result.Result = r.Result.GetAttributeValue("*", "url"); } return(result); }
/// <summary> /// Get a list of current 'Pandas' supported by Flickr. /// </summary> /// <returns>An array of panda names.</returns> public async Task <FlickrResult <string[]> > PandaGetListAsync() { Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.panda.getList"); var r = await GetResponseAsync <UnknownResponse>(parameters); FlickrResult <string[]> result = new FlickrResult <string[]>(); result.HasError = r.HasError; if (r.HasError) { result.Error = r.Error; } else { result.Result = r.Result.GetElementArray("panda"); } return(result); }
/// <summary> /// Returns the default privacy level preference for the user. /// </summary> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public void PrefsGetPrivacyAsync(Action <FlickrResult <PrivacyFilter> > callback) { CheckRequiresAuthentication(); var parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.prefs.getPrivacy"); GetResponseAsync <UnknownResponse>( parameters, r => { var result = new FlickrResult <PrivacyFilter>(); result.Error = r.Error; if (!r.HasError) { result.Result = (PrivacyFilter)int.Parse(r.Result.GetAttributeValue("*", "privacy"), System.Globalization.NumberFormatInfo.InvariantInfo); } callback(result); }); }
/// <summary> /// Returns a group NSID, given the url to a group's page or photo pool. /// </summary> /// <param name="urlToFind">The url to the group's page or photo pool.</param> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public void UrlsLookupGroupAsync(string urlToFind, Action <FlickrResult <string> > callback) { var parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.urls.lookupGroup"); parameters.Add("api_key", apiKey); parameters.Add("url", urlToFind); GetResponseAsync <UnknownResponse>( parameters, r => { var result = new FlickrResult <string>(); result.Error = r.Error; if (!r.HasError) { result.Result = r.Result.GetAttributeValue("*", "id"); } callback(result); }); }
/// <summary> /// Get the tag list for a given photo. /// </summary> /// <param name="photoId">The id of the photo to return tags for.</param> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public void TagsGetListPhotoAsync(string photoId, Action <FlickrResult <Collection <PhotoInfoTag> > > callback) { var parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.tags.getListPhoto"); parameters.Add("api_key", apiKey); parameters.Add("photo_id", photoId); GetResponseAsync <PhotoInfo>( parameters, r => { var result = new FlickrResult <Collection <PhotoInfoTag> >(); result.Error = r.Error; if (!r.HasError) { result.Result = r.Result.Tags; } callback(result); }); }
/// <summary> /// Gets the currently authenticated users default hidden from search setting. /// </summary> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public void PrefsGetHiddenAsync(Action <FlickrResult <HiddenFromSearch> > callback) { CheckRequiresAuthentication(); Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.prefs.getHidden"); GetResponseAsync <UnknownResponse>( parameters, r => { FlickrResult <HiddenFromSearch> result = new FlickrResult <HiddenFromSearch>(); result.Error = r.Error; if (!r.HasError) { result.Result = (HiddenFromSearch)int.Parse(r.Result.GetAttributeValue("*", "hidden"), System.Globalization.NumberFormatInfo.InvariantInfo); } callback(result); }); }
/// <summary> /// Retrieve a temporary FROB from the Flickr service, to be used in redirecting the /// user to the Flickr web site for authentication. Only required for desktop authentication. /// </summary> /// <remarks> /// Pass the FROB to the <see cref="AuthCalcUrl"/> method to calculate the url. /// </remarks> /// <example> /// <code> /// string frob = flickr.AuthGetFrob(); /// string url = flickr.AuthCalcUrl(frob, AuthLevel.Read); /// /// // redirect the user to the url above and then wait till they have authenticated and return to the app. /// /// Auth auth = flickr.AuthGetToken(frob); /// /// // then store the auth.Token for later use. /// string token = auth.Token; /// </code> /// </example> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public async Task <FlickrResult <string> > AuthGetFrobAsync() { CheckSigned(); Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.auth.getFrob"); var r = await GetResponseAsync <UnknownResponse>(parameters); FlickrResult <string> result = new FlickrResult <string>(); result.HasError = r.HasError; if (r.HasError) { result.Error = r.Error; } else { result.Result = r.Result.GetElementValue("frob"); } return(result); }
/// <summary> /// Returns the url to a user's profile. /// </summary> /// <param name="userId">The NSID of the user to fetch the url for. If omitted, the calling user is assumed.</param> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public void UrlsGetUserProfileAsync(string userId, Action <FlickrResult <string> > callback) { var parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.urls.getUserProfile"); if (userId != null && userId.Length > 0) { parameters.Add("user_id", userId); } GetResponseAsync <UnknownResponse>( parameters, r => { var result = new FlickrResult <string>(); result.Error = r.Error; if (!r.HasError) { result.Result = r.Result.GetAttributeValue("*", "url"); } callback(result); }); }
/// <summary> /// Get a list of current 'Pandas' supported by Flickr. /// </summary> /// <returns>An array of panda names.</returns> public void PandaGetListAsync(Action <FlickrResult <string[]> > callback) { var parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.panda.getList"); GetResponseAsync <UnknownResponse>( parameters, r => { var result = new FlickrResult <string[]>(); result.HasError = r.HasError; if (r.HasError) { result.Error = r.Error; } else { result.Result = r.Result.GetElementArray("panda"); } callback(result); }); }
/// <summary> /// Adds a new comment to a photo. /// </summary> /// <param name="photoId">The ID of the photo to add the comment to.</param> /// <param name="commentText">The text of the comment. Can contain some HTML.</param> /// <param name="callback">Callback method to call upon return of the response from Flickr.</param> public async Task <FlickrResult <string> > PhotosCommentsAddCommentAsync(string photoId, string commentText) { Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("method", "flickr.photos.comments.addComment"); parameters.Add("photo_id", photoId); parameters.Add("comment_text", commentText); var r = await GetResponseAsync <UnknownResponse>(parameters); FlickrResult <string> result = new FlickrResult <string>(); result.HasError = r.HasError; if (r.HasError) { result.Error = r.Error; } else { result.Result = r.Result.GetAttributeValue("*", "id"); } return(result); }
private void UploadDataAsync(Stream imageStream, string fileName, Uri uploadUri, Dictionary <string, string> parameters, Action <FlickrResult <string> > callback) { string boundary = "FLICKR_MIME_" + DateTime.Now.ToString("yyyyMMddhhmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo); string authHeader = FlickrResponder.OAuthCalculateAuthHeader(parameters); var dataBuffer = CreateUploadData(imageStream, fileName, parameters, boundary); var req = (HttpWebRequest)WebRequest.Create(uploadUri); req.Method = "POST"; req.ContentType = "multipart/form-data; boundary=" + boundary; #if (!SILVERLIGHT && !WINDOWS_PHONE) req.SendChunked = true; #endif req.AllowWriteStreamBuffering = false; if (!string.IsNullOrEmpty(authHeader)) { req.Headers["Authorization"] = authHeader; } req.BeginGetRequestStream( r => { var result = new FlickrResult <string>(); using (var reqStream = req.EndGetRequestStream(r)) { try { var bufferSize = 32 * 1024; if (dataBuffer.Length / 100 > bufferSize) { bufferSize = bufferSize * 2; } dataBuffer.UploadProgress += (o, e) => { if (OnUploadProgress != null) { OnUploadProgress(this, e); } }; dataBuffer.CopyTo(reqStream, bufferSize); reqStream.Close(); } catch (Exception ex) { result.Error = ex; callback(result); } } req.BeginGetResponse( r2 => { try { var res = req.EndGetResponse(r2); var sr = new StreamReader(res.GetResponseStream()); var responseXml = sr.ReadToEnd(); sr.Close(); var t = new UnknownResponse(); ((IFlickrParsable)t).Load(responseXml); result.Result = t.GetElementValue("photoid"); result.HasError = false; } catch (Exception ex) { if (ex is WebException) { var oauthEx = new OAuthException(ex); result.Error = string.IsNullOrEmpty(oauthEx.Message) ? ex : oauthEx; } else { result.Error = ex; } } callback(result); }, this); }, this); }
private void UploadDataAsync(Stream imageStream, string fileName, Uri uploadUri, Dictionary <string, string> parameters, Action <FlickrResult <string> > callback) { string boundary = "FLICKR_MIME_" + DateTime.Now.ToString("yyyyMMddhhmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo); string authHeader = FlickrResponder.OAuthCalculateAuthHeader(parameters); byte[] dataBuffer = CreateUploadData(imageStream, fileName, parameters, boundary); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uploadUri); req.Method = "POST"; req.ContentType = "multipart/form-data; boundary=" + boundary; if (!String.IsNullOrEmpty(authHeader)) { req.Headers["Authorization"] = authHeader; } req.BeginGetRequestStream( r => { Stream s = req.EndGetRequestStream(r); int bufferSize = 1024 * 32; int soFar = 0; while (soFar < dataBuffer.Length) { if ((dataBuffer.Length - soFar) < bufferSize) { bufferSize = dataBuffer.Length - soFar; } s.Write(dataBuffer, soFar, bufferSize); soFar += bufferSize; if (OnUploadProgress != null) { UploadProgressEventArgs args = new UploadProgressEventArgs(soFar, dataBuffer.Length); OnUploadProgress(this, args); } } req.BeginGetResponse( r2 => { FlickrResult <string> result = new FlickrResult <string>(); try { WebResponse res = req.EndGetResponse(r2); StreamReader sr = new StreamReader(res.GetResponseStream()); string responseXml = sr.ReadToEnd(); XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; XmlReader reader = XmlReader.Create(new StringReader(responseXml), settings); if (!reader.ReadToDescendant("rsp")) { throw new XmlException("Unable to find response element 'rsp' in Flickr response"); } while (reader.MoveToNextAttribute()) { if (reader.LocalName == "stat" && reader.Value == "fail") { throw ExceptionHandler.CreateResponseException(reader); } continue; } reader.MoveToElement(); reader.Read(); UnknownResponse t = new UnknownResponse(); ((IFlickrParsable)t).Load(reader); result.Result = t.GetElementValue("photoid"); result.HasError = false; } catch (Exception ex) { if (ex is WebException) { OAuthException oauthEx = new OAuthException(ex); if (String.IsNullOrEmpty(oauthEx.Message)) { result.Error = ex; } else { result.Error = oauthEx; } } else { result.Error = ex; } } callback(result); }, this); }, this); }
public FlickrResultArgs(FlickrResult <T> result) { Error = result.Error; HasError = result.HasError; Result = result.Result; }