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

nl.pvanassen.steam.store.listing.ListingMutationHandle Maven / Gradle / Ivy

Go to download

A Java API to access the Steam community market through the HTTP interface. This may be against the TOS so be careful using it!

There is a newer version: 3.0.8
Show newest version
package nl.pvanassen.steam.store.listing;

import com.google.common.io.ByteStreams;
import nl.pvanassen.steam.http.DefaultHandle;
import nl.pvanassen.steam.store.CommandResult;
import nl.pvanassen.steam.store.common.GenericHandle;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

class ListingMutationHandle extends DefaultHandle {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    private final GenericHandle handle;
    private final String mutationType;
    
    ListingMutationHandle(String mutationType, GenericHandle handle) {
        this.mutationType = mutationType;
        this.handle = handle;
    }

    @Override
    public void handle(InputStream stream) throws IOException {
        super.handle(stream);
        handle.handle(CommandResult.success());
    }

    @Override
    public void handleError(InputStream stream) throws IOException {
        ObjectMapper om = new ObjectMapper();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteStreams.copy(stream, baos);
        String streamString = new String(baos.toByteArray(), Charset.forName("UTF-8")); 
        JsonNode node;
        try {
            node = om.readTree(streamString);
        }
        catch (JsonProcessingException e) {
            logger.error("Error parsing answer from Steam: '" + streamString + "'", e);
            handle.handle(CommandResult.error(e, "unknown erorr"));
            return;
        }
        if ((node == null) || (node.get("message") == null)) {
            logger.error("Error could not " + mutationType + " item: unknown error");
            handle.handle(CommandResult.error("unknown erorr"));
            return;
        }
        String message = node.get("message").asText();
        handle.handle(CommandResult.error(message));
        logger.error("Error could not " + mutationType + " item: " + message);
    }
    
    @Override
    public void handleException(Exception exception) {
        handle.handle(CommandResult.error(exception));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy