package ru.yandex.direct.http.smart.parameters;

import javax.annotation.Nullable;

import ru.yandex.direct.http.smart.converter.RequestBodyConverter;
import ru.yandex.direct.http.smart.core.SmartRequestBuilder;

import static com.google.common.base.Preconditions.checkNotNull;

public class PathParameterHandler<T> implements ParameterHandler<T> {
    private final String name;
    private final RequestBodyConverter<T> valueConverter;

    public PathParameterHandler(String name, RequestBodyConverter<T> valueConverter) {
        this.name = checkNotNull(name, "name == null");
        this.valueConverter = valueConverter;
    }

    @Override
    public void apply(SmartRequestBuilder builder, @Nullable T value) {
        if (value == null) {
            throw new IllegalArgumentException(
                    "Path parameter \"" + name + "\" value must not be null.");
        }
        builder.addPathParam(name, valueConverter.convert(value).asString());
    }
}
