예제 #1
0
        /// <summary>
        /// Returns the object of a custom request with a provided endpoint.
        /// </summary>
        /// <typeparam name="T">The type that this request will be cast to.</typeparam>
        /// <param name="endpoint">The endpoint that will be appended to the base URL for this call.</param>
        /// <returns>An object of type T from the specified URL.</returns>
        protected async Task <T> GetAsync <T>(string endpoint)
        {
            HttpResponseMessage response = await HttpClient.GetAsync($"{DblApi.BaseUrl}{endpoint}");

            ApiResult <T> result;

            try
            {
                result = response.IsSuccessStatusCode
                                        ? ApiResult <T> .FromSuccess(JsonConvert.DeserializeObject <T>(await response.Content.ReadAsStringAsync()))
                                        : ApiResult <T> .FromHttpError(response.StatusCode);
            }
            catch (Exception ex)
            {
                result = ApiResult <T> .FromError(ex);
            }

            if (!result.IsSuccess)
            {
                throw new Exception(result.ErrorReason);
            }

            if (result.Value == null)
            {
                return(default);
예제 #2
0
        /// <summary>
        ///     Gets and parses objects
        /// </summary>
        /// <typeparam name="T">Type to parse to</typeparam>
        /// <param name="url">Url to get from</param>
        /// <returns>Object of type T</returns>
        protected async Task <T> GetAsync <T>(string url)
        {
            var t = await _httpClient.GetAsync(baseEndpoint + url);

            var payload = await t.Content.ReadAsStringAsync();

            var o      = JsonSerializer.Deserialize <T>(payload, _serializerOptions);
            var result = t.IsSuccessStatusCode
                ? ApiResult <T> .FromSuccess(await t.Content.ReadFromJsonAsync <T>(_serializerOptions))
                : ApiResult <T> .FromHttpError(t.StatusCode);

            return(result.Value);
        }
예제 #3
0
        /// <summary>
        /// Gets and parses objects
        /// </summary>
        /// <typeparam name="T">Type to parse to</typeparam>
        /// <param name="url">Url to get from</param>
        /// <returns>Object of type T</returns>
        protected async Task <T> GetAsync <T>(string url)
        {
            HttpResponseMessage t = await _httpClient.GetAsync(baseEndpoint + url);

            ApiResult <T> result;

            try
            {
                result = t.IsSuccessStatusCode ? ApiResult <T> .FromSuccess(JsonSerializer.Deserialize <T>(await t.Content.ReadAsStringAsync()))
                    : ApiResult <T> .FromHttpError(t.StatusCode);
            }
            catch (Exception ex)
            {
                result = ApiResult <T> .FromError(ex);
            }
            return(result.Value);
        }