Exemplo n.º 1
0
        /// <summary>
        ///     Attempts to deserialize and initialize the parent class with the response content using the
        ///         <see cref="IJsonSerializerService"/> defined in the calling <see cref="ClimaCellService"/> instance.
        /// </summary>
        public static new async Task <Realtime> Deserialize(HttpResponseMessage responseMessage, IJsonSerializerService jsonSerializerService)
        {
            Realtime r;

            try
            {
                r = await jsonSerializerService.DeserializeJsonAsync <Realtime>(responseMessage.Content?.ReadAsStringAsync()).ConfigureAwait(false);

                if (r != null)
                {
                    r.Response = new ClimaCellResponse(responseMessage);
                }
                else
                {
                    r = new Realtime {
                        Response = new ClimaCellResponse(responseMessage)
                    };
                }
            }
            catch (FormatException e)
            {
                r = new Realtime
                {
                    Response = new ClimaCellResponse
                    {
                        IsSuccessStatus = false,
                        ReasonPhrase    = $"Error parsing results: {e?.InnerException?.Message ?? e.Message}"
                    }
                };
            }
            return(r);
        }
        /// <summary>
        ///     A realtime call provides observational data at the present time, down to the minute.
        /// </summary>
        /// <param name="latitude">Latitude to request data for in decimal degrees, -87 to 89.</param>
        /// <param name="longitude">Longitude to request data for in decimal degrees, -180 to 180.</param>
        /// <param name="fields">Fields to request from climacell.</param>
        /// <param name="paramters">Optional request paramaters <see cref="OptionalParamters"/>.</param>
        /// <returns>A climacell <see cref="Realtime"/> response.</returns>
        public async Task <Realtime> GetRealtime(double latitude, double longitude, List <string> fields, OptionalParamters paramters = null)
        {
            if (fields.Count <= 0)
            {
                throw new ArgumentException($"{nameof(fields)} cannot be empty.");
            }

            var query    = BuildRequestUri(latitude, longitude, fields, paramters, Endpoint.Realtime);
            var response = await httpClient.HttpRequestAsync($"{baseUri}{query}").ConfigureAwait(false);

            return(await Realtime.Deserialize(response, jsonSerializerService));
        }