package org.xbib.io.webdav.client.methods;

import org.xbib.io.webdav.common.PollTimeoutHeader;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

/**
 * Represents an HTTP POLL request.
 * <p>
 * Note that "POLL" is a custom HTTP extension, not defined in a standards paper.
 */
public class Poll extends AbstractMethod {

    private final String subscriptionId;

    private final long timeout;

    public Poll(String subscriptionId, long timeout) {
        this.subscriptionId = subscriptionId;
        this.timeout = timeout;
    }

    @Override
    public String method() {
        return "POLL";
    }

    @Override
    public void accept(HttpRequest.Builder httpRequest) {
        super.accept(httpRequest);
        httpRequest.setHeader("SubscriptionId", subscriptionId);
        if (timeout > 0) {
            PollTimeoutHeader th = new PollTimeoutHeader(timeout);
            httpRequest.setHeader(th.getHeaderName(), th.getHeaderValue());
        }
    }

    @Override
    public boolean succeeded(HttpResponse<?> response) {
        return response.statusCode() == 200;
    }
}
