add AddApiVersionToRequest filter

This commit is contained in:
Andrea Turli 2017-01-12 10:21:24 +01:00
parent 34d013f7db
commit a7f97ac2ad
4 changed files with 63 additions and 3 deletions

View File

@ -65,6 +65,7 @@ public class PacketApiMetadata extends BaseHttpApiMetadata<PacketApi> {
.documentation(URI.create("https://www.packet.net/help/api/#")) .documentation(URI.create("https://www.packet.net/help/api/#"))
.defaultEndpoint("https://api.packet.net") .defaultEndpoint("https://api.packet.net")
.defaultProperties(PacketApiMetadata.defaultProperties()) .defaultProperties(PacketApiMetadata.defaultProperties())
.version("1")
//.view(typeToken(ComputeServiceContext.class)) //.view(typeToken(ComputeServiceContext.class))
.defaultModules(ImmutableSet.<Class<? extends Module>>builder() .defaultModules(ImmutableSet.<Class<? extends Module>>builder()
.add(PacketHttpApiModule.class) .add(PacketHttpApiModule.class)

View File

@ -36,6 +36,7 @@ import org.jclouds.packet.domain.Href;
import org.jclouds.packet.domain.Project; import org.jclouds.packet.domain.Project;
import org.jclouds.packet.domain.internal.PaginatedCollection; import org.jclouds.packet.domain.internal.PaginatedCollection;
import org.jclouds.packet.domain.options.ListOptions; import org.jclouds.packet.domain.options.ListOptions;
import org.jclouds.packet.filters.AddApiVersionToRequest;
import org.jclouds.packet.filters.AddXAuthTokenToRequest; import org.jclouds.packet.filters.AddXAuthTokenToRequest;
import org.jclouds.packet.functions.BaseToPagedIterable; import org.jclouds.packet.functions.BaseToPagedIterable;
import org.jclouds.rest.annotations.Fallback; import org.jclouds.rest.annotations.Fallback;
@ -49,7 +50,7 @@ import com.google.inject.TypeLiteral;
@Path("/projects") @Path("/projects")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@RequestFilters(AddXAuthTokenToRequest.class) @RequestFilters({ AddXAuthTokenToRequest.class, AddApiVersionToRequest.class} )
public interface ProjectApi { public interface ProjectApi {

View File

@ -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();
}
}

View File

@ -57,6 +57,7 @@ public class BasePacketApiMockTest {
protected MockWebServer server; protected MockWebServer server;
protected PacketApi api; protected PacketApi api;
private Json json; private Json json;
private ApiContext<PacketApi> ctx;
// So that we can ignore formatting. // So that we can ignore formatting.
private final JsonParser parser = new JsonParser(); private final JsonParser parser = new JsonParser();
@ -65,7 +66,7 @@ public class BasePacketApiMockTest {
public void start() throws IOException { public void start() throws IOException {
server = new MockWebServer(); server = new MockWebServer();
server.play(); server.play();
ApiContext<PacketApi> ctx = ContextBuilder.newBuilder("packet") ctx = ContextBuilder.newBuilder("packet")
.credentials("", X_AUTHORIZATION_TOKEN) .credentials("", X_AUTHORIZATION_TOKEN)
.endpoint(url("")) .endpoint(url(""))
.modules(modules) .modules(modules)
@ -130,7 +131,7 @@ public class BasePacketApiMockTest {
RecordedRequest request = server.takeRequest(); RecordedRequest request = server.takeRequest();
assertEquals(request.getMethod(), method); assertEquals(request.getMethod(), method);
assertEquals(request.getPath(), path); 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); assertEquals(request.getHeader("X-Auth-Token"), X_AUTHORIZATION_TOKEN);
return request; return request;
} }