-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebApiClient.cs
102 lines (89 loc) · 3.69 KB
/
WebApiClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace APIServiceDemo
{
class WebApiClient
{
#region Variables Privadas
private HttpClient client;
#endregion
#region Propiedades públicas
public System.Net.HttpStatusCode status { get; set; }
#endregion
public WebApiClient()
{
this.client = new HttpClient();
this.client.DefaultRequestHeaders.Accept.Clear();
this.client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public WebApiClient(string uri) : this()
{
this.client.BaseAddress = new Uri(uri);
}
public WebApiClient(string uri, string token): this(uri)
{
this.client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
/// <summary>
/// FUNCION ASINCRONA PARA OBTENER EL TOKEN DE ACCESO Y PODER SEGUIR UTILIZANDO EL RESTO DE LA API
/// </summary>
/// <param name="email">Correo electrónico del usuario</param>
/// <param name="pass">Contraseña asociada a ese correo</param>
/// <returns></returns>
public async Task<dynamic> Login(string email, string pass, int cooperativaid)
{
dynamic datos = null;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "token");
request.Content = new StringContent("username=" + email + "&password=" + pass + "&cooperativaid=" + cooperativaid + "&grant_type=password", Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
datos = await response.Content.ReadAsAsync<dynamic>();
}
this.status = response.StatusCode;
return datos;
}
/// <summary>
/// FUNCION PARA EJECUTAR LLAMADAS GET A LA WEB API
/// </summary>
/// <param name="uri">Ruta relativa de acceso al servicio</param>
/// <returns></returns>
public async Task<dynamic> GetAsync(string uri)
{
dynamic datos = null;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri);
HttpResponseMessage response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
datos = await response.Content.ReadAsAsync<dynamic>();
}
this.status = response.StatusCode;
return datos;
}
/// <summary>
/// FUNCION PARA EJECUTAR LLAMADAS POST A LA WEB API
/// </summary>
/// <param name="uri">Ruta relativa de acceso al servicio</param>
/// <param name="formBody">Contenido json que se desea enviar al servidor.
/// Por ejemplo, algo del tipo {\"name\":\"John Doe\",\"age\":33}</param>
/// <returns></returns>
public async Task<dynamic> PostAsync(string uri, string formBody)
{
dynamic datos = null;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri);
request.Content = new StringContent(formBody, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
datos = await response.Content.ReadAsAsync<dynamic>();
}
this.status = response.StatusCode;
return datos;
}
}
}