Search in sources :

Example 1 with HTTPClient

use of wslite.http.HTTPClient in project axelor-open-suite by axelor.

the class ECBCurrencyConversionService method validateAndGetRate.

@Override
public Float validateAndGetRate(int dayCount, Currency currencyFrom, Currency currencyTo, LocalDate date) throws AxelorException {
    try {
        HTTPResponse response = null;
        if (dayCount < 8) {
            HTTPClient httpclient = new HTTPClient();
            HTTPRequest request = new HTTPRequest();
            Map<String, Object> headers = new HashMap<>();
            headers.put("Accept", "application/json");
            request.setHeaders(headers);
            URL url = new URL(this.getUrlString(date, currencyFrom.getCode(), currencyTo.getCode()));
            // URL url = new URL(String.format(WSURL,currencyFrom.getCode()));
            LOG.trace("Currency conversion webservice URL: {}", new Object[] { url.toString() });
            request.setUrl(url);
            request.setMethod(HTTPMethod.GET);
            response = httpclient.execute(request);
            // JSONObject json = new JSONObject(response.getContentAsString());
            LOG.trace("Webservice response code: {}, response message: {}", response.getStatusCode(), response.getStatusMessage());
            if (response.getStatusCode() != 200)
                return -1f;
            if (response.getContentAsString().isEmpty()) {
                return this.validateAndGetRate((dayCount + 1), currencyFrom, currencyTo, date.minus(Period.ofDays(1)));
            } else {
                return this.getRateFromJson(currencyFrom, currencyTo, response);
            }
        } else {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, String.format(I18n.get(IExceptionMessage.CURRENCY_7), date.plus(Period.ofDays(1)), appBaseService.getTodayDate(Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null))));
        }
    } catch (Exception e) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CURRENCY_6));
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) HTTPRequest(wslite.http.HTTPRequest) User(com.axelor.auth.db.User) HashMap(java.util.HashMap) HTTPResponse(wslite.http.HTTPResponse) HTTPClient(wslite.http.HTTPClient) JSONObject(wslite.json.JSONObject) URL(java.net.URL) AxelorException(com.axelor.exception.AxelorException) JSONException(wslite.json.JSONException) MalformedURLException(java.net.MalformedURLException)

Example 2 with HTTPClient

use of wslite.http.HTTPClient in project axelor-open-suite by axelor.

the class FixerCurrencyConversionService method callApiBaseEuru.

private HTTPResponse callApiBaseEuru(Currency currencyFrom, Currency currencyTo, LocalDate date) throws MalformedURLException {
    HTTPClient httpclient = new HTTPClient();
    HTTPRequest request = new HTTPRequest();
    URL url = null;
    Map<String, Object> headers = new HashMap<>();
    headers.put("Accept", "application/json");
    request.setHeaders(headers);
    if (currencyFrom != null && currencyTo != null) {
        if (isRelatedConversion) {
            url = new URL(this.getUrlString(date, EURO_CURRENCY_CODE, currencyFrom.getCode(), currencyTo.getCode()));
        } else {
            url = new URL(this.getUrlString(date, currencyFrom.getCode(), currencyTo.getCode()));
        }
    } else {
        url = new URL(this.getUrlString(date, EURO_CURRENCY_CODE));
    }
    LOG.trace("Currency conversion webservice URL: {}", new Object[] { url.toString() });
    request.setUrl(url);
    request.setMethod(HTTPMethod.GET);
    return httpclient.execute(request);
}
Also used : HTTPRequest(wslite.http.HTTPRequest) HashMap(java.util.HashMap) HTTPClient(wslite.http.HTTPClient) JSONObject(wslite.json.JSONObject) URL(java.net.URL)

Aggregations

URL (java.net.URL)2 HashMap (java.util.HashMap)2 HTTPClient (wslite.http.HTTPClient)2 HTTPRequest (wslite.http.HTTPRequest)2 JSONObject (wslite.json.JSONObject)2 User (com.axelor.auth.db.User)1 AxelorException (com.axelor.exception.AxelorException)1 MalformedURLException (java.net.MalformedURLException)1 HTTPResponse (wslite.http.HTTPResponse)1 JSONException (wslite.json.JSONException)1