namespace Firebase.Database.Http
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Net;
///
/// The http client extensions for object deserializations.
///
internal static class HttpClientExtensions
{
///
/// The get object collection async.
///
/// The client.
/// The request uri.
/// The specific JSON Serializer Settings.
/// The type of entities the collection should contain.
/// The .
public static async Task>> GetObjectCollectionAsync(this HttpClient client, string requestUri,
JsonSerializerSettings jsonSerializerSettings)
{
var responseData = string.Empty;
var statusCode = HttpStatusCode.OK;
try
{
var response = await client.GetAsync(requestUri).ConfigureAwait(false);
statusCode = response.StatusCode;
responseData = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var dictionary = JsonConvert.DeserializeObject>(responseData, jsonSerializerSettings);
if (dictionary == null)
{
return new FirebaseObject[0];
}
return dictionary.Select(item => new FirebaseObject(item.Key, item.Value)).ToList();
}
catch (Exception ex)
{
throw new FirebaseException(requestUri, string.Empty, responseData, statusCode, ex);
}
}
/*///
/// The get object collection async.
///
/// The client.
/// The request uri.
/// /// The Data Type.
/// The specific JSON Serializer Settings.
/// The type of entities the collection should contain.
/// The .
public static async Task>> GetObjectCollectionAsync(this HttpClient client, string requestUri,
JsonSerializerSettings jsonSerializerSettings, Type dataType)
{
var responseData = string.Empty;
var statusCode = HttpStatusCode.OK;
try
{
var response = await client.GetAsync(requestUri).ConfigureAwait(false);
statusCode = response.StatusCode;
responseData = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
response.EnsureSuccessStatusCode();
Type dicType = typeof(Dictionary<,>).MakeGenericType(typeof(string), dataType);
var dictionary = JsonConvert.DeserializeObject(responseData,dicType, jsonSerializerSettings) as Dictionary;
if (dictionary == null)
{
return new FirebaseObject