mirror of https://github.com/apache/jclouds.git
add AddApiVersionToRequest filter
This commit is contained in:
parent
34d013f7db
commit
a7f97ac2ad
|
@ -65,6 +65,7 @@ public class PacketApiMetadata extends BaseHttpApiMetadata<PacketApi> {
|
|||
.documentation(URI.create("https://www.packet.net/help/api/#"))
|
||||
.defaultEndpoint("https://api.packet.net")
|
||||
.defaultProperties(PacketApiMetadata.defaultProperties())
|
||||
.version("1")
|
||||
//.view(typeToken(ComputeServiceContext.class))
|
||||
.defaultModules(ImmutableSet.<Class<? extends Module>>builder()
|
||||
.add(PacketHttpApiModule.class)
|
||||
|
|
|
@ -36,6 +36,7 @@ import org.jclouds.packet.domain.Href;
|
|||
import org.jclouds.packet.domain.Project;
|
||||
import org.jclouds.packet.domain.internal.PaginatedCollection;
|
||||
import org.jclouds.packet.domain.options.ListOptions;
|
||||
import org.jclouds.packet.filters.AddApiVersionToRequest;
|
||||
import org.jclouds.packet.filters.AddXAuthTokenToRequest;
|
||||
import org.jclouds.packet.functions.BaseToPagedIterable;
|
||||
import org.jclouds.rest.annotations.Fallback;
|
||||
|
@ -49,7 +50,7 @@ import com.google.inject.TypeLiteral;
|
|||
|
||||
@Path("/projects")
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@RequestFilters(AddXAuthTokenToRequest.class)
|
||||
@RequestFilters({ AddXAuthTokenToRequest.class, AddApiVersionToRequest.class} )
|
||||
public interface ProjectApi {
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 org.jclouds.packet.filters;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import org.jclouds.http.HttpException;
|
||||
import org.jclouds.http.HttpRequest;
|
||||
import org.jclouds.http.HttpRequestFilter;
|
||||
import org.jclouds.rest.annotations.ApiVersion;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.net.HttpHeaders.ACCEPT;
|
||||
import static java.lang.String.format;
|
||||
|
||||
@Singleton
|
||||
public class AddApiVersionToRequest implements HttpRequestFilter {
|
||||
|
||||
private final String apiVersion;
|
||||
|
||||
@Inject
|
||||
AddApiVersionToRequest(@ApiVersion String apiVersion) {
|
||||
this.apiVersion = apiVersion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpRequest filter(final HttpRequest request) throws HttpException {
|
||||
Collection<String> accept = checkNotNull(request.getHeaders().get(ACCEPT), "accept header must not be null");
|
||||
String versionHeader = Joiner.on("; ").join(ImmutableList.builder()
|
||||
.addAll(accept)
|
||||
.add(format("version=%s", apiVersion))
|
||||
.build());
|
||||
return request.toBuilder()
|
||||
.replaceHeader(ACCEPT, versionHeader)
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -57,6 +57,7 @@ public class BasePacketApiMockTest {
|
|||
protected MockWebServer server;
|
||||
protected PacketApi api;
|
||||
private Json json;
|
||||
private ApiContext<PacketApi> ctx;
|
||||
|
||||
// So that we can ignore formatting.
|
||||
private final JsonParser parser = new JsonParser();
|
||||
|
@ -65,7 +66,7 @@ public class BasePacketApiMockTest {
|
|||
public void start() throws IOException {
|
||||
server = new MockWebServer();
|
||||
server.play();
|
||||
ApiContext<PacketApi> ctx = ContextBuilder.newBuilder("packet")
|
||||
ctx = ContextBuilder.newBuilder("packet")
|
||||
.credentials("", X_AUTHORIZATION_TOKEN)
|
||||
.endpoint(url(""))
|
||||
.modules(modules)
|
||||
|
@ -130,7 +131,7 @@ public class BasePacketApiMockTest {
|
|||
RecordedRequest request = server.takeRequest();
|
||||
assertEquals(request.getMethod(), method);
|
||||
assertEquals(request.getPath(), path);
|
||||
assertEquals(request.getHeader("Accept"), "application/json");
|
||||
assertEquals(request.getHeader("Accept"), "application/json; version=" + ctx.getMetadata().get("apiVersion"));
|
||||
assertEquals(request.getHeader("X-Auth-Token"), X_AUTHORIZATION_TOKEN);
|
||||
return request;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue