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

com.notronix.etsy.impl.method.UpdateInventoryMethod Maven / Gradle / Ivy

There is a newer version: 3.0.0014
Show newest version
package com.notronix.etsy.impl.method;

import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpMethods;
import com.google.api.client.http.UrlEncodedContent;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.notronix.etsy.api.model.ListingInventory;
import com.notronix.etsy.api.model.ListingProduct;
import com.notronix.etsy.impl.model.EtsyListingInventory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.notronix.albacore.ContainerUtils.thereAreOneOrMore;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang3.StringUtils.join;

public class UpdateInventoryMethod extends AbstractEtsyMethod
{
    private Long listingId;
    private ListingInventory inventory;

    @Override
    String getURI() {
        return "/listings/" + listingId + "/inventory";
    }

    @Override
    public boolean requiresOAuth() {
        return true;
    }

    @Override
    public String getRequestMethod() {
        return HttpMethods.PUT;
    }

    @Override
    public HttpContent getContent(Gson gson) {
        Map params = new HashMap<>();
        requireNonNull(inventory);
        List products = inventory.getProducts();
        requireNonNull(products);

        String productsParam = gson.toJson(products);
        params.put("products", productsParam);

        List priceControllers = inventory.getPriceControllingProperties();
        if (thereAreOneOrMore(priceControllers)) {
            String param = join(priceControllers, ",");
            params.put("price_on_property", param);
        }

        List quantityControllers = inventory.getQuantityControllingProperties();
        if (thereAreOneOrMore(quantityControllers)) {
            String param = join(quantityControllers, ",");
            params.put("quantity_on_property", param);
        }

        List skuControllers = inventory.getSkuControllingProperties();
        if (thereAreOneOrMore(skuControllers)) {
            String param = join(skuControllers, ",");
            params.put("sku_on_property", param);
        }

        return new UrlEncodedContent(params);
    }

    @Override
    public EtsyListingInventory getResponse(Gson gson, String jsonPayload) {
        EtsyResponse response =
                gson.fromJson(jsonPayload, new TypeToken>(){}.getType());

        return response.getResults();
    }

    public Long getListingId() {
        return listingId;
    }

    public void setListingId(Long listingId) {
        this.listingId = listingId;
    }

    public UpdateInventoryMethod withListingId(Long listingId) {
        this.listingId = listingId;
        return this;
    }

    public ListingInventory getInventory() {
        return inventory;
    }

    public void setInventory(ListingInventory inventory) {
        this.inventory = inventory;
    }

    public UpdateInventoryMethod withInventory(ListingInventory inventory) {
        this.inventory = inventory;
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy