Search in sources :

Example 1 with RequestParameters

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);
    }
}
Also used : ArrayList(java.util.ArrayList) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) RequestParameters(org.openlmis.stockmanagement.util.RequestParameters)

Example 2 with RequestParameters

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);
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) ApprovedProductDto(org.openlmis.stockmanagement.dto.referencedata.ApprovedProductDto) RequestParameters(org.openlmis.stockmanagement.util.RequestParameters)

Example 3 with RequestParameters

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) Map(java.util.Map) RequestParameters(org.openlmis.stockmanagement.util.RequestParameters)

Example 4 with RequestParameters

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);
        }
    }
}
Also used : HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) RequestParameters(org.openlmis.stockmanagement.util.RequestParameters)

Example 5 with RequestParameters

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);
}
Also used : RequestParameters(org.openlmis.stockmanagement.util.RequestParameters)

Aggregations

RequestParameters (org.openlmis.stockmanagement.util.RequestParameters)7 ApprovedProductDto (org.openlmis.stockmanagement.dto.referencedata.ApprovedProductDto)3 OrderableDto (org.openlmis.stockmanagement.dto.referencedata.OrderableDto)3 URI (java.net.URI)2 UUID (java.util.UUID)2 UUID.randomUUID (java.util.UUID.randomUUID)2 Test (org.junit.Test)2 DynamicPageTypeReference (org.openlmis.stockmanagement.util.DynamicPageTypeReference)2 HttpStatusCodeException (org.springframework.web.client.HttpStatusCodeException)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1