Commit 0e14ac67 authored by Greg Messner's avatar Greg Messner
Browse files

Added putUpload() methods (#284).

parent a39ae6d0
...@@ -434,6 +434,47 @@ public abstract class AbstractApi implements Constants { ...@@ -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 * Perform an HTTP DELETE call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint. * a ClientResponse instance with the data returned from the endpoint.
......
...@@ -523,7 +523,7 @@ public class GitLabApiClient { ...@@ -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. * a ClientResponse instance with the data returned from the endpoint.
* *
* @param name the name for the form field that contains the file name * @param name the name for the form field that contains the file name
...@@ -561,6 +561,45 @@ public class GitLabApiClient { ...@@ -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 * Perform an HTTP PUT call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint. * a ClientResponse instance with the data returned from the endpoint.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment