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 */ 106 public <TValue> ByProjectKeyImageSearchPost withLimit(final TValue limit) { 107 return copy().withQueryParam("limit", limit); 108 } 109 110 /** 111 * add additional limit query parameter 112 */ 113 public <TValue> ByProjectKeyImageSearchPost addLimit(final TValue limit) { 114 return copy().addQueryParam("limit", limit); 115 } 116 117 /** 118 * set limit with the specified value 119 */ 120 public ByProjectKeyImageSearchPost withLimit(final Supplier<Integer> supplier) { 121 return copy().withQueryParam("limit", supplier.get()); 122 } 123 124 /** 125 * add additional limit query parameter 126 */ 127 public ByProjectKeyImageSearchPost addLimit(final Supplier<Integer> supplier) { 128 return copy().addQueryParam("limit", supplier.get()); 129 } 130 131 /** 132 * set limit with the specified value 133 */ 134 public ByProjectKeyImageSearchPost withLimit(final Function<StringBuilder, StringBuilder> op) { 135 return copy().withQueryParam("limit", op.apply(new StringBuilder())); 136 } 137 138 /** 139 * add additional limit query parameter 140 */ 141 public ByProjectKeyImageSearchPost addLimit(final Function<StringBuilder, StringBuilder> op) { 142 return copy().addQueryParam("limit", op.apply(new StringBuilder())); 143 } 144 145 /** 146 * set limit with the specified values 147 */ 148 public <TValue> ByProjectKeyImageSearchPost withLimit(final Collection<TValue> limit) { 149 return copy().withoutQueryParam("limit") 150 .addQueryParams( 151 limit.stream().map(s -> new ParamEntry<>("limit", s.toString())).collect(Collectors.toList())); 152 } 153 154 /** 155 * add additional limit query parameters 156 */ 157 public <TValue> ByProjectKeyImageSearchPost addLimit(final Collection<TValue> limit) { 158 return copy().addQueryParams( 159 limit.stream().map(s -> new ParamEntry<>("limit", s.toString())).collect(Collectors.toList())); 160 } 161 162 /** 163 * set offset with the specified value 164 */ 165 public <TValue> ByProjectKeyImageSearchPost withOffset(final TValue offset) { 166 return copy().withQueryParam("offset", offset); 167 } 168 169 /** 170 * add additional offset query parameter 171 */ 172 public <TValue> ByProjectKeyImageSearchPost addOffset(final TValue offset) { 173 return copy().addQueryParam("offset", offset); 174 } 175 176 /** 177 * set offset with the specified value 178 */ 179 public ByProjectKeyImageSearchPost withOffset(final Supplier<Integer> supplier) { 180 return copy().withQueryParam("offset", supplier.get()); 181 } 182 183 /** 184 * add additional offset query parameter 185 */ 186 public ByProjectKeyImageSearchPost addOffset(final Supplier<Integer> supplier) { 187 return copy().addQueryParam("offset", supplier.get()); 188 } 189 190 /** 191 * set offset with the specified value 192 */ 193 public ByProjectKeyImageSearchPost withOffset(final Function<StringBuilder, StringBuilder> op) { 194 return copy().withQueryParam("offset", op.apply(new StringBuilder())); 195 } 196 197 /** 198 * add additional offset query parameter 199 */ 200 public ByProjectKeyImageSearchPost addOffset(final Function<StringBuilder, StringBuilder> op) { 201 return copy().addQueryParam("offset", op.apply(new StringBuilder())); 202 } 203 204 /** 205 * set offset with the specified values 206 */ 207 public <TValue> ByProjectKeyImageSearchPost withOffset(final Collection<TValue> offset) { 208 return copy().withoutQueryParam("offset") 209 .addQueryParams( 210 offset.stream().map(s -> new ParamEntry<>("offset", s.toString())).collect(Collectors.toList())); 211 } 212 213 /** 214 * add additional offset query parameters 215 */ 216 public <TValue> ByProjectKeyImageSearchPost addOffset(final Collection<TValue> offset) { 217 return copy().addQueryParams( 218 offset.stream().map(s -> new ParamEntry<>("offset", s.toString())).collect(Collectors.toList())); 219 } 220 221 public java.io.File getBody() { 222 return file; 223 } 224 225 public ByProjectKeyImageSearchPost withBody(java.io.File file) { 226 ByProjectKeyImageSearchPost t = copy(); 227 t.file = file; 228 return t; 229 } 230 231 @Override 232 public boolean equals(Object o) { 233 if (this == o) 234 return true; 235 236 if (o == null || getClass() != o.getClass()) 237 return false; 238 239 ByProjectKeyImageSearchPost that = (ByProjectKeyImageSearchPost) o; 240 241 return new EqualsBuilder().append(projectKey, that.projectKey).append(file, that.file).isEquals(); 242 } 243 244 @Override 245 public int hashCode() { 246 return new HashCodeBuilder(17, 37).append(projectKey).append(file).toHashCode(); 247 } 248 249 @Override 250 protected ByProjectKeyImageSearchPost copy() { 251 return new ByProjectKeyImageSearchPost(this); 252 } 253}