Beispiel #1
0
        /// <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);
            });
        }
Beispiel #2
0
        private static void DownloadDataAsync(string method, string baseUrl, string data, string contentType, string authHeader, string url, Action <FlickrResult <string> > callback)
        {
            var client = new HttpClient();
            //client.DefaultRequestHeaders.Accept.Clear();
            //if (!string.IsNullOrEmpty(contentType)) client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", contentType); //Keeps returning false
            //if (!string.IsNullOrEmpty(authHeader)) client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authHeader);

            var result = new FlickrResult <string>();

            if (method == "POST")
            {
                HttpContent content = new StringContent(data, Encoding.UTF8);
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                HttpResponseMessage response = client.PostAsync(baseUrl, content).Result;
                string statusCode            = response.StatusCode.ToString();

                response.EnsureSuccessStatusCode();
                Task <string> responseBody = response.Content.ReadAsStringAsync();
                result.Result = responseBody.Result;
                callback(result);
            }
            else
            {
                String response = client.GetStringAsync(url).Result;
                result.Result = response;
                callback(result);
            }
        }
 internal FlickrResultArgs(FlickrResult <T> result)
 {
     Error    = result.Error;
     HasError = result.HasError;
     Result   = result.Result;
 }
Beispiel #4
0
        private void GetResponseAsync <T>(Dictionary <string, string> parameters, Action <FlickrResult <T> > callback) where T : IFlickrParsable, 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) && !method.EndsWith("oauth.checkToken", StringComparison.Ordinal))
            {
                if (!string.IsNullOrEmpty(AuthToken))
                {
                    parameters["auth_token"] = AuthToken;
                }
            }
            else
            {
                // If OAuth Token exists or no authentication required then use new OAuth
                if (!string.IsNullOrEmpty(OAuthAccessToken) || string.IsNullOrEmpty(AuthToken))
                {
                    OAuthGetBasicParameters(parameters);
                    if (!string.IsNullOrEmpty(OAuthAccessToken))
                    {
                        parameters["oauth_token"] = OAuthAccessToken;
                    }
                }
                else
                {
                    parameters["auth_token"] = AuthToken;
                }
            }


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

            lastRequest = url;

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

                            var settings = new XmlReaderSettings();
                            settings.IgnoreWhitespace = true;
                            XmlReader reader          = XmlReader.Create(new StringReader(r.Result), 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();

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

                    if (callback != null)
                    {
                        callback(result);
                    }
                });
            }
            catch (Exception ex)
            {
                var result = new FlickrResult <T>();
                result.Error = ex;
                if (null != callback)
                {
                    callback(result);
                }
            }
        }