Ejemplo n.º 1
0
        /// <summary>Makes a API POST call to create a new object.</summary>
        /// <param name="uri">The URI of the API call.</param>
        /// <param name="objJson">The object that will be created.</param>
        /// <param name="clientProvider">The client provider containing the HttpClient that will be used to make the API call.</param>
        public static async Task PostAsync(string uri, object objJson, HttpClientProvider clientProvider)
        {
            var json     = JsonConvert.SerializeObject(objJson);
            var response = await clientProvider.client.PostAsync(
                new Uri($"{clientProvider.client.BaseAddress + uri}"), new StringContent(json, Encoding.UTF8, "application/json"));

            ResponseChecker(response);
        }
Ejemplo n.º 2
0
        /// <summary>Makes a API GET call to get a list of objects.</summary>
        /// <typeparam name="T">The type of the object</typeparam>
        /// <param name="uri">The URI of the API call.</param>
        /// <param name="clientProvider">The client provider containing the HttpClient that will be used to make the API call.</param>
        /// <returns>A list of the objects that was requested</returns>
        public static async Task <List <T> > GetAllAsync <T>(string uri, HttpClientProvider clientProvider)
        {
            var response = await clientProvider.client.GetAsync(new Uri($"{clientProvider.client.BaseAddress + uri}"));

            ResponseChecker(response);
            var json = await response.Content.ReadAsStringAsync();

            return(JsonConvert.DeserializeObject <List <T> >(json));
        }
Ejemplo n.º 3
0
        internal static async Task GetCsrfToken(HttpClientProvider clientProvider, string url = "https://localhost:44348/api/authentication/login")
        {
            await clientProvider.client.GetAsync(url);

            var responseCookies = clientProvider.clientHandler.CookieContainer.GetCookies(new Uri(url)).Cast <Cookie>();

            foreach (var cookiee in responseCookies)
            {
                if (!cookiee.Name.Equals("XSRF-TOKEN", StringComparison.OrdinalIgnoreCase))
                {
                    clientProvider.clientHandler.CookieContainer.Add(cookiee);
                }
            }

            try
            {
                var cookieCsrf = responseCookies.Single(o => o.Name == "XSRF-TOKEN").Value;
                clientProvider.client.DefaultRequestHeaders.Add("X-XSRF-TOKEN", cookieCsrf);
            }
            catch
            {
            }
        }
Ejemplo n.º 4
0
        public static async Task <List <T> > SortListAsync <T>(string uri, List <T> listJson, HttpClientProvider clientProvider)
        {
            var json     = JsonConvert.SerializeObject(listJson);
            var response = await clientProvider.client.PutAsync(
                new Uri($"{clientProvider.client.BaseAddress + uri}"), new StringContent(json, Encoding.UTF8, "application/json"));

            ResponseChecker(response);
            return(JsonConvert.DeserializeObject <List <T> >(await response.Content.ReadAsStringAsync()));
        }
Ejemplo n.º 5
0
        /// <summary>Makes a API DELETE call to delete an object.</summary>
        /// <param name="uri">The URI of the API call.</param>
        /// <param name="clientProvider">The client provider containing the HttpClient that will be used to make the API call.</param>
        public static async Task DeleteAsync(string uri, HttpClientProvider clientProvider)
        {
            var response = await clientProvider.client.DeleteAsync(new Uri($"{clientProvider.client.BaseAddress + uri}"));

            ResponseChecker(response);
        }