All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gocardless.services.CurrencyExchangeRateService Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
package com.gocardless.services;

import com.gocardless.http.*;
import com.gocardless.resources.CurrencyExchangeRate;
import com.google.common.collect.ImmutableMap;
import com.google.gson.reflect.TypeToken;
import java.util.List;
import java.util.Map;

/**
 * Service class for working with currency exchange rate resources.
 *
 * Currency exchange rates from our foreign exchange provider.
 */
public class CurrencyExchangeRateService {
    private final HttpClient httpClient;

    /**
     * Constructor. Users of this library should have no need to call this - an instance of this
     * class can be obtained by calling
     * {@link com.gocardless.GoCardlessClient#currencyExchangeRates() }.
     */
    public CurrencyExchangeRateService(HttpClient httpClient) {
        this.httpClient = httpClient;
    }

    /**
     * Returns a [cursor-paginated](#api-usage-cursor-pagination) list of all exchange rates.
     */
    public CurrencyExchangeRateListRequest> list() {
        return new CurrencyExchangeRateListRequest<>(httpClient,
                ListRequest.pagingExecutor());
    }

    public CurrencyExchangeRateListRequest> all() {
        return new CurrencyExchangeRateListRequest<>(httpClient,
                ListRequest.iteratingExecutor());
    }

    /**
     * Request class for {@link CurrencyExchangeRateService#list }.
     *
     * Returns a [cursor-paginated](#api-usage-cursor-pagination) list of all exchange rates.
     */
    public static final class CurrencyExchangeRateListRequest
            extends ListRequest {
        private String source;
        private String target;

        /**
         * Cursor pointing to the start of the desired set.
         */
        public CurrencyExchangeRateListRequest withAfter(String after) {
            setAfter(after);
            return this;
        }

        /**
         * Cursor pointing to the end of the desired set.
         */
        public CurrencyExchangeRateListRequest withBefore(String before) {
            setBefore(before);
            return this;
        }

        /**
         * Number of records to return.
         */
        public CurrencyExchangeRateListRequest withLimit(Integer limit) {
            setLimit(limit);
            return this;
        }

        /**
         * Source currency
         */
        public CurrencyExchangeRateListRequest withSource(String source) {
            this.source = source;
            return this;
        }

        /**
         * Target currency
         */
        public CurrencyExchangeRateListRequest withTarget(String target) {
            this.target = target;
            return this;
        }

        private CurrencyExchangeRateListRequest(HttpClient httpClient,
                ListRequestExecutor executor) {
            super(httpClient, executor);
        }

        public CurrencyExchangeRateListRequest withHeader(String headerName,
                String headerValue) {
            this.addHeader(headerName, headerValue);
            return this;
        }

        @Override
        protected Map getQueryParams() {
            ImmutableMap.Builder params = ImmutableMap.builder();
            params.putAll(super.getQueryParams());
            if (source != null) {
                params.put("source", source);
            }
            if (target != null) {
                params.put("target", target);
            }
            return params.build();
        }

        @Override
        protected String getPathTemplate() {
            return "currency_exchange_rates";
        }

        @Override
        protected String getEnvelope() {
            return "currency_exchange_rates";
        }

        @Override
        protected TypeToken> getTypeToken() {
            return new TypeToken>() {};
        }
    }
}