PostStatusApiClientModule.java

package io.github.evisentin.wordpress.rest.client.adapter.apache.modules;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.evisentin.wordpress.rest.client.domain.api.PostStatusAPIs;
import io.github.evisentin.wordpress.rest.client.domain.model.WpStatus;
import lombok.NonNull;
import lombok.SneakyThrows;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.core5.net.URIBuilder;

import java.util.Map;

import static io.github.evisentin.wordpress.rest.client.adapter.apache.modules.TypeReferences.WP_STATUSES_MAP_TYPEREFERENCE;
import static io.github.evisentin.wordpress.rest.client.adapter.apache.modules.TypeReferences.WP_STATUS_TYPE_REFERENCE;
import static org.apache.commons.lang3.StringUtils.isBlank;

/**
 * Apache HttpClient implementation of {@link PostStatusAPIs}.
 *
 * <p>Provides access to WordPress post status definitions through the {@code /wp/v2/statuses} endpoint.</p>
 */
public class PostStatusApiClientModule extends ApiClientModule implements PostStatusAPIs {

    public PostStatusApiClientModule(final String apiUrl,
                                     final CloseableHttpClient httpClient,
                                     final ObjectMapper mapper) {
        super(apiUrl, httpClient, mapper);
    }

    @Override
    @SneakyThrows
    public WpStatus get(final @NonNull String name) {
        if (isBlank(name))
            throw new IllegalArgumentException("name cannot be blank");

        final URIBuilder builder = urlBuilder("${apiUrl}/wp/v2/statuses/${name}", Map.of(API_URL, apiUrl, "name", name));

        return performGetRequest(builder, WP_STATUS_TYPE_REFERENCE);
    }

    @Override
    @SneakyThrows
    public Map<String, WpStatus> list() {
        final URIBuilder builder = urlBuilder("${apiUrl}/wp/v2/statuses", Map.of(API_URL, apiUrl));

        return performGetRequest(builder, WP_STATUSES_MAP_TYPEREFERENCE);
    }
}