GetDataResponseAsync() public static method

Gets a data response for the given base url and parameters, either using OAuth or not depending on which parameters were passed in.
public static GetDataResponseAsync ( Flickr flickr, string hashCall, string baseUrl, string>.Dictionary parameters ) : Task>
flickr Flickr The current instance of the class.
hashCall string
baseUrl string The base url to be called.
parameters string>.Dictionary A dictionary of parameters.
return Task>
        /// <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);
        }
        internal async Task <T> GetResponseAsync <T>(IDictionary <string, string> parameters) where T : class, IFlickrParsable, new()
        {
            if (!parameters.ContainsKey("oauth_consumer_key"))
            {
                parameters.Add("oauth_consumer_key", ApiKey);
            }

            var result = await FlickrResponder.GetDataResponseAsync(this, BaseApiUrl, parameters);

            var typeInfo = typeof(T).GetTypeInfo();

            if (typeInfo.IsEnum)
            {
                return(default(T));
            }
            if (typeInfo.ImplementedInterfaces.Any(t => t.Name == "IFlickrParsable"))
            {
                using (var reader = XmlReader.Create(new StringReader(result), new XmlReaderSettings {
                    IgnoreWhitespace = true
                }))
                {
                    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);
                        }
                    }

                    reader.MoveToElement();
                    reader.Read();

                    var item = new T();
                    item.Load(reader);
                    return(item);
                }
            }

            return(default(T));
        }
        /// <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);
            });
        }
 internal async Task GetResponseAsync(IDictionary <string, string> parameters)
 {
     await FlickrResponder.GetDataResponseAsync(this, BaseApiUrl, parameters);
 }
        private void GetResponseAsync <T>(Dictionary <string, string> parameters, Action <FlickrResult <T> > callback) where T : IFlickrParsable, new()
        {
            CheckApiKey();

            parameters["api_key"] = ApiKey;

            if (!String.IsNullOrEmpty(OAuthAccessToken) || String.IsNullOrEmpty(AuthToken))
            {
                OAuthGetBasicParameters(parameters);
                parameters.Remove("api_key");
                if (!String.IsNullOrEmpty(OAuthAccessToken))
                {
                    parameters["oauth_token"] = OAuthAccessToken;
                }
            }
            else
            {
                parameters["auth_token"] = AuthToken;
            }

            Uri url;

            if (!String.IsNullOrEmpty(sharedSecret))
            {
                url = CalculateUri(parameters, true);
            }
            else
            {
                url = CalculateUri(parameters, false);
            }

            lastRequest = url.AbsoluteUri;

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

                        XmlReaderSettings 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();

                        T t = new T();
                        ((IFlickrParsable)t).Load(reader);
                        result.Result   = t;
                        result.HasError = false;
                    }

                    if (callback != null)
                    {
                        callback(result);
                    }
                });
            }
            catch (Exception ex)
            {
                FlickrResult <T> result = new FlickrResult <T>();
                result.Error = ex;
                if (null != callback)
                {
                    callback(result);
                }
            }
        }
Example #6
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, (r)
                                                     =>
                {
                    var result = new FlickrResult <T>();
                    if (r.HasError)
                    {
                        result.Error = r.Error;
                    }
                    else
                    {
                        try
                        {
                            lastResponse = r.Result;

                            var t = new T();
                            ((IFlickrParsable)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 FlickrResult <T>();
                result.Error = ex;
                if (null != callback)
                {
                    callback(result);
                }
            }
        }
        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, (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);
                }
            }
        }
Example #8
0
        private async Task <FlickrResult <T> > GetResponseAsync <T>(Dictionary <string, string> parameters) where T : IFlickrParsable, new()
        {
            var hashApiSig = CalculateAuthSignature(parameters);

            CheckApiKey();

            FlickrResult <T> result = new FlickrResult <T>();

            var parseResult = new Func <Uri, string, T>((uri, response) =>
            {
                lastResponse = response;

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

                T t = new T();
                ((IFlickrParsable)t).Load(reader);

                return(t);
            });



            parameters["api_key"] = ApiKey;

            if (!String.IsNullOrEmpty(OAuthAccessToken) || String.IsNullOrEmpty(AuthToken))
            {
                OAuthGetBasicParameters(parameters);
                parameters.Remove("api_key");
                if (!String.IsNullOrEmpty(OAuthAccessToken))
                {
                    parameters["oauth_token"] = OAuthAccessToken;
                }
            }
            else
            {
                parameters["auth_token"] = AuthToken;
            }

            Uri url;

            if (!String.IsNullOrEmpty(sharedSecret))
            {
                url = CalculateUri(parameters, true);
            }
            else
            {
                url = CalculateUri(parameters, false);
            }

            lastRequest = url.AbsoluteUri;

            //get cached result if its there
            var cachedResult = TryGetCacheCall(hashApiSig);

            if (!string.IsNullOrEmpty(cachedResult))
            {
                result.Result   = parseResult(url, cachedResult);
                result.HashCall = hashApiSig;
                result.HasError = false;
                return(result);
            }


            try
            {
                var r = await FlickrResponder.GetDataResponseAsync(this, hashApiSig, BaseUri.AbsoluteUri, parameters);

                if (r.HasError)
                {
                    result.Error = r.Error;
                }
                else
                {
                    //cache results
                    CacheCall(r.HashCall, r.Result);

                    result.Result   = parseResult(url, r.Result);
                    result.HashCall = r.HashCall;
                    result.HasError = false;
                }
            }
            catch (Exception ex)
            {
                result.Error = ex;
            }

            return(result);
        }