Exemplo n.º 1
0
        private WeatherDetails InvokeWeatherAPI(string uri)
        {
            WebRequest request = WebRequest.Create(uri);

            request.ContentType = "application/json; charset=utf-8";
            WebResponse response = request.GetResponse();


            // Display the status.
            // Console.WriteLine(((HttpWebResponse)response).StatusDescription);

            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            //Console.WriteLine(responseFromServer);
            // Clean up the streams and the response.

            dynamic responseJSON = JsonConvert.DeserializeObject(responseFromServer);
            var     weather      = responseJSON.main;

            WeatherDetails weatherrequest = JsonConvert.DeserializeObject <WeatherDetails>(System.Convert.ToString(weather));

            reader.Close();
            response.Close();

            return(weatherrequest);
        }
Exemplo n.º 2
0
        //public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "Weather/cityname/{cityname}")]HttpRequestMessage req, string cityname, TraceWriter log)
        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");

            // parse query parameter
            string cityname = req.GetQueryNameValuePairs()
                              .FirstOrDefault(q => string.Compare(q.Key, "cityname", true) == 0)
                              .Value;

            // Get request body
            //dynamic data = await req.Content.ReadAsAsync<object>();
            dynamic data = req.Content.ReadAsAsync <object>();

            // Set name to query string or body data
            //name = name ?? data?.name;

            cityname = cityname ?? data?.cityname;
            InvokeWebRequest weatherreq     = new InvokeWebRequest();
            WeatherDetails   weatherdetails = new WeatherDetails();

            weatherdetails = weatherreq.InvokeWeatherWebRequest(cityname);
            try
            {
                weatherdetails = weatherreq.InvokeWeatherWebRequest(cityname);
            }
            catch (System.Exception)
            {
                cityname = "Invalid City: " + cityname;
                return(req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(cityname)));
            }

            var sWeatherdetails = JsonConvert.SerializeObject(weatherdetails, Formatting.Indented);

            return(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(sWeatherdetails, Encoding.UTF8, "application/json")
            });

            //return cityname == null
            //    ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
            //    //: req.CreateResponse(HttpStatusCode.OK, "You asked for: " + cityname);
            //    : req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(weatherdetails));


            //return cityname == null
            //    ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
            //    //: req.CreateResponse(HttpStatusCode.OK, "You asked for: " + cityname);
            //    : req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(weatherdetails));

            // Fetching the name from the path parameter in the request URL
            //return req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
        }