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

io.gs2.core.net.Gs2RestSessionTask Maven / Gradle / Ivy

The newest version!
package io.gs2.core.net;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.gs2.core.model.AsyncAction;
import io.gs2.core.model.AsyncResult;
import io.gs2.core.model.IResult;

import java.io.IOException;


public abstract class Gs2RestSessionTask extends Gs2SessionTask {

    protected HttpTaskBuilder builder;
    private AsyncAction> callback;

    public Gs2RestSessionTask(
            Gs2RestSession gs2RestSession,
            AsyncAction> callback
    ) {

        super(gs2RestSession);

        this.builder = HttpTaskBuilder.create();
        this.callback = callback;
    }

    public abstract T parse(JsonNode data);

    protected void prepareImpl() {
        this.builder.setHeader("X-GS2-CLIENT-ID", getGs2Session().getGs2Credential().getClientId());
        this.builder.setHeader("Authorization", "Bearer " + getProjectToken());
    }

    protected void executeImpl() {
        this.builder.build().send();
    }

    protected void triggerUserCallback(Gs2Response gs2Response) {
        try {
            if(gs2Response.getGs2Exception() == null) {
                T result = parse(new ObjectMapper().readTree(gs2Response.message));
                AsyncResult asyncResult = new AsyncResult<>(result, gs2Response.getGs2Exception());
                callback.callback(asyncResult);
            } else {
                AsyncResult asyncResult = new AsyncResult<>(null, gs2Response.getGs2Exception());
                callback.callback(asyncResult);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy