Exemple #1
0
        /// <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 void PhotosCommentsAddCommentAsync(string photoId, string commentText, Action <TwentyThreeResult <string> > callback)
        {
            var parameters = new Dictionary <string, string>();

            parameters.Add("method", "flickr.photos.comments.addComment");
            parameters.Add("photo_id", photoId);
            parameters.Add("comment_text", commentText);

            GetResponseAsync <UnknownResponse>(
                parameters,
                r =>
            {
                var result      = new TwentyThreeResult <string>();
                result.HasError = r.HasError;
                if (r.HasError)
                {
                    result.Error = r.Error;
                }
                else
                {
                    result.Result = r.Result.GetAttributeValue("*", "id");
                }
                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 <TwentyThreeResult <Place> > callback)
        {
            var 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 =>
            {
                var result   = new TwentyThreeResult <Place>();
                result.Error = r.Error;
                if (!r.HasError)
                {
                    result.Result = r.Result[0];
                }
                callback(result);
            });
        }
Exemple #3
0
        /// <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 void AuthGetFrobAsync(Action <TwentyThreeResult <string> > callback)
        {
            CheckSigned();

            var parameters = new Dictionary <string, string>();

            parameters.Add("method", "flickr.auth.getFrob");

            GetResponseAsync <UnknownResponse>(
                parameters,
                r =>
            {
                var result      = new TwentyThreeResult <string>();
                result.HasError = r.HasError;
                if (r.HasError)
                {
                    result.Error = r.Error;
                }
                else
                {
                    result.Result = r.Result.GetElementValue("frob");
                }
                callback(result);
            });
        }
Exemple #4
0
        /// <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 void PhotosNotesAddAsync(string photoId, int noteX, int noteY, int noteWidth, int noteHeight, string noteText, Action <TwentyThreeResult <string> > callback)
        {
            var 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);

            GetResponseAsync <UnknownResponse>(
                parameters,
                r =>
            {
                var result      = new TwentyThreeResult <string>();
                result.HasError = r.HasError;
                if (r.HasError)
                {
                    result.Error = r.Error;
                }
                else
                {
                    result.Result = r.Result.GetAttributeValue("*", "id");
                }
                callback(result);
            });
        }
Exemple #5
0
        private static void DownloadDataAsync(string method, string baseUrl, string data, string contentType, string authHeader, Action <TwentyThreeResult <string> > callback)
        {
            var 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)
                {
                    var result = new TwentyThreeResult <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)
                {
                    var result = new TwentyThreeResult <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 void UrlsGetGroupAsync(string groupId, Action <TwentyThreeResult <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 TwentyThreeResult <string>();
                result.Error = r.Error;
                if (!r.HasError)
                {
                    result.Result = r.Result.GetAttributeValue("*", "url");
                }
                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 <TwentyThreeResult <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 TwentyThreeResult <Collection <PhotoInfoTag> >();
                result.Error = r.Error;
                if (!r.HasError)
                {
                    result.Result = r.Result.Tags;
                }
                callback(result);
            });
        }
Exemple #8
0
        /// <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 <TwentyThreeResult <PrivacyFilter> > callback)
        {
            CheckRequiresAuthentication();

            var parameters = new Dictionary <string, string>();

            parameters.Add("method", "flickr.prefs.getPrivacy");

            GetResponseAsync <UnknownResponse>(
                parameters,
                r =>
            {
                var result   = new TwentyThreeResult <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 <TwentyThreeResult <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 TwentyThreeResult <string>();
                result.Error = r.Error;
                if (!r.HasError)
                {
                    result.Result = r.Result.GetAttributeValue("*", "id");
                }
                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 void UrlsGetUserProfileAsync(string userId, Action <TwentyThreeResult <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 TwentyThreeResult <string>();
                result.Error = r.Error;
                if (!r.HasError)
                {
                    result.Result = r.Result.GetAttributeValue("*", "url");
                }
                callback(result);
            });
        }
Exemple #11
0
        private void UploadDataAsync(Stream imageStream, string fileName, Uri uploadUri, Dictionary <string, string> parameters, Action <TwentyThreeResult <string> > callback)
        {
            string boundary = "TWENTYTHREE_MIME_" + DateTime.Now.ToString("yyyyMMddhhmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo);

            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;

            req.BeginGetRequestStream(
                r =>
            {
                using (var reqStream = req.EndGetRequestStream(r))
                {
                    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();
                }

                req.BeginGetResponse(
                    r2 =>
                {
                    var result = new TwentyThreeResult <string>();

                    try
                    {
                        var res         = req.EndGetResponse(r2);
                        var sr          = new StreamReader(res.GetResponseStream());
                        var responseXml = sr.ReadToEnd();
                        sr.Close();

                        var t = new UnknownResponse();
                        ((ITwentyThreeParsable)t).Load(responseXml);
                        result.Result   = t.GetElementValue("photoid");
                        result.HasError = false;
                    }
                    catch (Exception ex)
                    {
                        result.Error = ex;
                    }

                    callback(result);
                },
                    this);
            },
                this);
        }
Exemple #12
0
        private void DoGetResponseAsync <T>(Uri url, Action <TwentyThreeResult <T> > callback) where T : ITwentyThreeParsable, new()
        {
            string postContents = string.Empty;

            if (url.AbsoluteUri.Length > 2000)
            {
                postContents = url.Query.Substring(1);
                url          = new Uri(url, string.Empty);
            }

            var result = new TwentyThreeResult <T>();

            var request = (HttpWebRequest)WebRequest.Create(url);

            request.ContentType = "application/x-www-form-urlencoded";
            request.Method      = "POST";
            request.BeginGetRequestStream(requestAsyncResult =>
            {
                using (Stream s = request.EndGetRequestStream(requestAsyncResult))
                {
                    using (StreamWriter sw = new StreamWriter(s))
                    {
                        sw.Write(postContents);
                        sw.Close();
                    }
                    s.Close();
                }

                request.BeginGetResponse(responseAsyncResult =>
                {
                    try
                    {
                        var response = (HttpWebResponse)request.EndGetResponse(responseAsyncResult);
                        using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                        {
                            string responseXml = sr.ReadToEnd();

                            lastResponse = responseXml;

                            var t = new T();
                            ((ITwentyThreeParsable)t).Load(responseXml);
                            result.Result   = t;
                            result.HasError = false;

                            sr.Close();
                        }

                        if (null != callback)
                        {
                            callback(result);
                        }
                    }
                    catch (Exception ex)
                    {
                        result.Error = ex;
                        if (null != callback)
                        {
                            callback(result);
                        }
                    }
                }, null);
            }, null);
        }
Exemple #13
0
        private void GetResponseAsync <T>(Dictionary <string, string> parameters, Action <TwentyThreeResult <T> > callback) where T : ITwentyThreeParsable, new()
        {
            CheckApiKey();

            parameters["api_key"] = ApiKey;

            // If performing one of the old 'flickr.auth' methods then use old authentication details.
            string method = parameters["method"];

            if (method.StartsWith("flickr.auth", StringComparison.Ordinal))
            {
                if (!string.IsNullOrEmpty(AuthToken))
                {
                    parameters["auth_token"] = AuthToken;
                }
            }

            var url = CalculateUri(parameters, !string.IsNullOrEmpty(sharedSecret));

            lastRequest = url;

            try
            {
                TwentyThreeResponder.GetDataResponseAsync(this, BaseUri.AbsoluteUri, parameters, (r)
                                                          =>
                {
                    var result = new TwentyThreeResult <T>();
                    if (r.HasError)
                    {
                        result.Error = r.Error;
                    }
                    else
                    {
                        try
                        {
                            lastResponse = r.Result;

                            var t = new T();
                            ((ITwentyThreeParsable)t).Load(r.Result);
                            result.Result   = t;
                            result.HasError = false;
                        }
                        catch (Exception ex)
                        {
                            result.Error = ex;
                        }
                    }

                    if (callback != null)
                    {
                        callback(result);
                    }
                });
            }
            catch (Exception ex)
            {
                var result = new TwentyThreeResult <T>();
                result.Error = ex;
                if (null != callback)
                {
                    callback(result);
                }
            }
        }
 internal TwentyThreeResultArgs(TwentyThreeResult <T> result)
 {
     Error    = result.Error;
     HasError = result.HasError;
     Result   = result.Result;
 }