Skip to content

Commit 8fdd58a

Browse files
authored
Feature/ss730 (#55) (#56)
* update version * update readme * add auth v2 * add users v2 * add balance v2 * add request put and delete with body * add encoding utf-8 * fix methods * add constant user
1 parent 608a1b8 commit 8fdd58a

File tree

18 files changed

+1334
-902
lines changed

18 files changed

+1334
-902
lines changed

README.md

Lines changed: 275 additions & 177 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>mx.com.sw</groupId>
55
<artifactId>sdk-java18</artifactId>
6-
<version>0.0.14.1</version>
6+
<version>0.0.15.1</version>
77
<packaging>jar</packaging>
88
<properties>
99
<maven.compiler.source>1.8</maven.compiler.source>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package mx.com.sw.helpers;
2+
3+
/**
4+
* Representa filtros utilizados para cuentas con claves asociadas a consultas.
5+
*/
6+
public enum EnumAccountFilters {
7+
8+
/**
9+
* Filtro por correo electrónico.
10+
*/
11+
EMAIL("Email"),
12+
13+
/**
14+
* Filtro por identificación fiscal.
15+
*/
16+
TAX_ID("TaxId"),
17+
18+
/**
19+
* Filtro por ID de usuario.
20+
*/
21+
ID_USER("IdUser"),
22+
23+
/**
24+
* Filtro por estado activo/inactivo.
25+
*/
26+
IS_ACTIVE("IsActive");
27+
28+
// Campo privado que almacena la clave de consulta asociada al filtro
29+
private final String queryKey;
30+
31+
/**
32+
* Constructor para asociar la clave de consulta al filtro.
33+
*
34+
* @param queryKey La clave utilizada para la consulta asociada.
35+
*/
36+
EnumAccountFilters(String queryKey) {
37+
this.queryKey = queryKey;
38+
}
39+
40+
/**
41+
* Devuelve la clave de consulta asociada al filtro.
42+
*
43+
* @return La clave de consulta.
44+
*/
45+
public String getQueryKey() {
46+
return queryKey;
47+
}
48+
}

src/main/java/mx/com/sw/services/ResponseHandler.java

Lines changed: 151 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.ByteArrayInputStream;
77
import java.io.IOException;
88
import java.io.InputStream;
9+
import java.net.URI;
910
import java.util.Map;
1011
import java.util.concurrent.ExecutionException;
1112
import java.util.concurrent.Future;
@@ -19,8 +20,10 @@
1920
import org.apache.http.HttpStatus;
2021
import org.apache.http.client.config.RequestConfig;
2122
import org.apache.http.client.methods.HttpDelete;
23+
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
2224
import org.apache.http.client.methods.HttpGet;
2325
import org.apache.http.client.methods.HttpPost;
26+
import org.apache.http.client.methods.HttpPut;
2427
import org.apache.http.entity.BufferedHttpEntity;
2528
import org.apache.http.entity.ContentType;
2629
import org.apache.http.entity.StringEntity;
@@ -33,6 +36,7 @@
3336
/**
3437
* ResponseHandler Clase mediante la cual se hacen las peticiones y
3538
* des-serializaciones de respuestas.
39+
*
3640
* @param <T> IResponse subclasses
3741
* @author Juan Gamez
3842
* @version 0.0.0.1
@@ -46,6 +50,7 @@ public abstract class ResponseHandler<T> {
4650

4751
/**
4852
* Este método realiza un HTTP POST con la configuracion enviada.
53+
*
4954
* @param url String url o host.
5055
* @param path String path
5156
* @param headers Map String String con headers.
@@ -79,7 +84,80 @@ public T postHTTPJson(String url, String path, Map<String, String> headers, Stri
7984
HttpEntity responseEntity = response.getEntity();
8085
String responseBody = new String();
8186
if (responseEntity != null) {
82-
responseBody = EntityUtils.toString(responseEntity);
87+
responseBody = EntityUtils.toString(responseEntity,"UTF-8");
88+
return deserialize(responseBody, contentClass);
89+
} else {
90+
throw new GeneralException(
91+
response.getStatusLine() != null ? response.getStatusLine().getStatusCode()
92+
: HttpStatus.SC_UNPROCESSABLE_ENTITY,
93+
"Can´t get body from the request made.");
94+
}
95+
} else {
96+
throw new GeneralException(response.getStatusLine() != null ? response.getStatusLine().getStatusCode()
97+
: HttpStatus.SC_INTERNAL_SERVER_ERROR, "Error > 500 calling the service.");
98+
}
99+
} catch (IllegalArgumentException e) {
100+
return handleException(e);
101+
} catch (GeneralException e) {
102+
return handleException(e);
103+
} catch (InterruptedException e) {
104+
return handleException(e);
105+
} catch (ExecutionException e) {
106+
return handleException(e);
107+
} catch (TimeoutException e) {
108+
return handleException(e);
109+
} catch (IOException e) {
110+
return handleException(e);
111+
} catch (JsonSyntaxException e) {
112+
return handleException(e);
113+
} catch (ServicesException e) {
114+
return handleException(e);
115+
} finally {
116+
try {
117+
client.close();
118+
} catch (IOException e) {
119+
return handleException(e);
120+
}
121+
}
122+
}
123+
124+
/**
125+
* Este método realiza un HTTP POST con la configuracion enviada.
126+
*
127+
* @param url String url o host.
128+
* @param path String path
129+
* @param headers Map String String con headers.
130+
* @param jsonBody String body
131+
* @param configHTTP RequestConfig objeto de configuración.
132+
* @param contentClass Clase de respuesta esperada.
133+
* @return T
134+
*/
135+
public T putHTTPJson(String url, String path, Map<String, String> headers, String jsonBody,
136+
RequestConfig configHTTP, Class<T> contentClass) {
137+
CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
138+
try {
139+
client.start();
140+
HttpPut request = new HttpPut(url + path);
141+
if (headers != null) {
142+
for (Map.Entry<String, String> entry : headers.entrySet()) {
143+
request.addHeader(new BasicHeader(entry.getKey(), entry.getValue()));
144+
}
145+
}
146+
if (configHTTP != null) {
147+
request.setConfig(configHTTP);
148+
}
149+
if (jsonBody != null) {
150+
StringEntity sEntity = new StringEntity(jsonBody, "UTF-8");
151+
request.setEntity(sEntity);
152+
}
153+
Future<HttpResponse> future = client.execute(request, null);
154+
HttpResponse response = future.get(MAX_EXECUTION_TIME, TimeUnit.MINUTES);
155+
if (response.getStatusLine() != null
156+
&& response.getStatusLine().getStatusCode() < HttpStatus.SC_INTERNAL_SERVER_ERROR) {
157+
HttpEntity responseEntity = response.getEntity();
158+
String responseBody = new String();
159+
if (responseEntity != null) {
160+
responseBody = EntityUtils.toString(responseEntity, "UTF-8");
83161
return deserialize(responseBody, contentClass);
84162
} else {
85163
throw new GeneralException(
@@ -119,6 +197,7 @@ public T postHTTPJson(String url, String path, Map<String, String> headers, Stri
119197
/**
120198
* Este método realiza un HTTP POST (modo Multipart form data) con la
121199
* configuracion enviada.
200+
*
122201
* @param url String url o host.
123202
* @param path String path.
124203
* @param headers Map String String con headers.
@@ -155,7 +234,7 @@ public T postHTTPMultipart(String url, String path, Map<String, String> headers,
155234
HttpEntity responseEntity = response.getEntity();
156235
String responseBody = new String();
157236
if (responseEntity != null) {
158-
responseBody = EntityUtils.toString(responseEntity);
237+
responseBody = EntityUtils.toString(responseEntity,"UTF-8");
159238
return deserialize(responseBody, contentClass);
160239
} else {
161240
throw new GeneralException(
@@ -198,6 +277,7 @@ public T postHTTPMultipart(String url, String path, Map<String, String> headers,
198277

199278
/**
200279
* Este método realiza un HTTP GET con la configuracion enviada.
280+
*
201281
* @param url String url o host.
202282
* @param path String path.
203283
* @param headers Map String String con headers.
@@ -269,6 +349,7 @@ public T getHTTP(String url, String path, Map<String, String> headers, RequestCo
269349

270350
/**
271351
* Este método realiza un HTTP DELTE con la configuracion enviada.
352+
*
272353
* @param url String url o host.
273354
* @param path String path.
274355
* @param headers Map String String con headers.
@@ -290,26 +371,73 @@ public T deleteHTTP(String url, String path, Map<String, String> headers, Reques
290371
if (configHTTP != null) {
291372
request.setConfig(configHTTP);
292373
}
374+
375+
Future<HttpResponse> future = client.execute(request, null);
376+
HttpResponse response = future.get(MAX_EXECUTION_TIME, TimeUnit.MINUTES);
377+
int statusCode = response.getStatusLine().getStatusCode();
378+
if (statusCode == 204) { // 204 No Content
379+
String responseBody = "{ \"data\": \"Usuario borrado exitosamente.\", \"status\": \"success\" }";
380+
return deserialize(responseBody, contentClass);
381+
} else if (statusCode < HttpStatus.SC_INTERNAL_SERVER_ERROR) {
382+
HttpEntity responseEntity = response.getEntity();
383+
String responseBody = "";
384+
if (responseEntity != null) {
385+
responseBody = EntityUtils.toString(responseEntity, "UTF-8");
386+
return deserialize(responseBody, contentClass);
387+
} else {
388+
throw new GeneralException(statusCode, "Can´t get body from the request made.");
389+
}
390+
} else {
391+
throw new GeneralException(statusCode, "Error > 500 calling the service.");
392+
}
393+
} catch (IllegalArgumentException | GeneralException | InterruptedException | ExecutionException
394+
| TimeoutException | IOException | JsonSyntaxException | ServicesException e) {
395+
return handleException(e);
396+
} finally {
397+
try {
398+
client.close();
399+
} catch (IOException e) {
400+
return handleException(e);
401+
}
402+
}
403+
}
404+
405+
public T deleteHTTPJson(String url, String path, Map<String, String> headers, String jsonBody,
406+
RequestConfig configHTTP, Class<T> contentClass) {
407+
CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
408+
try {
409+
client.start();
410+
HttpDeleteWithBody request = new HttpDeleteWithBody(url + path);
411+
if (headers != null) {
412+
for (Map.Entry<String, String> entry : headers.entrySet()) {
413+
request.addHeader(new BasicHeader(entry.getKey(), entry.getValue()));
414+
}
415+
}
416+
if (configHTTP != null) {
417+
request.setConfig(configHTTP);
418+
}
419+
if (jsonBody != null) {
420+
StringEntity sEntity = new StringEntity(jsonBody, "UTF-8");
421+
request.setEntity(sEntity);
422+
}
293423
Future<HttpResponse> future = client.execute(request, null);
294424
HttpResponse response = future.get(MAX_EXECUTION_TIME, TimeUnit.MINUTES);
295425
if (response.getStatusLine() != null
296426
&& response.getStatusLine().getStatusCode() < HttpStatus.SC_INTERNAL_SERVER_ERROR) {
297427
HttpEntity responseEntity = response.getEntity();
298428
String responseBody = new String();
299429
if (responseEntity != null) {
300-
responseBody = EntityUtils.toString(responseEntity, "UTF-8");
430+
responseBody = EntityUtils.toString(responseEntity,"UTF-8");
301431
return deserialize(responseBody, contentClass);
302432
} else {
303433
throw new GeneralException(
304-
response.getStatusLine() != null
305-
? response.getStatusLine().getStatusCode()
434+
response.getStatusLine() != null ? response.getStatusLine().getStatusCode()
306435
: HttpStatus.SC_UNPROCESSABLE_ENTITY,
307-
"Can´t get body from the request made.");
436+
"Can't get body from the request made.");
308437
}
309438
} else {
310439
throw new GeneralException(
311-
response.getStatusLine() != null
312-
? response.getStatusLine().getStatusCode()
440+
response.getStatusLine() != null ? response.getStatusLine().getStatusCode()
313441
: HttpStatus.SC_INTERNAL_SERVER_ERROR,
314442
"Error > 500 calling the service.");
315443
}
@@ -338,8 +466,23 @@ public T deleteHTTP(String url, String path, Map<String, String> headers, Reques
338466
}
339467
}
340468

469+
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
470+
public static final String METHOD_NAME = "DELETE";
471+
472+
public HttpDeleteWithBody(final String uri) {
473+
super();
474+
setURI(URI.create(uri));
475+
}
476+
477+
@Override
478+
public String getMethod() {
479+
return METHOD_NAME;
480+
}
481+
}
482+
341483
/**
342484
* Este método realiza una deserializacion de un JSON al tipo de clase T.
485+
*
343486
* @param json String json.
344487
* @param contentClass Clase esperada de respuesta.
345488
* @return T

0 commit comments

Comments
 (0)