Skip to content

Commit 0e14ac6

Browse files
committed
Added putUpload() methods (#284).
1 parent a39ae6d commit 0e14ac6

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

src/main/java/org/gitlab4j/api/AbstractApi.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,47 @@ protected Response putWithFormData(Response.Status expectedStatus, Form formData
434434
}
435435
}
436436

437+
438+
/**
439+
* Perform a file upload using the HTTP PUT method with the specified File instance and path objects,
440+
* returning a ClientResponse instance with the data returned from the endpoint.
441+
*
442+
* @param expectedStatus the HTTP status that should be returned from the server
443+
* @param name the name for the form field that contains the file name
444+
* @param fileToUpload a File instance pointing to the file to upload
445+
* @param mediaType the content-type of the uploaded file, if null will be determined from fileToUpload
446+
* @param pathArgs variable list of arguments used to build the URI
447+
* @return a ClientResponse instance with the data returned from the endpoint
448+
* @throws GitLabApiException if any exception occurs during execution
449+
*/
450+
protected Response putUpload(Response.Status expectedStatus, String name, File fileToUpload, String mediaType, Object... pathArgs) throws GitLabApiException {
451+
try {
452+
return validate(getApiClient().putUpload(name, fileToUpload, mediaType, pathArgs), expectedStatus);
453+
} catch (Exception e) {
454+
throw handle(e);
455+
}
456+
}
457+
458+
/**
459+
* Perform a file upload using the HTTP PUT method with the specified File instance and path objects,
460+
* returning a ClientResponse instance with the data returned from the endpoint.
461+
*
462+
* @param expectedStatus the HTTP status that should be returned from the server
463+
* @param name the name for the form field that contains the file name
464+
* @param fileToUpload a File instance pointing to the file to upload
465+
* @param mediaType the content-type of the uploaded file, if null will be determined from fileToUpload
466+
* @param url the fully formed path to the GitLab API endpoint
467+
* @return a ClientResponse instance with the data returned from the endpoint
468+
* @throws GitLabApiException if any exception occurs during execution
469+
*/
470+
protected Response putUpload(Response.Status expectedStatus, String name, File fileToUpload, String mediaType, URL url) throws GitLabApiException {
471+
try {
472+
return validate(getApiClient().putUpload(name, fileToUpload, mediaType, url), expectedStatus);
473+
} catch (Exception e) {
474+
throw handle(e);
475+
}
476+
}
477+
437478
/**
438479
* Perform an HTTP DELETE call with the specified form data and path objects, returning
439480
* a ClientResponse instance with the data returned from the endpoint.

src/main/java/org/gitlab4j/api/GitLabApiClient.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ protected Response post(StreamingOutput stream, String mediaType, Object... path
523523
}
524524

525525
/**
526-
* Perform a file upload as part of the , returning
526+
* Perform a file upload using the specified media type, returning
527527
* a ClientResponse instance with the data returned from the endpoint.
528528
*
529529
* @param name the name for the form field that contains the file name
@@ -561,6 +561,45 @@ protected Response upload(String name, File fileToUpload, String mediaTypeString
561561
}
562562
}
563563

564+
/**
565+
* Perform a file upload using multipart/form-data using the HTTP PUT method, returning
566+
* a ClientResponse instance with the data returned from the endpoint.
567+
*
568+
* @param name the name for the form field that contains the file name
569+
* @param fileToUpload a File instance pointing to the file to upload
570+
* @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
571+
* @param pathArgs variable list of arguments used to build the URI
572+
* @return a ClientResponse instance with the data returned from the endpoint
573+
* @throws IOException if an error occurs while constructing the URL
574+
*/
575+
protected Response putUpload(String name, File fileToUpload, String mediaTypeString, Object... pathArgs) throws IOException {
576+
URL url = getApiUrl(pathArgs);
577+
return (putUpload(name, fileToUpload, mediaTypeString, url));
578+
}
579+
580+
/**
581+
* Perform a file upload using multipart/form-data using the HTTP PUT method, returning
582+
* a ClientResponse instance with the data returned from the endpoint.
583+
*
584+
* @param name the name for the form field that contains the file name
585+
* @param fileToUpload a File instance pointing to the file to upload
586+
* @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
587+
* @param url the fully formed path to the GitLab API endpoint
588+
* @return a ClientResponse instance with the data returned from the endpoint
589+
* @throws IOException if an error occurs while constructing the URL
590+
*/
591+
protected Response putUpload(String name, File fileToUpload, String mediaTypeString, URL url) throws IOException {
592+
593+
MediaType mediaType = (mediaTypeString != null ? MediaType.valueOf(mediaTypeString) : null);
594+
try (MultiPart multiPart = new FormDataMultiPart()) {
595+
FileDataBodyPart filePart = mediaType != null ?
596+
new FileDataBodyPart(name, fileToUpload, mediaType) :
597+
new FileDataBodyPart(name, fileToUpload);
598+
multiPart.bodyPart(filePart);
599+
return (invocation(url, null).put(Entity.entity(multiPart, MULTIPART_FORM_DATA_TYPE)));
600+
}
601+
}
602+
564603
/**
565604
* Perform an HTTP PUT call with the specified form data and path objects, returning
566605
* a ClientResponse instance with the data returned from the endpoint.

0 commit comments

Comments
 (0)