com.mycila.hc.HttpRequestBuilder.groovy Maven / Gradle / Ivy
/**
* Copyright (C) 2013 Mycila ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mycila.hc
import com.mycila.hc.io.ContentProvider
import com.mycila.hc.io.ContentProviderFactory
import com.mycila.hc.io.DelegatingContentProvider
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
/**
* @author Mathieu Carbou ([email protected])
* @date 2014-02-12
*/
class HttpRequestBuilder {
protected final HttpRequest request
protected final HttpClient client
HttpRequestBuilder(HttpClient client) {
this.client = client
this.request = new HttpRequest(
responseContentHandling: client.config.responseContentHandling
)
}
HttpRequestBuilder url(String url) {
request.url = url == null ? null : new URL(url)
return this
}
HttpRequestBuilder url(URL url) {
request.url = url
return this
}
HttpRequestBuilder method(HttpMethod method) {
request.method = method
return this
}
// content
HttpRequestBuilder form(HttpForm form) {
method(HttpMethod.POST)
if (!request.headers.contains(HttpHeader.CONTENT_TYPE)) {
contentType('application/x-www-form-urlencoded', StandardCharsets.UTF_8)
}
content(form)
return this
}
HttpRequestBuilder json(String json) {
if (!request.headers.contains(HttpHeader.CONTENT_TYPE)) {
contentType('application/json', StandardCharsets.UTF_8)
}
content(json)
return this
}
HttpRequestBuilder contentProvider(ContentProvider contentProvider) {
request.contentProvider = contentProvider
return this
}
HttpRequestBuilder contentProviderFactory(ContentProviderFactory factory) {
contentProvider(new DelegatingContentProvider(factory))
}
HttpRequestBuilder content(Object content) {
Class
© 2015 - 2025 Weber Informatics LLC | Privacy Policy