diff --git a/sdk/appservice/mgmt/src/test/java/com/azure/resourcemanager/appservice/AppServiceTest.java b/sdk/appservice/mgmt/src/test/java/com/azure/resourcemanager/appservice/AppServiceTest.java index 814bc570538f..a7cfd3991baf 100644 --- a/sdk/appservice/mgmt/src/test/java/com/azure/resourcemanager/appservice/AppServiceTest.java +++ b/sdk/appservice/mgmt/src/test/java/com/azure/resourcemanager/appservice/AppServiceTest.java @@ -34,11 +34,11 @@ import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; -import java.net.URL; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.time.temporal.ChronoUnit; +import com.azure.resourcemanager.resources.fluentcore.utils.Utils; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.junit.jupiter.api.Assertions; @@ -171,7 +171,7 @@ public static void uploadFileToWebApp(PublishingProfile profile, String fileName protected Response curl(String urlString) throws IOException { try { - return stringResponse(httpClient.getString(getHost(urlString), getPathAndQuery(urlString))).block(); + return stringResponse(httpClient.getString(Utils.getHost(urlString), Utils.getPathAndQuery(urlString))).block(); } catch (MalformedURLException e) { Assertions.fail(); return null; @@ -180,7 +180,7 @@ protected Response curl(String urlString) throws IOException { protected String post(String urlString, String body) { try { - return stringResponse(httpClient.postString(getHost(urlString), getPathAndQuery(urlString), body)) + return stringResponse(httpClient.postString(Utils.getHost(urlString), Utils.getPathAndQuery(urlString), body)) .block() .getValue(); } catch (Exception e) { @@ -201,23 +201,6 @@ private static Mono> stringResponse(Mono invokeTaskAsync(TaskGroup.InvocationContext context) { .doOnNext(createdExternalChild -> externalChild.setPendingOperation(PendingOperation.None)) .map(updatedExternalChild -> updatedExternalChild); case ToBeRemoved: - // With 2.0 runtime, deleteResourceAsync() will be - // returning 'Completable' then use below code instead - // - // return this.externalChild.deleteResourceAsync().doOnCompleted(new Action0() { - // @Override - // public void call() { - // externalChild.setPendingOperation(PendingOperation.None); - // } - // }).andThen(voidObservable()); - // - // TODO: Fix void mono result. return this.externalChild.deleteResourceAsync() .doOnSuccess(aVoid -> externalChild.setPendingOperation(PendingOperation.None)) .map(aVoid -> voidIndexable()); diff --git a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/policy/ProviderRegistrationPolicy.java b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/policy/ProviderRegistrationPolicy.java index 1943e854e7e4..ac8386bbaf5e 100644 --- a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/policy/ProviderRegistrationPolicy.java +++ b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/policy/ProviderRegistrationPolicy.java @@ -66,7 +66,6 @@ public Mono process(HttpPipelineCallContext context, HttpPipelineN } if (cloudError != null && MISSING_SUBSCRIPTION_REGISTRATION.equals(cloudError.getCode())) { - // TODO: add proxy in rest client ResourceManager resourceManager = ResourceManager.authenticate(credential, profile) .withDefaultSubscription(); Pattern providerPattern = Pattern.compile(".*'(.*)'"); diff --git a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/utils/Utils.java b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/utils/Utils.java index 665ccaf4e6b0..3f33fafeabf4 100644 --- a/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/utils/Utils.java +++ b/sdk/resources/mgmt/src/main/java/com/azure/resourcemanager/resources/fluentcore/utils/Utils.java @@ -4,19 +4,27 @@ package com.azure.resourcemanager.resources.fluentcore.utils; import com.azure.core.annotation.Get; +import com.azure.core.annotation.Host; +import com.azure.core.annotation.HostParam; import com.azure.core.annotation.PathParam; +import com.azure.core.annotation.ServiceInterface; import com.azure.core.http.HttpPipeline; import com.azure.core.http.HttpRequest; -import com.azure.core.http.HttpResponse; import com.azure.core.http.rest.PagedIterable; import com.azure.core.http.rest.RestProxy; +import com.azure.core.http.rest.SimpleResponse; import com.azure.core.management.AzureEnvironment; +import com.azure.core.util.FluxUtil; import com.azure.core.util.logging.ClientLogger; import com.azure.resourcemanager.resources.fluentcore.arm.ResourceId; import com.azure.resourcemanager.resources.fluentcore.model.Indexable; import com.azure.resourcemanager.resources.models.Subscription; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -115,20 +123,48 @@ public static Mono rootResource(Mono stream) * Download a file asynchronously. * * @param url the URL pointing to the file - * @param retrofit the retrofit client + * @param httpPipeline the http pipeline * @return an Observable pointing to the content of the file */ + public static Mono downloadFileAsync(String url, HttpPipeline httpPipeline) { + FileService service = RestProxy.create(FileService.class, httpPipeline); + try { + return service.download(getHost(url), getPathAndQuery(url)) + .flatMap(response -> FluxUtil.collectBytesInByteBufferStream(response.getValue())); + } catch (MalformedURLException ex) { + return Mono.error(() -> ex); + } + } + /** - * Download a file asynchronously. + * Get host from url. * - * @param url the URL pointing to the file - * @param retrofit the retrofit client - * @return an Observable pointing to the content of the file + * @param urlString the url string + * @return the host + * @throws MalformedURLException when url is invalid format */ - public static Mono downloadFileAsync(String url, HttpPipeline retrofit) { - FileService service = RestProxy.create(FileService.class, retrofit); - Mono response = service.download(url); - return response.flatMap(httpResponse -> httpResponse.getBodyAsByteArray()); + public static String getHost(String urlString) throws MalformedURLException { + URL url = new URL(urlString); + String protocol = url.getProtocol(); + String host = url.getAuthority(); + return protocol + "://" + host; + } + + /** + * Get path from url. + * + * @param urlString the url string + * @return the path + * @throws MalformedURLException when the url is invalid format + */ + public static String getPathAndQuery(String urlString) throws MalformedURLException { + URL url = new URL(urlString); + String path = url.getPath(); + String query = url.getQuery(); + if (query != null && !query.isEmpty()) { + path = path + "?" + query; + } + return path; } /** @@ -185,9 +221,12 @@ public static String resourceGroupId(String id) { /** * A Retrofit service used to download a file. */ + @Host("{$host}") + @ServiceInterface(name = "FileService") private interface FileService { - @Get("{url}") - Mono download(@PathParam("url") String url); + @Get("{path}") + Mono>> download( + @HostParam("$host") String host, @PathParam(value = "path", encoded = true) String path); } /** diff --git a/sdk/resources/mgmt/src/test/java/com/azure/resourcemanager/resources/ResourceUtilsTests.java b/sdk/resources/mgmt/src/test/java/com/azure/resourcemanager/resources/ResourceUtilsTests.java index 677803cd1233..651898b9150d 100644 --- a/sdk/resources/mgmt/src/test/java/com/azure/resourcemanager/resources/ResourceUtilsTests.java +++ b/sdk/resources/mgmt/src/test/java/com/azure/resourcemanager/resources/ResourceUtilsTests.java @@ -3,12 +3,20 @@ package com.azure.resourcemanager.resources; +import com.azure.core.http.HttpPipeline; +import com.azure.core.http.HttpPipelineBuilder; +import com.azure.core.http.policy.HttpLogDetailLevel; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.http.policy.HttpLoggingPolicy; +import com.azure.core.http.policy.RetryPolicy; import com.azure.core.management.AzureEnvironment; import com.azure.resourcemanager.resources.fluentcore.arm.ResourceUtils; import com.azure.resourcemanager.resources.fluentcore.utils.Utils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.time.temporal.ChronoUnit; + public class ResourceUtilsTests { @Test public void canExtractGroupFromId() throws Exception { @@ -39,11 +47,16 @@ public void canExtractRelativePathFromId() throws Exception { @Test public void canDownloadFile() throws Exception { - // TODO(not known): Fix this -// RestProxy retrofit = new RestProxyBuilder().baseUrl("http://microsoft.com").addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build(); -// byte[] content = Utils.downloadFileAsync("http://google.com/humans.txt", retrofit).toBlocking().single(); -// String contentString = new String(content); -// Assertions.assertNotNull(contentString); + HttpPipeline httpPipeline = new HttpPipelineBuilder() + .policies( + new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)), + new RetryPolicy("Retry-After", ChronoUnit.SECONDS) + ) + .build(); + byte[] content = Utils.downloadFileAsync("https://www.google.com/humans.txt", httpPipeline).block(); + String contentString = new String(content); + Assertions.assertNotNull(contentString); + Assertions.assertTrue(contentString.startsWith("Google is built by a large team of engineers,")); } @Test