/// <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> /// Make a request to get forecast data. /// </summary> /// <param name="latitude">Latitude to request data for in decimal degrees.</param> /// <param name="longitude">Longitude to request data for in decimal degrees.</param> /// <param name="parameters">The OptionalParameters to use for the request.</param> /// <returns>A DarkSkyResponse with the API headers and data.</returns> public async Task <DarkSkyResponse> GetForecast(double latitude, double longitude, OptionalParameters parameters = null) { var requestString = BuildRequestUri(latitude, longitude, parameters); var response = await httpClient.HttpRequestAsync($"{baseUri}{requestString}").ConfigureAwait(false); var responseContent = response.Content?.ReadAsStringAsync(); var darkSkyResponse = new DarkSkyResponse { IsSuccessStatus = response.IsSuccessStatusCode, ResponseReasonPhrase = response.ReasonPhrase }; if (!darkSkyResponse.IsSuccessStatus) { return(darkSkyResponse); } try { darkSkyResponse.Response = await jsonSerializerService.DeserializeJsonAsync <Forecast>(responseContent).ConfigureAwait(false); } catch (FormatException e) { darkSkyResponse.Response = null; darkSkyResponse.IsSuccessStatus = false; darkSkyResponse.ResponseReasonPhrase = $"Error parsing results: {e?.InnerException?.Message ?? e.Message}"; } response.Headers.TryGetValues("X-Forecast-API-Calls", out var apiCallsHeader); response.Headers.TryGetValues("X-Response-Time", out var responseTimeHeader); darkSkyResponse.Headers = new ResponseHeaders { CacheControl = response.Headers.CacheControl, ApiCalls = long.TryParse(apiCallsHeader?.FirstOrDefault(), out var callsParsed) ? (long?)callsParsed : null, ResponseTime = responseTimeHeader?.FirstOrDefault() }; if (darkSkyResponse.Response == null) { return(darkSkyResponse); } if (darkSkyResponse.Response.Currently != null) { darkSkyResponse.Response.Currently.TimeZone = darkSkyResponse.Response.TimeZone; } darkSkyResponse.Response.Alerts?.ForEach(a => a.TimeZone = darkSkyResponse.Response.TimeZone); darkSkyResponse.Response.Daily?.Data?.ForEach(d => d.TimeZone = darkSkyResponse.Response.TimeZone); darkSkyResponse.Response.Hourly?.Data?.ForEach(h => h.TimeZone = darkSkyResponse.Response.TimeZone); darkSkyResponse.Response.Minutely?.Data?.ForEach( m => m.TimeZone = darkSkyResponse.Response.TimeZone); return(darkSkyResponse); }
/// <summary> /// Attempts to initalize a new <see cref="Hourly"/> from the responding <see cref="HttpResponseMessage"/>, and deserializes the response content /// using the <see cref="IJsonSerializerService"/> defined in the calling <see cref="ClimaCellService"/> instance and appends all model objects into the <see cref="ForecastResponse{T}.DataPoints"/> collection. /// </summary> public static new async Task <Nowcast> Deserialize(HttpResponseMessage responseMessage, IJsonSerializerService jsonSerializerService) { Nowcast h = new Nowcast() { Response = new ClimaCellResponse(responseMessage) }; try { h._dataPoints = await jsonSerializerService.DeserializeJsonAsync <List <Nowcast.Model> >(responseMessage.Content?.ReadAsStringAsync()).ConfigureAwait(false); } catch (FormatException e) { h.Response.IsSuccessStatus = false; h.Response.ReasonPhrase = $"Error parsing results: {e?.InnerException?.Message ?? e.Message}"; } return(h); }
/// <summary> /// Attempts to initalize a new <see cref="Daily"/> from the responding <see cref="HttpResponseMessage"/>, and deserializes the response content /// using the <see cref="IJsonSerializerService"/> defined in the calling <see cref="ClimaCellService"/> instance and appends all model objects into the <see cref="ForecastResponse{T}.DataPoints"/> collection. /// /// After deserializing, it'll then convert the private <see cref="_model"/> objects into the more readable public Daily <see cref="Model"/> using <see cref="_model.ToDaily(_model)"/>. /// </summary> public static new async Task <Daily> Deserialize(HttpResponseMessage responseMessage, IJsonSerializerService jsonSerializerService) { Daily d = new Daily() { Response = new ClimaCellResponse(responseMessage) }; try { var models = await jsonSerializerService.DeserializeJsonAsync <List <_model> >(responseMessage.Content?.ReadAsStringAsync()).ConfigureAwait(false); d._dataPoints = models.ConvertAll(d => _model.ToDaily(d)); } catch (FormatException e) { d.Response.IsSuccessStatus = false; d.Response.ReasonPhrase = $"Error parsing results: {e?.InnerException?.Message ?? e.Message}"; } return(d); }
private static async Task <TouTiaoResponse <T> > ParseContent <T>(HttpResponseMessage response) where T : class { TouTiaoResponse <T> res = new TouTiaoResponse <T>() { IsSuccessStatus = response.IsSuccessStatusCode }; if (!res.IsSuccessStatus) { return(null); } var contentType = response.Content.Headers.ContentType; switch (contentType.MediaType) { case "application/json": break; default: res.Response = response.Content as T; return(res); } Task <String> responseContent = response.Content?.ReadAsStringAsync(); jsonSerializerService = jsonSerializerService ?? new JsonNetJsonSerializerService(); try { res.Error = await jsonSerializerService.DeserializeJsonAsync <TouTiaoError>(responseContent).ConfigureAwait(false); if (res.Error.errcode == 0 && String.IsNullOrEmpty(res.Error.errmsg)) { res.Error = null; } if (null == res.Error) { res.Response = await jsonSerializerService.DeserializeJsonAsync <T>(responseContent).ConfigureAwait(false); if (null != res.Response) { res.IsSuccessStatus = true; } } } catch (FormatException e) { res.Response = null; res.IsSuccessStatus = false; res.ResponseReasonPhrase = $"Error parsing results: {e?.InnerException?.Message ?? e.Message}"; } if (null != res.Response) { response.Headers.TryGetValues("x-tt-timestamp", out var responseTimeHeader); res.Headers = new ResponseHeaders { CacheControl = response.Headers.CacheControl, ResponseTime = responseTimeHeader?.FirstOrDefault(), ContentType = response.Content.Headers.ContentType }; } return(res); }
public async Task <ClimaCellResponse> GetForecast(double latitude, double longitude) { var requestString = BuildRequestUri(latitude, longitude); var response = await httpClient.HttpRequestAsync($"{baseUri}{requestString}").ConfigureAwait(false); var responseContent = response.Content?.ReadAsStringAsync(); var climaCellResponse = new ClimaCellResponse { IsSuccessStatus = response.IsSuccessStatusCode, ResponseReasonPhrase = response.ReasonPhrase }; if (!climaCellResponse.IsSuccessStatus) { return(climaCellResponse); } try { climaCellResponse.Response = await jsonSerializerService.DeserializeJsonAsync <RealTime>(responseContent).ConfigureAwait(false); } catch (FormatException e) { climaCellResponse.Response = null; climaCellResponse.IsSuccessStatus = false; climaCellResponse.ResponseReasonPhrase = $"Error parsing results: {e?.InnerException?.Message ?? e.Message}"; } response.Headers.TryGetValues("X-RateLimit-Limit-day", out var rateLimitLimitDay); response.Headers.TryGetValues("X-RateLimit-Limit-hour", out var rateLimitLimitHour); response.Headers.TryGetValues("X-RateLimit-Remaining-day", out var rateLimitRemainingDay); response.Headers.TryGetValues("X-RateLimit-Remaining-hour", out var rateLimitRemainingHour); response.Headers.TryGetValues("Date", out var responseTimeHeader); climaCellResponse.Headers = new ResponseHeaders { CacheControl = response.Headers.CacheControl, RateLimitLimitDay = long.TryParse(rateLimitLimitDay?.FirstOrDefault(), out var rateLimitLimitDayParsed) ? (long?)rateLimitLimitDayParsed : null, RateLimitLimitHour = long.TryParse(rateLimitLimitHour?.FirstOrDefault(), out var rateLimitLimitHourParsed) ? (long?)rateLimitLimitHourParsed : null, RateLimitRemainingDay = long.TryParse(rateLimitRemainingDay?.FirstOrDefault(), out var rateLimitRemainingDayParsed) ? (long?)rateLimitRemainingDayParsed : null, RateLimitRemainingHour = long.TryParse(rateLimitRemainingHour?.FirstOrDefault(), out var rateLimitRemainingHourParsed) ? (long?)rateLimitRemainingHourParsed : null, ResponseTime = responseTimeHeader?.FirstOrDefault() }; if (climaCellResponse.Response == null) { return(climaCellResponse); } return(climaCellResponse); }