Skip to content

Commit

Permalink
Separate methods for Resource and ExtensionAttributes
Browse files Browse the repository at this point in the history
Signed-off-by: BOUHOURS Antoine <[email protected]>
  • Loading branch information
antoinebhs committed Sep 23, 2024
1 parent 87c9eee commit de1e708
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/
package com.powsybl.network.store.client;

import com.powsybl.network.store.model.AbstractTopLevelDocument;
import com.powsybl.network.store.model.Attributes;
import com.powsybl.network.store.model.ExtensionAttributes;
import com.powsybl.network.store.model.IdentifiableAttributes;
import com.powsybl.network.store.model.Resource;
import org.springframework.core.ParameterizedTypeReference;
Expand All @@ -22,9 +22,16 @@ public interface RestClient {

<T extends IdentifiableAttributes> void createAll(String url, List<Resource<T>> resources, Object... uriVariables);

<T, D extends AbstractTopLevelDocument<T>> Optional<T> getOne(String target, String url, ParameterizedTypeReference<D> parameterizedTypeReference, Object... uriVariables);
<T extends IdentifiableAttributes> Optional<Resource<T>> getOne(String target, String url, Object... uriVariables);

<T, D extends AbstractTopLevelDocument<T>> List<T> getAll(String target, String url, ParameterizedTypeReference<D> parameterizedTypeReference, Object... uriVariables);
/**
* Retrieves one extension attributes from the server.
* @return {@link ExtensionAttributes} which is a subset of an identifiable resource. The extension attributes can be put in the extensionAttributes
* map of an {@link IdentifiableAttributes} or used to load an extension.
*/
Optional<ExtensionAttributes> getOneExtensionAttributes(String url, Object... uriVariables);

<T extends IdentifiableAttributes> List<Resource<T>> getAll(String target, String url, Object... uriVariables);

<T extends Attributes> void updateAll(String url, List<Resource<T>> resources, Object... uriVariables);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,18 @@ public <T extends IdentifiableAttributes> void createAll(String url, List<Resour
}

@Override
public <T, D extends AbstractTopLevelDocument<T>> Optional<T> getOne(String target, String url, ParameterizedTypeReference<D> parameterizedTypeReference, Object... uriVariables) {
public <T extends IdentifiableAttributes> Optional<Resource<T>> getOne(String target, String url, Object... uriVariables) {
return getOneDocument(target, url, new ParameterizedTypeReference<TopLevelDocument<T>>() {
}, uriVariables);
}

@Override
public Optional<ExtensionAttributes> getOneExtensionAttributes(String url, Object... uriVariables) {
return getOneDocument(null, url, new ParameterizedTypeReference<ExtensionAttributesTopLevelDocument>() {
}, uriVariables);
}

public <T, D extends AbstractTopLevelDocument<T>> Optional<T> getOneDocument(String target, String url, ParameterizedTypeReference<D> parameterizedTypeReference, Object... uriVariables) {
ResponseEntity<D> response = getDocument(url, parameterizedTypeReference, uriVariables);
if (response.getStatusCode() == HttpStatus.OK) {
AbstractTopLevelDocument<T> body = getBody(response);
Expand All @@ -103,12 +114,13 @@ public <T, D extends AbstractTopLevelDocument<T>> Optional<T> getOne(String targ
}

@Override
public <T, D extends AbstractTopLevelDocument<T>> List<T> getAll(String target, String url, ParameterizedTypeReference<D> parameterizedTypeReference, Object... uriVariables) {
ResponseEntity<D> response = getDocument(url, parameterizedTypeReference, uriVariables);
public <T extends IdentifiableAttributes> List<Resource<T>> getAll(String target, String url, Object... uriVariables) {
ResponseEntity<TopLevelDocument<T>> response = getDocument(url, new ParameterizedTypeReference<>() {
}, uriVariables);
if (response.getStatusCode() != HttpStatus.OK) {
throw createHttpException(url, "get", response.getStatusCode());
}
AbstractTopLevelDocument<T> body = getBody(response);
TopLevelDocument<T> body = getBody(response);
return body.getData();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ private <T extends IdentifiableAttributes> List<Resource<T>> getAll(String targe
LOGGER.info("Loading {} resources {}", target, UriComponentsBuilder.fromUriString(url).buildAndExpand(uriVariables));
}
Stopwatch stopwatch = Stopwatch.createStarted();
List<Resource<T>> resourceList = restClient.getAll(target, url, new ParameterizedTypeReference<TopLevelDocument<T>>() {
}, uriVariables);
List<Resource<T>> resourceList = restClient.getAll(target, url, uriVariables);
stopwatch.stop();
LOGGER.info("{} {} resources loaded in {} ms", resourceList.size(), target, stopwatch.elapsed(TimeUnit.MILLISECONDS));
return resourceList;
Expand All @@ -110,8 +109,7 @@ private <T extends IdentifiableAttributes> Optional<Resource<T>> get(String targ
LOGGER.info("Loading {} resource {}", target, UriComponentsBuilder.fromUriString(url).buildAndExpand(uriVariables));
}
Stopwatch stopwatch = Stopwatch.createStarted();
Optional<Resource<T>> resource = restClient.getOne(target, url, new ParameterizedTypeReference<TopLevelDocument<T>>() {
}, uriVariables);
Optional<Resource<T>> resource = restClient.getOne(target, url, uriVariables);
stopwatch.stop();
LOGGER.info("{} resource (empty={}) loaded in {} ms", target, resource.isEmpty(), stopwatch.elapsed(TimeUnit.MILLISECONDS));
return resource;
Expand All @@ -120,8 +118,7 @@ private <T extends IdentifiableAttributes> Optional<Resource<T>> get(String targ
private Optional<ExtensionAttributes> getExtensionAttributes(String urlTemplate, Object... uriVariables) {
logGetExtensionAttributesUrl(urlTemplate, uriVariables);
Stopwatch stopwatch = Stopwatch.createStarted();
Optional<ExtensionAttributes> extensionAttributes = restClient.getOne(null, urlTemplate, new ParameterizedTypeReference<ExtensionAttributesTopLevelDocument>() {
}, uriVariables);
Optional<ExtensionAttributes> extensionAttributes = restClient.getOneExtensionAttributes(urlTemplate, uriVariables);
stopwatch.stop();
logGetExtensionAttributesTime(extensionAttributes.isPresent() ? 1 : 0, stopwatch.elapsed(TimeUnit.MILLISECONDS));

Expand Down

0 comments on commit de1e708

Please sign in to comment.