-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebApiClient.cs
134 lines (114 loc) · 4.85 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace WindowsGoiaService.APIService
{
public 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 PARA ASIGNAR EL TOKEN A LAS LLAMADAS
/// </summary>
/// <param name="token">Cadena de texto con el token</param>
/// <returns></returns>
public void setToken(string token)
{
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 COMPROBAR SI EL TOKEN ACTUAL ES VÁLIDO
/// </summary>
/// <param name="token">Cadena de texto con el token</param>
/// <returns></returns>
public async Task<dynamic> tryLoginWithToken(string token)
{
dynamic datos = null;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "api/account/getuser");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
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;
}
}
}