Skip to content
This repository has been archived by the owner on Nov 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #27 from Filter94/master
Browse files Browse the repository at this point in the history
Patch method fix
  • Loading branch information
kosteman authored Nov 13, 2017
2 parents 2b937ba + 7f7b9b4 commit 650be3a
Showing 1 changed file with 83 additions and 51 deletions.
134 changes: 83 additions & 51 deletions src/main/java/ru/sbtqa/tag/apifactory/rest/RestEntityImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.StringEntity;
Expand Down Expand Up @@ -43,33 +45,22 @@ public Bullet get(String url, Map<String, String> headers) throws ApiRestExcepti
LOG.info("Request url {}", url);
final HttpGet get = new HttpGet(url);

for (Map.Entry<String,String> h: headers.entrySet()) {
get.setHeader(h.getKey(), h.getValue());
}
setHeaders(headers, get);

LOG.info("Request headers {}", headers);

HttpResponse response = null;
HttpResponse response;
try {
response = client.execute(get);
} catch (IOException ex) {
LOG.error("There is an error in the request processing", ex);
throw new AutotestError(ex);

}
Map<String, String> headersResponse = new HashMap<>();
for (Header h : response.getAllHeaders()) {
ParamsHelper.addParam(h.getName(), h.getValue());
if (headersResponse.containsKey(h.getName())) {
headersResponse.put(h.getName(), headersResponse.get(h.getName()) + "; " + h.getValue());
} else {
headersResponse.put(h.getName(), h.getValue());
}
}
Map<String, String> headersResponse = getHeadersMap(response);

try {
bullet = new Bullet(headersResponse,
EntityUtils.toString(response.getEntity(), Props.get("api.encoding")));
String responseBody = getEntityString(response, Props.get("api.encoding"));
bullet = new Bullet(headersResponse, responseBody);
} catch (IOException | ParseException ex) {
LOG.error("Error in response body get", ex);
}
Expand All @@ -81,57 +72,98 @@ public Bullet get(String url, Map<String, String> headers) throws ApiRestExcepti
return bullet;
}

private String getEntityString(HttpResponse response, String encoding) throws IOException {
if (response.getEntity().getContentType() != null) {
return EntityUtils.toString(response.getEntity(), encoding);
}
return null;
}

private void setHeaders(final Map<String, String> headers, HttpGet get) {
for (Map.Entry<String,String> h: headers.entrySet()) {
get.setHeader(h.getKey(), h.getValue());
}
}

private Map<String, String> getHeadersMap(final HttpResponse response) {
Map<String, String> headersResponse = new HashMap<>();
for (Header h : response.getAllHeaders()) {
ParamsHelper.addParam(h.getName(), h.getValue());
if (headersResponse.containsKey(h.getName())) {
headersResponse.put(h.getName(), headersResponse.get(h.getName()) + "; " + h.getValue());
} else {
headersResponse.put(h.getName(), h.getValue());
}
}
return headersResponse;
}

@Override
public Bullet post(String url, Map<String, String> headers, Object body) throws ApiRestException {
HttpClient client = HttpClients.createDefault();

try {
LOG.info("Sending 'POST' request to URL : {}", url);
final HttpPost post = new HttpPost(url);
return performRequest(headers, body, client, post);
} catch (IOException ex) {
LOG.error("Failed to get response", ex);
throw new ApiRestException("Empty bullet", ex);
} finally {
HttpClientUtils.closeQuietly(client);
}
}

for (Map.Entry<String,String> h: headers.entrySet()) {
post.setHeader(h.getKey(), h.getValue());
}

LOG.info("Headers are: {}", headers);
@Override
public Bullet patch(String url, Map<String, String> headers, Object body) throws ApiRestException {
HttpClient client = HttpClients.createDefault();

List<NameValuePair> postParams = new ArrayList<>();
String encoding = Props.get("api.encoding");
if (body instanceof Map) {
Map<String, String> params = (Map<String, String>) body;
try {
LOG.info("Sending 'PATCH' request to URL : {}", url);
HttpPatch patch = new HttpPatch(url);
return performRequest(headers, body, client, patch);
} catch (IOException ex) {
LOG.error("Failed to get response", ex);
throw new ApiRestException("Empty bullet", ex);
} finally {
HttpClientUtils.closeQuietly(client);
}
}

for (Map.Entry<String,String> e: params.entrySet()) {
postParams.add(new BasicNameValuePair(e.getKey(), e.getValue()));
}
private Bullet performRequest(Map<String, String> headers, Object body, HttpClient client,
HttpEntityEnclosingRequestBase request) throws IOException {
for (Map.Entry<String,String> h: headers.entrySet()) {
request.setHeader(h.getKey(), h.getValue());
}

if (!postParams.isEmpty()) {
post.setEntity(new UrlEncodedFormEntity(postParams));
}
LOG.info("Body (form-data) is: {}", body);
} else if (body instanceof String) {
post.setEntity(new StringEntity((String) body, encoding));
}
LOG.info("Headers are: {}", headers);

HttpResponse response = client.execute(post);
List<NameValuePair> postParams = new ArrayList<>();
String encoding = Props.get("api.encoding");
if (body instanceof Map) {
Map<String, String> params = (Map<String, String>) body;

Map<String, String> headersResponse = new HashMap<>();
for (Header h : response.getAllHeaders()) {
ParamsHelper.addParam(h.getName(), h.getValue());
if (headersResponse.containsKey(h.getName())) {
headersResponse.put(h.getName(), headersResponse.get(h.getName()) + "; " + h.getValue());
} else {
headersResponse.put(h.getName(), h.getValue());
}
for (Map.Entry<String,String> e: params.entrySet()) {
postParams.add(new BasicNameValuePair(e.getKey(), e.getValue()));
}

return new Bullet(headersResponse, EntityUtils.toString(response.getEntity(), encoding));
} catch (IOException ex) {
LOG.error("Failed to get response", ex);
} finally {
HttpClientUtils.closeQuietly(client);
if (!postParams.isEmpty()) {
request.setEntity(new UrlEncodedFormEntity(postParams));
}
LOG.info("Body (form-data) is: {}", body);
} else if (body instanceof String) {
request.setEntity(new StringEntity((String) body, encoding));
}

return null;
}
HttpResponse response = client.execute(request);

Map<String, String> headersResponse = getHeadersMap(response);
String bodyResponse = null;
try {
bodyResponse = getEntityString(response, encoding);
} catch (Exception e) {
LOG.info(e.getMessage());
}
return new Bullet(headersResponse, bodyResponse);
}
}

0 comments on commit 650be3a

Please sign in to comment.