From a7f97ac2ad0ffc2780abb83c77229afd2b033412 Mon Sep 17 00:00:00 2001 From: Andrea Turli Date: Thu, 12 Jan 2017 10:21:24 +0100 Subject: [PATCH] add AddApiVersionToRequest filter --- .../org/jclouds/packet/PacketApiMetadata.java | 1 + .../jclouds/packet/features/ProjectApi.java | 3 +- .../filters/AddApiVersionToRequest.java | 57 +++++++++++++++++++ .../internal/BasePacketApiMockTest.java | 5 +- 4 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 providers/packet/src/main/java/org/jclouds/packet/filters/AddApiVersionToRequest.java diff --git a/providers/packet/src/main/java/org/jclouds/packet/PacketApiMetadata.java b/providers/packet/src/main/java/org/jclouds/packet/PacketApiMetadata.java index 75ba0e6817..e05685ef98 100644 --- a/providers/packet/src/main/java/org/jclouds/packet/PacketApiMetadata.java +++ b/providers/packet/src/main/java/org/jclouds/packet/PacketApiMetadata.java @@ -65,6 +65,7 @@ public class PacketApiMetadata extends BaseHttpApiMetadata { .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.>builder() .add(PacketHttpApiModule.class) diff --git a/providers/packet/src/main/java/org/jclouds/packet/features/ProjectApi.java b/providers/packet/src/main/java/org/jclouds/packet/features/ProjectApi.java index e6bf0ca7b0..afdf1ef3d7 100644 --- a/providers/packet/src/main/java/org/jclouds/packet/features/ProjectApi.java +++ b/providers/packet/src/main/java/org/jclouds/packet/features/ProjectApi.java @@ -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 { diff --git a/providers/packet/src/main/java/org/jclouds/packet/filters/AddApiVersionToRequest.java b/providers/packet/src/main/java/org/jclouds/packet/filters/AddApiVersionToRequest.java new file mode 100644 index 0000000000..70e66fb542 --- /dev/null +++ b/providers/packet/src/main/java/org/jclouds/packet/filters/AddApiVersionToRequest.java @@ -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 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(); + } +} diff --git a/providers/packet/src/test/java/org/jclouds/packet/compute/internal/BasePacketApiMockTest.java b/providers/packet/src/test/java/org/jclouds/packet/compute/internal/BasePacketApiMockTest.java index 5b8d6aebf1..9fe3d6fb51 100644 --- a/providers/packet/src/test/java/org/jclouds/packet/compute/internal/BasePacketApiMockTest.java +++ b/providers/packet/src/test/java/org/jclouds/packet/compute/internal/BasePacketApiMockTest.java @@ -57,6 +57,7 @@ public class BasePacketApiMockTest { protected MockWebServer server; protected PacketApi api; private Json json; + private ApiContext 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 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; }