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

io.github.dft.mayers.MayersSdk Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
package io.github.dft.mayers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import io.github.dft.mayers.model.ShipMethods;
import io.github.dft.mayers.model.account.AccountInformation;
import io.github.dft.mayers.common.LocalDateTimeDeserializer;
import io.github.dft.mayers.common.LocalDateTimeSerializer;
import io.github.dft.mayers.model.address.AccountAddresses;
import io.github.dft.mayers.model.item.ItemInformations;
import io.github.dft.mayers.model.order.CancelOrderResponse;
import io.github.dft.mayers.model.order.CreateOrderRequest;
import io.github.dft.mayers.model.order.CreateOrderResponse;
import io.github.dft.mayers.model.order.Orders;
import io.github.dft.mayers.model.tracking.TrackingNumbers;
import lombok.SneakyThrows;

import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.LocalDateTime;

import static io.github.dft.mayers.constantcode.ConstantCodes.*;

public class MayersSdk {

    private final ObjectMapper objectMapper;
    protected HttpClient client;
    protected String accountId;
    protected String apiKey;

    public MayersSdk(String accountId, String apiKey) {
        client = HttpClient.newHttpClient();

        ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
        module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
        objectMapper.registerModule(module);

        this.objectMapper = objectMapper;
        this.accountId = accountId;
        this.apiKey = "Espresso " + apiKey + ":1";
    }

    public ItemInformations getItemInformation(String itemNumbers) {
        URI uri  = baseUrl(ITEM_INFORMATION_ENDPOINT + "?ItemNumber=" + itemNumbers);
        HttpRequest request = get(uri);
        return getRequestWrapped(request, ItemInformations.class);
    }

    public AccountInformation getAccountInformation() {
        URI uri  = baseUrl(ACCOUNT_INFORMATION_ENDPOINT);
        HttpRequest request = get(uri);
        return getRequestWrapped(request, AccountInformation.class);
    }

    public AccountAddresses getAccountAddress() {
        URI uri  = baseUrl(ACCOUNT_ADDRESSSES_ENDPOINT);
        HttpRequest request = get(uri);
        return getRequestWrapped(request, AccountAddresses.class);
    }

    public ShipMethods getShipMethod() {
        URI uri  = baseUrl(SHIP_METHODS_ENDPOINT);
        HttpRequest request = get(uri);
        return getRequestWrapped(request, ShipMethods.class);
    }

    public TrackingNumbers getSalesTracking(String orderNumber) {
        URI uri  = baseUrl(SALES_TRACKING_ENDPOINT + "?OrderNumber=" + orderNumber);
        HttpRequest request = get(uri);
        return getRequestWrapped(request, TrackingNumbers.class);
    }


    public Orders getSalesCurrent() {
        URI uri  = baseUrl(SALES_CURRENT_ENDPOINT);
        HttpRequest request = get(uri);
        return getRequestWrapped(request, Orders.class);
    }

    public CancelOrderResponse cancelOrder(String orderNumber) {
        URI uri  = baseUrl(CANCEL_ORDER_ENDPOINT + "?OrderNumber=" + orderNumber);
        HttpRequest request = delete(uri);
        return getRequestWrapped(request, CancelOrderResponse.class);
    }

    public CreateOrderResponse createSalesOrder(CreateOrderRequest createOrderRequest) {
        URI uri  = baseUrl(CREATE_ORDER_ENDPOINT);
        HttpRequest request = post(uri, createOrderRequest);
        return getRequestWrapped(request, CreateOrderResponse.class);
    }

    @SneakyThrows
    public  T getRequestWrapped(HttpRequest request, Class tClass) {
        String body = client.send(request, HttpResponse.BodyHandlers.ofString()).body();
        return convertBody(body, tClass);
    }

    @SneakyThrows
    protected HttpResponse getFile(HttpRequest request) {
        return client.send(request, HttpResponse.BodyHandlers.ofInputStream());
    }

    protected URI baseUrl(String path) {
        return URI.create(BASE_ENDPOINT + path);
    }

    protected  HttpRequest post(URI uri, T tClass) {
        String jsonBody = convertBodyToString(tClass);

        return HttpRequest.newBuilder(uri)
                .header(AUTHORIZATION_HEADER, apiKey)
                .header(CONTENT_TYPE, "application/json")
                .header(ACCEPT, "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
                .build();
    }

    protected HttpRequest get(URI uri) {
        return HttpRequest.newBuilder(uri)
                .header(AUTHORIZATION_HEADER, apiKey)
                .header(CONTENT_TYPE, "application/json")
                .GET()
                .build();
    }

    protected HttpRequest delete(URI uri) {
        return HttpRequest.newBuilder(uri)
                .header(AUTHORIZATION_HEADER, apiKey)
                .header(CONTENT_TYPE, "application/json")
                .DELETE()
                .build();
    }

    @SneakyThrows
    private  T convertBody(String body, Class tClass) {
        return objectMapper.readValue(body, tClass);
    }

    @SneakyThrows
    private  String convertBodyToString(T tClass) {
        return objectMapper.writeValueAsString(tClass);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy