From 0e14ac6733a739bc49e9a30cd4ee41063806591b Mon Sep 17 00:00:00 2001 From: Greg Messner Date: Sat, 15 Dec 2018 11:27:33 -0800 Subject: [PATCH] Added putUpload() methods (#284). --- .../java/org/gitlab4j/api/AbstractApi.java | 41 +++++++++++++++++++ .../org/gitlab4j/api/GitLabApiClient.java | 41 ++++++++++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/gitlab4j/api/AbstractApi.java b/src/main/java/org/gitlab4j/api/AbstractApi.java index 15a66681..fd24b519 100644 --- a/src/main/java/org/gitlab4j/api/AbstractApi.java +++ b/src/main/java/org/gitlab4j/api/AbstractApi.java @@ -434,6 +434,47 @@ public abstract class AbstractApi implements Constants { } } + + /** + * Perform a file upload using the HTTP PUT method with the specified File instance and path objects, + * returning a ClientResponse instance with the data returned from the endpoint. + * + * @param expectedStatus the HTTP status that should be returned from the server + * @param name the name for the form field that contains the file name + * @param fileToUpload a File instance pointing to the file to upload + * @param mediaType the content-type of the uploaded file, if null will be determined from fileToUpload + * @param pathArgs variable list of arguments used to build the URI + * @return a ClientResponse instance with the data returned from the endpoint + * @throws GitLabApiException if any exception occurs during execution + */ + protected Response putUpload(Response.Status expectedStatus, String name, File fileToUpload, String mediaType, Object... pathArgs) throws GitLabApiException { + try { + return validate(getApiClient().putUpload(name, fileToUpload, mediaType, pathArgs), expectedStatus); + } catch (Exception e) { + throw handle(e); + } + } + + /** + * Perform a file upload using the HTTP PUT method with the specified File instance and path objects, + * returning a ClientResponse instance with the data returned from the endpoint. + * + * @param expectedStatus the HTTP status that should be returned from the server + * @param name the name for the form field that contains the file name + * @param fileToUpload a File instance pointing to the file to upload + * @param mediaType the content-type of the uploaded file, if null will be determined from fileToUpload + * @param url the fully formed path to the GitLab API endpoint + * @return a ClientResponse instance with the data returned from the endpoint + * @throws GitLabApiException if any exception occurs during execution + */ + protected Response putUpload(Response.Status expectedStatus, String name, File fileToUpload, String mediaType, URL url) throws GitLabApiException { + try { + return validate(getApiClient().putUpload(name, fileToUpload, mediaType, url), expectedStatus); + } catch (Exception e) { + throw handle(e); + } + } + /** * Perform an HTTP DELETE call with the specified form data and path objects, returning * a ClientResponse instance with the data returned from the endpoint. diff --git a/src/main/java/org/gitlab4j/api/GitLabApiClient.java b/src/main/java/org/gitlab4j/api/GitLabApiClient.java index 569eef57..8ad9d1da 100755 --- a/src/main/java/org/gitlab4j/api/GitLabApiClient.java +++ b/src/main/java/org/gitlab4j/api/GitLabApiClient.java @@ -523,7 +523,7 @@ public class GitLabApiClient { } /** - * Perform a file upload as part of the , returning + * Perform a file upload using the specified media type, returning * a ClientResponse instance with the data returned from the endpoint. * * @param name the name for the form field that contains the file name @@ -561,6 +561,45 @@ public class GitLabApiClient { } } + /** + * Perform a file upload using multipart/form-data using the HTTP PUT method, returning + * a ClientResponse instance with the data returned from the endpoint. + * + * @param name the name for the form field that contains the file name + * @param fileToUpload a File instance pointing to the file to upload + * @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload + * @param pathArgs variable list of arguments used to build the URI + * @return a ClientResponse instance with the data returned from the endpoint + * @throws IOException if an error occurs while constructing the URL + */ + protected Response putUpload(String name, File fileToUpload, String mediaTypeString, Object... pathArgs) throws IOException { + URL url = getApiUrl(pathArgs); + return (putUpload(name, fileToUpload, mediaTypeString, url)); + } + + /** + * Perform a file upload using multipart/form-data using the HTTP PUT method, returning + * a ClientResponse instance with the data returned from the endpoint. + * + * @param name the name for the form field that contains the file name + * @param fileToUpload a File instance pointing to the file to upload + * @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload + * @param url the fully formed path to the GitLab API endpoint + * @return a ClientResponse instance with the data returned from the endpoint + * @throws IOException if an error occurs while constructing the URL + */ + protected Response putUpload(String name, File fileToUpload, String mediaTypeString, URL url) throws IOException { + + MediaType mediaType = (mediaTypeString != null ? MediaType.valueOf(mediaTypeString) : null); + try (MultiPart multiPart = new FormDataMultiPart()) { + FileDataBodyPart filePart = mediaType != null ? + new FileDataBodyPart(name, fileToUpload, mediaType) : + new FileDataBodyPart(name, fileToUpload); + multiPart.bodyPart(filePart); + return (invocation(url, null).put(Entity.entity(multiPart, MULTIPART_FORM_DATA_TYPE))); + } + } + /** * Perform an HTTP PUT call with the specified form data and path objects, returning * a ClientResponse instance with the data returned from the endpoint. -- GitLab