Search in sources :

Example 1 with ModelApiResponse

use of com.baeldung.petstore.client.model.ModelApiResponse in project tutorials by eugenp.

the class PetApi method uploadFile.

/**
 * uploads an image
 *
 * <p><b>200</b> - successful operation
 * @param petId ID of pet to update
 * @param additionalMetadata Additional data to pass to server
 * @param file file to upload
 * @return ModelApiResponse
 * @throws RestClientException if an error occurs while attempting to invoke the API
 */
public ModelApiResponse uploadFile(Long petId, String additionalMetadata, File file) throws RestClientException {
    Object postBody = null;
    // verify the required parameter 'petId' is set
    if (petId == null) {
        throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'petId' when calling uploadFile");
    }
    // create path and map variables
    final Map<String, Object> uriVariables = new HashMap<String, Object>();
    uriVariables.put("petId", petId);
    String path = UriComponentsBuilder.fromPath("/pet/{petId}/uploadImage").buildAndExpand(uriVariables).toUriString();
    final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
    final HttpHeaders headerParams = new HttpHeaders();
    final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();
    if (additionalMetadata != null)
        formParams.add("additionalMetadata", additionalMetadata);
    if (file != null)
        formParams.add("file", new FileSystemResource(file));
    final String[] accepts = { "application/json" };
    final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
    final String[] contentTypes = { "multipart/form-data" };
    final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);
    String[] authNames = new String[] { "petstore_auth" };
    ParameterizedTypeReference<ModelApiResponse> returnType = new ParameterizedTypeReference<ModelApiResponse>() {
    };
    return apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HashMap(java.util.HashMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) FileSystemResource(org.springframework.core.io.FileSystemResource) ModelApiResponse(com.baeldung.petstore.client.model.ModelApiResponse) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) MediaType(org.springframework.http.MediaType)

Example 2 with ModelApiResponse

use of com.baeldung.petstore.client.model.ModelApiResponse in project tutorials by eugenp.

the class PetApiTest method uploadFileTest.

/**
 * uploads an image
 *
 * @throws ApiException
 *          if the Api call fails
 */
@Test
public void uploadFileTest() {
    Long petId = null;
    String additionalMetadata = null;
    File file = null;
    ModelApiResponse response = api.uploadFile(petId, additionalMetadata, file);
// TODO: test validations
}
Also used : ModelApiResponse(com.baeldung.petstore.client.model.ModelApiResponse) File(java.io.File) Test(org.junit.Test)

Aggregations

ModelApiResponse (com.baeldung.petstore.client.model.ModelApiResponse)2 File (java.io.File)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)1 FileSystemResource (org.springframework.core.io.FileSystemResource)1 HttpHeaders (org.springframework.http.HttpHeaders)1 MediaType (org.springframework.http.MediaType)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)1