Skip to content
Snippets Groups Projects
Commit a18b599e authored by Martin Weise's avatar Martin Weise
Browse files

added zenodo api

Former-commit-id: 4c0da8ac
parent 3fec09cb
No related branches found
No related tags found
1 merge request!42Fixed the query service tests
Showing
with 306 additions and 118 deletions
ZENODO_API_KEY=OjphEf9axmjOxPqe419sTkhlVKZKA335NH5OGw0g4DyRUSd0LMryNtjNWg7x
\ No newline at end of file
...@@ -4,6 +4,9 @@ target/ ...@@ -4,6 +4,9 @@ target/
!**/src/main/**/target/ !**/src/main/**/target/
!**/src/test/**/target/ !**/src/test/**/target/
### Environment ###
.env
### Generated ### ### Generated ###
mapping.xml mapping.xml
rest-service/src/main/java/at/tuwien/userdb/Table.java rest-service/src/main/java/at/tuwien/userdb/Table.java
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>fda-table-service</artifactId>
<groupId>at.tuwien</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>api</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
package at.tuwien.api.zenodo;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MetadataDto {
@JsonProperty("prereserve_doi")
private PreserveDoiDto prereserveDoi;
}
package at.tuwien.api.zenodo;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PreserveDoiDto {
private String doi;
@JsonProperty("recid")
private Long recId;
}
package at.tuwien.api.zenodo.deposit;
import at.tuwien.api.zenodo.MetadataDto;
import at.tuwien.api.zenodo.files.FileDto;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import java.time.Instant;
import java.util.List;
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DepositDto {
@JsonProperty("conceptrecid")
private Long conceptRecId;
private Instant created;
private List<FileDto> files;
private Long id;
private Instant modified;
private Long owner;
private MetadataDto metadata;
@JsonProperty("record_id")
private Long recordId;
private String state;
private Boolean submitted;
private String title;
}
package at.tuwien.api.zenodo.deposit;
import lombok.*;
@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DepositLinksDto {
private String bucket;
private String discard;
private String edit;
private String files;
private String html;
private String latest_draft;
private String latest_draft_html;
private String publish;
private String self;
}
package at.tuwien.api.zenodo.files;
import lombok.*;
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FileDto {
private String checksum;
private String filename;
private Long filesize;
private String id;
private FileLinksDto links;
}
package at.tuwien.api.zenodo.files;
import lombok.*;
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FileLinksDto {
private String download;
private String self;
}
...@@ -2,29 +2,26 @@ ...@@ -2,29 +2,26 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent> <parent>
<artifactId>fda-table-service</artifactId> <artifactId>fda-table-service</artifactId>
<groupId>at.tuwien</groupId> <groupId>at.tuwien</groupId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gateways</artifactId> <artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fda-table-service-gateways</name>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>at.tuwien</groupId> <groupId>at.tuwien</groupId>
<artifactId>api</artifactId> <artifactId>api</artifactId>
<version>${project.version}</version> <version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project> </project>
\ No newline at end of file
package at.tuwien.config;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriTemplateHandler;
@Configuration
public class ZenodoConfig {
@Getter
@Value("${zenodo.endpoint}")
private String zenodoEndpoint;
@Getter
@Value("${zenodo.api_key}")
private String zenodoApiKey;
@Bean
public RestTemplate zenodo() {
DefaultUriTemplateHandler defaultUriTemplateHandler = new DefaultUriTemplateHandler();
defaultUriTemplateHandler.setBaseUrl(zenodoEndpoint);
return new RestTemplateBuilder()
.uriTemplateHandler(defaultUriTemplateHandler)
.build();
}
}
package at.tuwien.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.FORBIDDEN)
public class ZenodoApiException extends Exception {
public ZenodoApiException(String msg) {
super(msg);
}
public ZenodoApiException(String msg, Throwable thr) {
super(msg, thr);
}
public ZenodoApiException(Throwable thr) {
super(thr);
}
}
package at.tuwien.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.FORBIDDEN)
public class ZenodoAuthenticationException extends Exception {
public ZenodoAuthenticationException(String msg) {
super(msg);
}
public ZenodoAuthenticationException(String msg, Throwable thr) {
super(msg, thr);
}
public ZenodoAuthenticationException(Throwable thr) {
super(thr);
}
}
package at.tuwien.gateway;
import at.tuwien.api.zenodo.deposit.DepositDto;
import at.tuwien.config.ZenodoConfig;
import at.tuwien.exception.ZenodoApiException;
import at.tuwien.exception.ZenodoAuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
import java.util.List;
@Component
public class ZenodoGateway {
private final RestTemplate zenodoRestTemplate;
private final ZenodoConfig zenodoConfig;
@Autowired
public ZenodoGateway(RestTemplate zenodoRestTemplate, ZenodoConfig zenodoConfig) {
this.zenodoRestTemplate = zenodoRestTemplate;
this.zenodoConfig = zenodoConfig;
}
public List<DepositDto> listDeposits() throws ZenodoAuthenticationException, ZenodoApiException {
final ResponseEntity<DepositDto[]> response = zenodoRestTemplate.exchange("/api/deposit/depositions?access_token={token}",
HttpMethod.GET, null, DepositDto[].class, zenodoConfig.getZenodoApiKey());
if (response.getStatusCode().is4xxClientError()) {
throw new ZenodoAuthenticationException("Token is missing or invalid.");
}
if (response.getBody() == null) {
throw new ZenodoApiException("Endpoint returned null body");
}
if (response.getBody().length == 0) {
throw new ZenodoApiException("Endpoint returned empty body");
}
return Arrays.asList(response.getBody());
}
}
package at.tuwien.client;
import at.tuwien.dto.CreateTableViaCsvDTO;
import at.tuwien.mapper.ContainerIdAndQueryToExecuteInternalQueryMapper;
import at.tuwien.mapper.CreateTableViaCsvToExecuteStatementMapper;
import at.tuwien.model.CreateCSVTableWithDataset;
import at.tuwien.model.ExecuteInternalQueryDTO;
import at.tuwien.model.ExecuteStatementDTO;
import at.tuwien.model.QueryResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@Component
public class FdaQueryServiceClient {
private static final Logger LOGGER = LoggerFactory.getLogger(FdaQueryServiceClient.class);
private WebClient.Builder webClientBuilder;
private CreateTableViaCsvToExecuteStatementMapper mapper;
@Autowired
public FdaQueryServiceClient(WebClient.Builder webClientBuilder, CreateTableViaCsvToExecuteStatementMapper mapper) {
this.webClientBuilder = webClientBuilder;
this.mapper = mapper;
}
/**
* @deprecated use Spring RestTempaltes in future releases
*/
public boolean executeStatement(CreateTableViaCsvDTO dto, String statement) {
LOGGER.debug("request fda-query-service for executing statement");
ExecuteStatementDTO statementDTO = mapper.map(dto, statement);
ClientResponse response = webClientBuilder
.build()
.post()
.uri("http://fda-query-service/query/executeStatement")
.contentType(MediaType.APPLICATION_JSON)
.body(Mono.just(statementDTO), ExecuteStatementDTO.class)
.exchange()
.block();
return true;
}
/**
* @deprecated use Spring RestTempaltes in future releases
*/
public QueryResult executeQuery(String containerID, String query) {
ContainerIdAndQueryToExecuteInternalQueryMapper mapper = new ContainerIdAndQueryToExecuteInternalQueryMapper();
ExecuteInternalQueryDTO execInternalQueryDTO = mapper.map(containerID, query);
QueryResult queryResult = webClientBuilder
.build()
.post()
.uri("http://fda-query-service/query/executeQuery")
.contentType(MediaType.APPLICATION_JSON)
.body(Mono.just(execInternalQueryDTO), ExecuteInternalQueryDTO.class)
.retrieve()
.bodyToMono(QueryResult.class)
.block();
return queryResult;
}
/**
* @deprecated use Spring RestTempaltes in future releases
*/
public boolean copyCSVIntoTable(CreateCSVTableWithDataset tableWithDataset) {
ClientResponse response = webClientBuilder
.build()
.post()
.uri("http://fda-query-service/query/copyCSVIntoTable")
.contentType(MediaType.APPLICATION_JSON)
.body(Mono.just(tableWithDataset), CreateCSVTableWithDataset.class)
.exchange()
.block();
if(response.statusCode().is2xxSuccessful()){
return true;
}
return false;
}
}
package at.tuwien.mapper;
import at.tuwien.dto.CreateTableViaCsvDTO;
import at.tuwien.model.ExecuteStatementDTO;
import org.springframework.stereotype.Component;
@Component
public class CreateTableViaCsvToExecuteStatementMapper {
public ExecuteStatementDTO map(CreateTableViaCsvDTO dto, String statement){
ExecuteStatementDTO statementDTO = new ExecuteStatementDTO();
statementDTO.setContainerID(dto.getContainerId());
statementDTO.setStatement(statement);
return statementDTO;
}
}
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
<module>rest-service</module> <module>rest-service</module>
<module>services</module> <module>services</module>
<module>report</module> <module>report</module>
<module>gateway</module>
<module>api</module>
</modules> </modules>
<properties> <properties>
......
...@@ -19,6 +19,12 @@ ...@@ -19,6 +19,12 @@
<artifactId>services</artifactId> <artifactId>services</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>at.tuwien</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -31,3 +31,6 @@ eureka: ...@@ -31,3 +31,6 @@ eureka:
fda: fda:
mapping.path: /root mapping.path: /root
table.path: /root table.path: /root
zenodo:
endpoint: https://sandbox.zenodo.org/
api_key: "${ZENODO_API_KEY}"
\ No newline at end of file
...@@ -32,3 +32,6 @@ eureka: ...@@ -32,3 +32,6 @@ eureka:
fda: fda:
mapping.path: rest-service/src/main/resources mapping.path: rest-service/src/main/resources
table.path: rest-service/src/main/java/at/tuwien/userdb table.path: rest-service/src/main/java/at/tuwien/userdb
zenodo:
endpoint: https://sandbox.zenodo.org/
api_key: "${ZENODO_API_KEY}"
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment