Skip to content

Commit

Permalink
refactored URL building
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Adams committed Oct 6, 2016
1 parent 22b52b1 commit 8c865f2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
Binary file modified .gradle/2.13/taskArtifacts/cache.properties.lock
Binary file not shown.
Binary file modified .gradle/2.13/taskArtifacts/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/2.13/taskArtifacts/fileSnapshots.bin
Binary file not shown.
Binary file modified .gradle/2.13/taskArtifacts/taskArtifacts.bin
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
Expand Down Expand Up @@ -83,7 +84,7 @@ public void setServerURL(String serverURL) {
*/
@Override
public Dataverse getDataverseById(String dataverseAlias) {
String url = serverAPIURL + "/dataverses/" + dataverseAlias;
String url = createUrl("dataverses" , dataverseAlias);
log.debug(url);
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
Expand All @@ -93,10 +94,16 @@ public Dataverse getDataverseById(String dataverseAlias) {
log.debug(resp.getBody().toString());
return resp.getBody().getData();
}

private String createUrl(String ... pathComponents) {
String url = serverAPIURL + "/" + StringUtils.join(pathComponents, "/") ;
log.info("URL is {}", url);
return url;
}

@Override
public DataverseResponse<DvMessage> deleteDataverse(String dataverseAlias) {
String url = serverAPIURL + "/dataverses/" + dataverseAlias;
String url = createUrl("dataverses", dataverseAlias);
log.debug(url);
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
Expand All @@ -114,7 +121,7 @@ public DataverseResponse<Dataverse> createNewDataverse(String parentDv, Datavers
isTrue(!isEmpty(toCreate.getName()), "Name must be specified");
noNullElements(toCreate.getDataverseContacts(), "At least 1 email contact must be provided");
isTrue(!isEmpty(toCreate.getAlias()), "Alias must be specified");
String url = serverAPIURL + "/dataverses/" + parentDv;
String url = createUrl("dataverses", parentDv);

RestTemplate template = createTemplate();
String json = marshalDataset(toCreate);
Expand All @@ -133,7 +140,7 @@ public DataverseResponse<Dataverse> createNewDataverse(String parentDv, Datavers
*/
@Override
public Identifier createDataset(DatasetFacade facade, String dataverseAlias) {
String url = serverAPIURL + "/dataverses/" + dataverseAlias + "/datasets";
String url = createUrl("dataverses", dataverseAlias,"datasets");
RestTemplate template = createTemplate();

String json = getJsonFromFacade(facade);
Expand All @@ -152,7 +159,7 @@ public Identifier createDataset(DatasetFacade facade, String dataverseAlias) {
*/
@Override
public DatasetVersion updateDataset(DatasetFacade facade, Identifier id) {
String url = serverAPIURL + "/datasets/" + id.getId() + "/versions/:draft";
String url = createUrl("datasets", id.getId() +"", "versions",":draft");

Dataset ds = new DatasetBuilder().build(facade);
String json = marshalDataset(ds.getDatasetVersion());
Expand All @@ -173,7 +180,7 @@ public DatasetVersion updateDataset(DatasetFacade facade, Identifier id) {
*/
@Override
public Dataset getDataset(Identifier dsIdentifier) {
String url = serverAPIURL + "/datasets/" + dsIdentifier.getId();
String url = createUrl("datasets", dsIdentifier.getId() +"");
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
ParameterizedTypeReference<DataverseResponse<Dataset>> type = new ParameterizedTypeReference<DataverseResponse<Dataset>>() {
Expand All @@ -188,7 +195,7 @@ public Dataset getDataset(Identifier dsIdentifier) {
*/
@Override
public List<DatasetVersion> getDatasetVersions (Identifier dsIdentifier) {
String url = serverAPIURL + "/datasets/" + dsIdentifier.getId() +"/versions";
String url = createUrl("datasets", dsIdentifier.getId() +"", "versions");
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
ParameterizedTypeReference<DataverseResponse<List<DatasetVersion>>> type = new ParameterizedTypeReference<DataverseResponse<List<DatasetVersion>>>() {
Expand Down Expand Up @@ -218,7 +225,7 @@ public void uploadFile (String doi, File file) {
*/
@Override
public DvMessage deleteDataset(Identifier dsIdentifier) {
String url = serverAPIURL + "/datasets/" + dsIdentifier.getId();
String url = createUrl("datasets", dsIdentifier.getId() +"" );
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
ParameterizedTypeReference<DataverseResponse<DvMessage>> type = new ParameterizedTypeReference<DataverseResponse<DvMessage>>() {
Expand Down Expand Up @@ -256,7 +263,7 @@ private String marshalDataset(Object object) {
*/
@Override
public List<DataverseObject> getDataverseContents(String dataverseAlias) {
String url = serverAPIURL + "/dataverses/" + dataverseAlias + "/contents";
String url = createUrl("dataverses", dataverseAlias, "contents" );
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
ParameterizedTypeReference<DataverseResponse<List<DataverseObject>>> type = new ParameterizedTypeReference<DataverseResponse<List<DataverseObject>>>() {
Expand Down Expand Up @@ -285,7 +292,7 @@ private HttpHeaders addAPIKeyToHeader() {
*/
@Override
public List<MetadataBlock> getMetadataBlockInfo() {
String url = serverAPIURL + "/metadatablocks";
String url = createUrl("metadatablocks" );
RestTemplate template = createTemplate();
HttpHeaders headers = addAPIKeyToHeader();
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
Expand All @@ -302,7 +309,7 @@ public List<MetadataBlock> getMetadataBlockInfo() {
*/
@Override
public MetadataBlock getMetadataById(String name) {
String url = serverAPIURL + "/metadatablocks/" + name;
String url = createUrl("metadatablocks", name );
RestTemplate template = createTemplate();
HttpHeaders headers = addAPIKeyToHeader();
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
Expand Down Expand Up @@ -336,7 +343,8 @@ RestTemplate createTemplate() {
*/
@Override
public void publishDataset(Identifier dsIdentifier, Version version) {
String url = serverAPIURL + "/datasets/" + dsIdentifier.getId() +"/actions/:publish?type=" + version.name().toLowerCase();
String url = createUrl("datasets", dsIdentifier.getId() + "", "actions", ":publish") + "?type="
+ version.name().toLowerCase();
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
// ParameterizedTypeReference<DataverseResponse<DvMessage>> type = new ParameterizedTypeReference<DataverseResponse<DvMessage>>() {
Expand All @@ -350,7 +358,7 @@ public void publishDataset(Identifier dsIdentifier, Version version) {

@Override
public DataverseResponse<Dataverse> publishDataverse(String dvName) {
String url = serverAPIURL +"/dataverses/" + dvName + "/actions/:publish";
String url = createUrl("dataverses", dvName, "actions", ":publish");
RestTemplate template = createTemplate();
HttpEntity<String> entity = createHttpEntity("");
ParameterizedTypeReference<DataverseResponse<Dataverse>> type = new ParameterizedTypeReference<DataverseResponse<Dataverse>>() {
Expand Down

0 comments on commit 8c865f2

Please sign in to comment.