use of org.openlmis.stockmanagement.util.RequestParameters in project openlmis-stockmanagement by OpenLMIS.
the class BaseCommunicationService method findAll.
/**
* Return all reference data T objects.
*
* @param resourceUrl Endpoint url.
* @param parameters Map of query parameters.
* @return all reference data T objects.
*/
protected Collection<T> findAll(String resourceUrl, Map<String, Object> parameters) {
String url = getServiceUrl() + getUrl() + resourceUrl;
RequestParameters params = RequestParameters.of(parameters);
try {
ResponseEntity<T[]> responseEntity = doListRequest(url, params, HttpMethod.GET, getArrayResultClass());
return new ArrayList<>(Arrays.asList(responseEntity.getBody()));
} catch (HttpStatusCodeException ex) {
throw buildDataRetrievalException(ex);
}
}
use of org.openlmis.stockmanagement.util.RequestParameters in project openlmis-stockmanagement by OpenLMIS.
the class ApprovedProductReferenceDataService method getApprovedProducts.
/**
* Retrieves all facility approved products from the reference data service, based on the
* provided facility and full supply flag. It can be optionally filtered by the program ID.
*
* @param facilityId id of the facility
* @param programId id of the program
* @return a collection of approved products matching the search criteria
*/
public Page<OrderableDto> getApprovedProducts(UUID facilityId, UUID programId, Collection<UUID> orderableIds) {
RequestParameters params = RequestParameters.init();
params.set("programId", programId);
if (!isEmpty(orderableIds)) {
params.set("orderableId", orderableIds);
}
Page<ApprovedProductDto> approvedProductPage = getPage(facilityId + "/approvedProducts", params);
List<OrderableDto> content = approvedProductPage.getContent().stream().map(ApprovedProductDto::getOrderable).collect(Collectors.toList());
return Pagination.getPage(content);
}
use of org.openlmis.stockmanagement.util.RequestParameters in project openlmis-stockmanagement by OpenLMIS.
the class AuthService method obtainAccessToken.
/**
* Retrieves access token from the auth service.
*
* @return token.
*/
public String obtainAccessToken() {
String plainCreds = clientId + ":" + clientSecret;
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
HttpEntity<String> request = new HttpEntity<>(headers);
RequestParameters params = RequestParameters.init().set("grant_type", "client_credentials");
ResponseEntity<?> response = restTemplate.exchange(createUri(authorizationUrl, params), HttpMethod.POST, request, Object.class);
return ((Map<String, String>) response.getBody()).get(ACCESS_TOKEN);
}
use of org.openlmis.stockmanagement.util.RequestParameters in project openlmis-stockmanagement by OpenLMIS.
the class BaseCommunicationService method findOne.
/**
* Return one object from service.
*
* @param resourceUrl Endpoint url.
* @param parameters Map of query parameters.
* @param type set to what type a response should be converted.
* @return one reference data T objects.
*/
public T findOne(String resourceUrl, RequestParameters parameters, Class<T> type) {
String url = getServiceUrl() + getUrl() + StringUtils.defaultIfBlank(resourceUrl, "");
RequestParameters params = RequestParameters.init().setAll(parameters);
try {
ResponseEntity<T> responseEntity = restTemplate.exchange(RequestHelper.createUri(url, params), HttpMethod.GET, createEntity(), type);
return responseEntity.getBody();
} catch (HttpStatusCodeException ex) {
// rest template will handle 404 as an exception, instead of returning null
if (HttpStatus.NOT_FOUND == ex.getStatusCode()) {
logger.warn("{} matching params does not exist. Params: {}", getResultClass().getSimpleName(), parameters);
return null;
} else {
throw buildDataRetrievalException(ex);
}
}
}
use of org.openlmis.stockmanagement.util.RequestParameters in project openlmis-stockmanagement by OpenLMIS.
the class BaseCommunicationService method getPage.
protected <P> Page<P> getPage(String resourceUrl, Map<String, Object> parameters, Object payload, HttpMethod method, Class<P> type) {
RequestParameters params = RequestParameters.init();
parameters.forEach(params::set);
return getPage(resourceUrl, params, payload, method, type);
}
Aggregations