Exemple #1
0
        /// <summary>
        /// Creates a GET request to api and returns the root element of the response
        /// </summary>
        /// <param name="method">Api method</param>
        /// <param name="parameters">Api parameters</param>
        /// <param name="root_name">The name of the root element</param>
        /// <returns>The root element of the response</returns>
        public async Task <XElement> GetRootElementAsync(string method, NameValueCollection parameters, string root_name)
        {
            if (parameters == null)
            {
                parameters = new NameValueCollection();
            }

            parameters["apikey"] = Key;

            var headers = new NameValueCollection();

            headers.Add("Accept", "application/xml");

            byte[] data = await HttpManager.GetAsync(endpoint, api, null, method, parameters, null, headers);

            using (var stream = new MemoryStream(data))
            {
                string s = System.Text.Encoding.ASCII.GetString(data);

                var x_doc = XDocument.Load(stream);

                var x_root = x_doc.Element(root_name);

                if (x_root.Element("Status").Element("Code").Value != "0")
                {
                    return(null); // TODO: throw am exception
                }

                return(x_root);
            }
        }
        /// <summary>
        /// Creates a GET request to apitude
        /// </summary>
        /// <param name="api">An api</param>
        /// <param name="api_version">An api version</param>
        /// <param name="method">An api method</param>
        /// <param name="parameters">Requets parameters</param>
        /// <returns>A request result (Response body)</returns>
        public async Task <byte[]> GetAsync(string api, string api_version, string method, NameValueCollection parameters)
        {
            if (requestDelay.ContainsKey(Key))
            {
                await requestDelay[Key];
            }
            var data = await HttpManager.GetAsync(ENDPOINT, api, api_version, method, parameters, null, createHeaders(Key, Secret));

            requestDelay[Key] = Task.Delay(DELAY_TIME);
            return(data);
        }
        /// <summary>
        /// Queries a weather forecast for nearest given count of days
        /// </summary>
        /// <param name="location"></param>
        /// <param name="count_of_days"></param>
        /// <returns>The weather forecast for nearest given count of days</returns>
        public async Task <Weather[]> GetWeatherAsync(Location location, uint count_of_days)
        {
            NameValueCollection parameters = new NameValueCollection();

            //parameters.Add("lat", location.Attitude.ToString());
            //parameters.Add("lon", location.Longitude.ToString());
            parameters.Add("q", "Sankt-Peterburg");
            parameters.Add("cnt", count_of_days.ToString());
            parameters.Add("mode", "xml");
            parameters.Add("appid", Key);

            byte[] data = await HttpManager.GetAsync(endpoint, api, version, "forecast/daily",
                                                     parameters, null, null);

            string str = Encoding.ASCII.GetString(data);

            XDocument response;

            using (var stream = new MemoryStream(data))
            {
                using (var reader = XmlReader.Create(stream))
                {
                    response = XDocument.Load(reader);
                }
            }

            var weatherdata = response.Root;

            float lat = float.Parse(weatherdata.Element("location").Element("location").Attribute("altitude").Value, CultureInfo.InvariantCulture);
            float lon = float.Parse(weatherdata.Element("location").Element("location").Attribute("longitude").Value, CultureInfo.InvariantCulture);

            Location loc = new Location {
                Attitude = lat, Longitude = lon
            };

            var weather = from time in response.Element("weatherdata").Element("forecast").Elements("time")
                          select new Weather
            {
                Region      = loc,
                Date        = DateTime.ParseExact(time.Attribute("day").Value, "yyyy-MM-dd", CultureInfo.InvariantCulture),
                Temperature = float.Parse(time.Element("temperature").Attribute("day").Value, CultureInfo.InvariantCulture) - KELVIN_CONST,
                Unit        = "°C"
            };

            return(weather.ToArray());
        }
Exemple #4
0
        /// <summary>
        /// Queries for currency rates by EUR
        /// </summary>
        /// <returns>Dictionary of currency rates. The base is EUR</returns>
        public async Task <Dictionary <string, float> > GetRatesAsync()
        {
            byte[] data = await HttpManager.GetAsync(endpoint, null, null, null, null, null, null);

            JObject jresponse;

            using (var stream = new MemoryStream(data))
            {
                using (var reader = new JsonTextReader(new StreamReader(stream)))
                {
                    jresponse = (JObject)JToken.ReadFrom(reader);
                }
            }

            IDictionary <string, JToken> dict = (JObject)jresponse["rates"];

            return(dict.ToDictionary(t => t.Key, t => (float)t.Value));
        }