Commit 156d828f authored by Greg Messner's avatar Greg Messner
Browse files

Simplified uploading avatar for project (#284).

parent 6eb8bd73
...@@ -442,14 +442,13 @@ public abstract class AbstractApi implements Constants { ...@@ -442,14 +442,13 @@ public abstract class AbstractApi implements Constants {
* @param expectedStatus the HTTP status that should be returned from the server * @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 name the name for the form field that contains the file name
* @param fileToUpload a File instance pointing to the file to upload * @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 * @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint * @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
protected Response putUpload(Response.Status expectedStatus, String name, File fileToUpload, String mediaType, Object... pathArgs) throws GitLabApiException { protected Response putUpload(Response.Status expectedStatus, String name, File fileToUpload, Object... pathArgs) throws GitLabApiException {
try { try {
return validate(getApiClient().putUpload(name, fileToUpload, mediaType, pathArgs), expectedStatus); return validate(getApiClient().putUpload(name, fileToUpload, pathArgs), expectedStatus);
} catch (Exception e) { } catch (Exception e) {
throw handle(e); throw handle(e);
} }
...@@ -462,14 +461,13 @@ public abstract class AbstractApi implements Constants { ...@@ -462,14 +461,13 @@ public abstract class AbstractApi implements Constants {
* @param expectedStatus the HTTP status that should be returned from the server * @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 name the name for the form field that contains the file name
* @param fileToUpload a File instance pointing to the file to upload * @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 * @param url the fully formed path to the GitLab API endpoint
* @return a ClientResponse instance with the data returned from the endpoint * @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
protected Response putUpload(Response.Status expectedStatus, String name, File fileToUpload, String mediaType, URL url) throws GitLabApiException { protected Response putUpload(Response.Status expectedStatus, String name, File fileToUpload, URL url) throws GitLabApiException {
try { try {
return validate(getApiClient().putUpload(name, fileToUpload, mediaType, url), expectedStatus); return validate(getApiClient().putUpload(name, fileToUpload, url), expectedStatus);
} catch (Exception e) { } catch (Exception e) {
throw handle(e); throw handle(e);
} }
......
...@@ -567,14 +567,13 @@ public class GitLabApiClient { ...@@ -567,14 +567,13 @@ public class GitLabApiClient {
* *
* @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
* @param fileToUpload a File instance pointing to the file to upload * @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 * @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint * @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL * @throws IOException if an error occurs while constructing the URL
*/ */
protected Response putUpload(String name, File fileToUpload, String mediaTypeString, Object... pathArgs) throws IOException { protected Response putUpload(String name, File fileToUpload, Object... pathArgs) throws IOException {
URL url = getApiUrl(pathArgs); URL url = getApiUrl(pathArgs);
return (putUpload(name, fileToUpload, mediaTypeString, url)); return (putUpload(name, fileToUpload, url));
} }
/** /**
...@@ -583,21 +582,15 @@ public class GitLabApiClient { ...@@ -583,21 +582,15 @@ public class GitLabApiClient {
* *
* @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
* @param fileToUpload a File instance pointing to the file to upload * @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 * @param url the fully formed path to the GitLab API endpoint
* @return a ClientResponse instance with the data returned from the endpoint * @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL * @throws IOException if an error occurs while constructing the URL
*/ */
protected Response putUpload(String name, File fileToUpload, String mediaTypeString, URL url) throws IOException { protected Response putUpload(String name, File fileToUpload, URL url) throws IOException {
final FormDataMultiPart multiPart = new FormDataMultiPart();
MediaType mediaType = (mediaTypeString != null ? MediaType.valueOf(mediaTypeString) : null); multiPart.bodyPart(new FileDataBodyPart(name, fileToUpload, MediaType.APPLICATION_OCTET_STREAM_TYPE));
try (MultiPart multiPart = new FormDataMultiPart()) { return (invocation(url, null).put(Entity.entity(multiPart, MULTIPART_FORM_DATA_TYPE)));
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)));
}
} }
/** /**
......
...@@ -2388,20 +2388,6 @@ public class ProjectApi extends AbstractApi implements Constants { ...@@ -2388,20 +2388,6 @@ public class ProjectApi extends AbstractApi implements Constants {
return (response.readEntity(Project.class)); return (response.readEntity(Project.class));
} }
/**
* Uploads and sets the project avatar for the specified project
*
* <pre><code>PUT /projects/:id/uploads</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param avatarFile the File instance of the avatar file to upload
* @return the updated Project instance
* @throws GitLabApiException if any exception occurs
*/
public Project setProjectAvatar(Object projectIdOrPath, File avatarFile) throws GitLabApiException {
return (setProjectAvatar(projectIdOrPath, avatarFile, null));
}
/** /**
* Uploads and sets the project avatar for the specified project. * Uploads and sets the project avatar for the specified project.
* *
...@@ -2413,8 +2399,8 @@ public class ProjectApi extends AbstractApi implements Constants { ...@@ -2413,8 +2399,8 @@ public class ProjectApi extends AbstractApi implements Constants {
* @return the updated Project instance * @return the updated Project instance
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
*/ */
public Project setProjectAvatar(Object projectIdOrPath, File avatarFile, String mediaType) throws GitLabApiException { public Project setProjectAvatar(Object projectIdOrPath, File avatarFile) throws GitLabApiException {
Response response = putUpload(Response.Status.OK, "avatar", avatarFile, mediaType, "projects", getProjectIdOrPath(projectIdOrPath)); Response response = putUpload(Response.Status.OK, "avatar", avatarFile, "projects", getProjectIdOrPath(projectIdOrPath));
return (response.readEntity(Project.class)); return (response.readEntity(Project.class));
} }
} }
\ No newline at end of file
...@@ -84,19 +84,7 @@ public class TestAvatarUpload { ...@@ -84,19 +84,7 @@ public class TestAvatarUpload {
assertNotNull(project); assertNotNull(project);
File avatarFile = new File("src/test/resources/org/gitlab4j/api/avatar.png"); File avatarFile = new File("src/test/resources/org/gitlab4j/api/avatar.png");
Project updatedProject = gitLabApi.getProjectApi().setProjectAvatar(project.getId(), avatarFile, null); Project updatedProject = gitLabApi.getProjectApi().setProjectAvatar(project.getId(), avatarFile);
assertNotNull(updatedProject);
assertTrue(updatedProject.getAvatarUrl().endsWith("avatar.png"));
}
@Test
public void testSetProjectAvatarWithMediaType() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
File avatarFile = new File("src/test/resources/org/gitlab4j/api/avatar.png");
Project updatedProject = gitLabApi.getProjectApi().setProjectAvatar(project.getId(), avatarFile, "image/png");
assertNotNull(updatedProject); assertNotNull(updatedProject);
assertTrue(updatedProject.getAvatarUrl().endsWith("avatar.png")); assertTrue(updatedProject.getAvatarUrl().endsWith("avatar.png"));
} }
......
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