001 002package com.commercetools.ml.client; 003 004import java.net.URI; 005import java.net.URLConnection; 006import java.nio.file.Files; 007import java.time.Duration; 008import java.util.ArrayList; 009import java.util.Collection; 010import java.util.List; 011import java.util.Optional; 012import java.util.concurrent.CompletableFuture; 013import java.util.function.Function; 014import java.util.function.Supplier; 015import java.util.stream.Collectors; 016 017import io.vrap.rmf.base.client.*; 018import io.vrap.rmf.base.client.utils.Generated; 019 020import org.apache.commons.lang3.builder.EqualsBuilder; 021import org.apache.commons.lang3.builder.HashCodeBuilder; 022 023/** 024 * <p>Accepts an image file and returns similar products from product catalogue.</p> 025 * 026 * <hr> 027 * <div class=code-example> 028 * <pre><code class='java'>{@code 029 * CompletableFuture<ApiHttpResponse<com.commercetools.ml.models.image_search.ImageSearchResponse>> result = apiRoot 030 * .withProjectKey("{projectKey}") 031 * .imageSearch() 032 * .post(file) 033 * .execute() 034 * }</code></pre> 035 * </div> 036 */ 037@Generated(value = "io.vrap.rmf.codegen.rendering.CoreCodeGenerator", comments = "https://github.com/commercetools/rmf-codegen") 038public class ByProjectKeyImageSearchPost extends 039 BodyApiMethod<ByProjectKeyImageSearchPost, com.commercetools.ml.models.image_search.ImageSearchResponse, java.io.File> { 040 041 private String projectKey; 042 043 private java.io.File file; 044 045 public ByProjectKeyImageSearchPost(final ApiHttpClient apiHttpClient, String projectKey, java.io.File file) { 046 super(apiHttpClient); 047 this.projectKey = projectKey; 048 this.file = file; 049 } 050 051 public ByProjectKeyImageSearchPost(ByProjectKeyImageSearchPost t) { 052 super(t); 053 this.projectKey = t.projectKey; 054 this.file = t.file; 055 } 056 057 @Override 058 protected ApiHttpRequest buildHttpRequest() { 059 List<String> params = new ArrayList<>(getQueryParamUriStrings()); 060 String httpRequestPath = String.format("%s/image-search", this.projectKey); 061 if (!params.isEmpty()) { 062 httpRequestPath += "?" + String.join("&", params); 063 } 064 ApiHttpHeaders headers = getHeaders(); 065 if (headers.getFirst(ApiHttpHeaders.CONTENT_TYPE) == null) { 066 final String mimeType = Optional.ofNullable(URLConnection.guessContentTypeFromName(file.getName())) 067 .orElse("application/octet-stream"); 068 headers = headers.withHeader(ApiHttpHeaders.CONTENT_TYPE, mimeType); 069 } 070 return new ApiHttpRequest(ApiHttpMethod.POST, URI.create(httpRequestPath), headers, 071 io.vrap.rmf.base.client.utils.FileUtils.executing(() -> Files.readAllBytes(file.toPath()))); 072 073 } 074 075 @Override 076 public ApiHttpResponse<com.commercetools.ml.models.image_search.ImageSearchResponse> executeBlocking( 077 final ApiHttpClient client, final Duration timeout) { 078 return executeBlocking(client, timeout, com.commercetools.ml.models.image_search.ImageSearchResponse.class); 079 } 080 081 @Override 082 public CompletableFuture<ApiHttpResponse<com.commercetools.ml.models.image_search.ImageSearchResponse>> execute( 083 final ApiHttpClient client) { 084 return execute(client, com.commercetools.ml.models.image_search.ImageSearchResponse.class); 085 } 086 087 public String getProjectKey() { 088 return this.projectKey; 089 } 090 091 public List<String> getLimit() { 092 return this.getQueryParam("limit"); 093 } 094 095 public List<String> getOffset() { 096 return this.getQueryParam("offset"); 097 } 098 099 public void setProjectKey(final String projectKey) { 100 this.projectKey = projectKey; 101 } 102 103 /** 104 * set limit with the specified value 105 * @param limit value to be set 106 * @param <TValue> value type 107 * @return ByProjectKeyImageSearchPost 108 */ 109 public <TValue> ByProjectKeyImageSearchPost withLimit(final TValue limit) { 110 return copy().withQueryParam("limit", limit); 111 } 112 113 /** 114 * add additional limit query parameter 115 * @param limit value to be added 116 * @param <TValue> value type 117 * @return ByProjectKeyImageSearchPost 118 */ 119 public <TValue> ByProjectKeyImageSearchPost addLimit(final TValue limit) { 120 return copy().addQueryParam("limit", limit); 121 } 122 123 /** 124 * set limit with the specified value 125 * @param supplier supplier for the value to be set 126 * @return ByProjectKeyImageSearchPost 127 */ 128 public ByProjectKeyImageSearchPost withLimit(final Supplier<Integer> supplier) { 129 return copy().withQueryParam("limit", supplier.get()); 130 } 131 132 /** 133 * add additional limit query parameter 134 * @param supplier supplier for the value to be added 135 * @return ByProjectKeyImageSearchPost 136 */ 137 public ByProjectKeyImageSearchPost addLimit(final Supplier<Integer> supplier) { 138 return copy().addQueryParam("limit", supplier.get()); 139 } 140 141 /** 142 * set limit with the specified value 143 * @param op builder for the value to be set 144 * @return ByProjectKeyImageSearchPost 145 */ 146 public ByProjectKeyImageSearchPost withLimit(final Function<StringBuilder, StringBuilder> op) { 147 return copy().withQueryParam("limit", op.apply(new StringBuilder())); 148 } 149 150 /** 151 * add additional limit query parameter 152 * @param op builder for the value to be added 153 * @return ByProjectKeyImageSearchPost 154 */ 155 public ByProjectKeyImageSearchPost addLimit(final Function<StringBuilder, StringBuilder> op) { 156 return copy().addQueryParam("limit", op.apply(new StringBuilder())); 157 } 158 159 /** 160 * set limit with the specified values 161 * @param limit values to be set 162 * @param <TValue> value type 163 * @return ByProjectKeyImageSearchPost 164 */ 165 public <TValue> ByProjectKeyImageSearchPost withLimit(final Collection<TValue> limit) { 166 return copy().withoutQueryParam("limit") 167 .addQueryParams( 168 limit.stream().map(s -> new ParamEntry<>("limit", s.toString())).collect(Collectors.toList())); 169 } 170 171 /** 172 * add additional limit query parameters 173 * @param limit values to be added 174 * @param <TValue> value type 175 * @return ByProjectKeyImageSearchPost 176 */ 177 public <TValue> ByProjectKeyImageSearchPost addLimit(final Collection<TValue> limit) { 178 return copy().addQueryParams( 179 limit.stream().map(s -> new ParamEntry<>("limit", s.toString())).collect(Collectors.toList())); 180 } 181 182 /** 183 * set offset with the specified value 184 * @param offset value to be set 185 * @param <TValue> value type 186 * @return ByProjectKeyImageSearchPost 187 */ 188 public <TValue> ByProjectKeyImageSearchPost withOffset(final TValue offset) { 189 return copy().withQueryParam("offset", offset); 190 } 191 192 /** 193 * add additional offset query parameter 194 * @param offset value to be added 195 * @param <TValue> value type 196 * @return ByProjectKeyImageSearchPost 197 */ 198 public <TValue> ByProjectKeyImageSearchPost addOffset(final TValue offset) { 199 return copy().addQueryParam("offset", offset); 200 } 201 202 /** 203 * set offset with the specified value 204 * @param supplier supplier for the value to be set 205 * @return ByProjectKeyImageSearchPost 206 */ 207 public ByProjectKeyImageSearchPost withOffset(final Supplier<Integer> supplier) { 208 return copy().withQueryParam("offset", supplier.get()); 209 } 210 211 /** 212 * add additional offset query parameter 213 * @param supplier supplier for the value to be added 214 * @return ByProjectKeyImageSearchPost 215 */ 216 public ByProjectKeyImageSearchPost addOffset(final Supplier<Integer> supplier) { 217 return copy().addQueryParam("offset", supplier.get()); 218 } 219 220 /** 221 * set offset with the specified value 222 * @param op builder for the value to be set 223 * @return ByProjectKeyImageSearchPost 224 */ 225 public ByProjectKeyImageSearchPost withOffset(final Function<StringBuilder, StringBuilder> op) { 226 return copy().withQueryParam("offset", op.apply(new StringBuilder())); 227 } 228 229 /** 230 * add additional offset query parameter 231 * @param op builder for the value to be added 232 * @return ByProjectKeyImageSearchPost 233 */ 234 public ByProjectKeyImageSearchPost addOffset(final Function<StringBuilder, StringBuilder> op) { 235 return copy().addQueryParam("offset", op.apply(new StringBuilder())); 236 } 237 238 /** 239 * set offset with the specified values 240 * @param offset values to be set 241 * @param <TValue> value type 242 * @return ByProjectKeyImageSearchPost 243 */ 244 public <TValue> ByProjectKeyImageSearchPost withOffset(final Collection<TValue> offset) { 245 return copy().withoutQueryParam("offset") 246 .addQueryParams( 247 offset.stream().map(s -> new ParamEntry<>("offset", s.toString())).collect(Collectors.toList())); 248 } 249 250 /** 251 * add additional offset query parameters 252 * @param offset values to be added 253 * @param <TValue> value type 254 * @return ByProjectKeyImageSearchPost 255 */ 256 public <TValue> ByProjectKeyImageSearchPost addOffset(final Collection<TValue> offset) { 257 return copy().addQueryParams( 258 offset.stream().map(s -> new ParamEntry<>("offset", s.toString())).collect(Collectors.toList())); 259 } 260 261 public java.io.File getBody() { 262 return file; 263 } 264 265 public ByProjectKeyImageSearchPost withBody(java.io.File file) { 266 ByProjectKeyImageSearchPost t = copy(); 267 t.file = file; 268 return t; 269 } 270 271 @Override 272 public boolean equals(Object o) { 273 if (this == o) 274 return true; 275 276 if (o == null || getClass() != o.getClass()) 277 return false; 278 279 ByProjectKeyImageSearchPost that = (ByProjectKeyImageSearchPost) o; 280 281 return new EqualsBuilder().append(projectKey, that.projectKey).append(file, that.file).isEquals(); 282 } 283 284 @Override 285 public int hashCode() { 286 return new HashCodeBuilder(17, 37).append(projectKey).append(file).toHashCode(); 287 } 288 289 @Override 290 protected ByProjectKeyImageSearchPost copy() { 291 return new ByProjectKeyImageSearchPost(this); 292 } 293}