|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +package io.clientcore.annotation.processor.test; |
| 4 | + |
| 5 | +import io.clientcore.core.http.models.HttpHeaderName; |
| 6 | +import io.clientcore.core.http.models.HttpMethod; |
| 7 | +import io.clientcore.core.http.models.HttpRequest; |
| 8 | +import io.clientcore.core.http.models.Response; |
| 9 | +import io.clientcore.core.http.pipeline.HttpPipeline; |
| 10 | +import io.clientcore.core.implementation.utils.UriEscapers; |
| 11 | +import io.clientcore.core.models.binarydata.BinaryData; |
| 12 | +import java.io.InputStream; |
| 13 | +import io.clientcore.annotation.processor.test.implementation.SpecialReturnBodiesService; |
| 14 | +import io.clientcore.core.instrumentation.logging.ClientLogger; |
| 15 | +import io.clientcore.core.serialization.json.JsonSerializer; |
| 16 | +import io.clientcore.core.serialization.xml.XmlSerializer; |
| 17 | + |
| 18 | +/** |
| 19 | + * Initializes a new instance of the SpecialReturnBodiesServiceImpl type. |
| 20 | + */ |
| 21 | +public class SpecialReturnBodiesServiceImpl implements SpecialReturnBodiesService { |
| 22 | + |
| 23 | + private static final ClientLogger LOGGER = new ClientLogger(SpecialReturnBodiesService.class); |
| 24 | + |
| 25 | + private final HttpPipeline httpPipeline; |
| 26 | + |
| 27 | + private final JsonSerializer jsonSerializer; |
| 28 | + |
| 29 | + private final XmlSerializer xmlSerializer; |
| 30 | + |
| 31 | + private SpecialReturnBodiesServiceImpl(HttpPipeline httpPipeline) { |
| 32 | + this.httpPipeline = httpPipeline; |
| 33 | + this.jsonSerializer = JsonSerializer.getInstance(); |
| 34 | + this.xmlSerializer = XmlSerializer.getInstance(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates an instance of SpecialReturnBodiesService that is capable of sending requests to the service. |
| 39 | + * @param httpPipeline The HTTP pipeline to use for sending requests. |
| 40 | + * @return An instance of `SpecialReturnBodiesService`; |
| 41 | + */ |
| 42 | + public static SpecialReturnBodiesService getNewInstance(HttpPipeline httpPipeline) { |
| 43 | + return new SpecialReturnBodiesServiceImpl(httpPipeline); |
| 44 | + } |
| 45 | + |
| 46 | + @SuppressWarnings("cast") |
| 47 | + @Override |
| 48 | + public BinaryData getBinaryData(String url) { |
| 49 | + String uri = url + "/bytes"; |
| 50 | + // Create the HTTP request |
| 51 | + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uri); |
| 52 | + // Send the request through the httpPipeline |
| 53 | + Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest); |
| 54 | + int responseCode = networkResponse.getStatusCode(); |
| 55 | + boolean expectedResponse = responseCode == 200; |
| 56 | + if (!expectedResponse) { |
| 57 | + throw new RuntimeException("Unexpected response code: " + responseCode); |
| 58 | + } |
| 59 | + return networkResponse.getValue(); |
| 60 | + } |
| 61 | + |
| 62 | + @SuppressWarnings("cast") |
| 63 | + @Override |
| 64 | + public Response<BinaryData> getBinaryDataWithResponse(String url) { |
| 65 | + String uri = url + "/bytes"; |
| 66 | + // Create the HTTP request |
| 67 | + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uri); |
| 68 | + // Send the request through the httpPipeline |
| 69 | + Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest); |
| 70 | + int responseCode = networkResponse.getStatusCode(); |
| 71 | + boolean expectedResponse = responseCode == 200; |
| 72 | + if (!expectedResponse) { |
| 73 | + throw new RuntimeException("Unexpected response code: " + responseCode); |
| 74 | + } |
| 75 | + return networkResponse; |
| 76 | + } |
| 77 | + |
| 78 | + @SuppressWarnings("cast") |
| 79 | + @Override |
| 80 | + public byte[] getByteArray(String url) { |
| 81 | + String uri = url + "/bytes"; |
| 82 | + // Create the HTTP request |
| 83 | + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uri); |
| 84 | + // Send the request through the httpPipeline |
| 85 | + Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest); |
| 86 | + int responseCode = networkResponse.getStatusCode(); |
| 87 | + boolean expectedResponse = responseCode == 200; |
| 88 | + if (!expectedResponse) { |
| 89 | + throw new RuntimeException("Unexpected response code: " + responseCode); |
| 90 | + } |
| 91 | + BinaryData responseBody = networkResponse.getValue(); |
| 92 | + byte[] responseBodyBytes = responseBody != null ? responseBody.toBytes() : null; |
| 93 | + // Close the network response as the body should be consumed. |
| 94 | + networkResponse.close(); |
| 95 | + return responseBodyBytes; |
| 96 | + } |
| 97 | + |
| 98 | + @SuppressWarnings("cast") |
| 99 | + @Override |
| 100 | + public Response<byte[]> getByteArrayWithResponse(String url) { |
| 101 | + String uri = url + "/bytes"; |
| 102 | + // Create the HTTP request |
| 103 | + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uri); |
| 104 | + // Send the request through the httpPipeline |
| 105 | + Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest); |
| 106 | + int responseCode = networkResponse.getStatusCode(); |
| 107 | + boolean expectedResponse = responseCode == 200; |
| 108 | + if (!expectedResponse) { |
| 109 | + throw new RuntimeException("Unexpected response code: " + responseCode); |
| 110 | + } |
| 111 | + BinaryData responseBody = networkResponse.getValue(); |
| 112 | + byte[] responseBodyBytes = responseBody != null ? responseBody.toBytes() : null; |
| 113 | + // Close the network response as the body should be consumed. |
| 114 | + networkResponse.close(); |
| 115 | + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), responseBodyBytes); |
| 116 | + } |
| 117 | + |
| 118 | + @SuppressWarnings("cast") |
| 119 | + @Override |
| 120 | + public InputStream getInputStream(String url) { |
| 121 | + String uri = url + "/bytes"; |
| 122 | + // Create the HTTP request |
| 123 | + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uri); |
| 124 | + // Send the request through the httpPipeline |
| 125 | + Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest); |
| 126 | + int responseCode = networkResponse.getStatusCode(); |
| 127 | + boolean expectedResponse = responseCode == 200; |
| 128 | + if (!expectedResponse) { |
| 129 | + throw new RuntimeException("Unexpected response code: " + responseCode); |
| 130 | + } |
| 131 | + return networkResponse.getValue().toStream(); |
| 132 | + } |
| 133 | + |
| 134 | + @SuppressWarnings("cast") |
| 135 | + @Override |
| 136 | + public Response<InputStream> getInputStreamWithResponse(String url) { |
| 137 | + String uri = url + "/bytes"; |
| 138 | + // Create the HTTP request |
| 139 | + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uri); |
| 140 | + // Send the request through the httpPipeline |
| 141 | + Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest); |
| 142 | + int responseCode = networkResponse.getStatusCode(); |
| 143 | + boolean expectedResponse = responseCode == 200; |
| 144 | + if (!expectedResponse) { |
| 145 | + throw new RuntimeException("Unexpected response code: " + responseCode); |
| 146 | + } |
| 147 | + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), networkResponse.getValue().toStream()); |
| 148 | + } |
| 149 | +} |
0 commit comments