io.rocketbase.commons.resource.UserSearchResource Maven / Gradle / Ivy
package io.rocketbase.commons.resource;
import io.rocketbase.commons.adapters.JwtRestTemplate;
import io.rocketbase.commons.dto.PageableResult;
import io.rocketbase.commons.dto.appuser.QueryAppUser;
import io.rocketbase.commons.exception.NotFoundException;
import io.rocketbase.commons.model.AppUserReference;
import io.rocketbase.commons.model.SimpleAppUserToken;
import lombok.SneakyThrows;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.Optional;
import static io.rocketbase.commons.resource.AppUserResource.addQueryParams;
public class UserSearchResource implements BaseRestResource {
public static final String API_USER = "/api/user-search/";
protected RestTemplate restTemplate;
protected String baseAuthApiUrl;
public UserSearchResource(String baseAuthApiUrl, RestTemplate restTemplate) {
Assert.hasText(baseAuthApiUrl, "baseAuthApiUrl is required");
this.restTemplate = restTemplate;
this.baseAuthApiUrl = baseAuthApiUrl;
}
public UserSearchResource(JwtRestTemplate restTemplate) {
this.restTemplate = restTemplate;
this.baseAuthApiUrl = restTemplate.getTokenProvider().getBaseAuthApiUrl();
}
@SneakyThrows
public PageableResult search(QueryAppUser query, Pageable pageable) {
UriComponentsBuilder uriBuilder = appendParams(createUriComponentsBuilder(baseAuthApiUrl), pageable)
.path(API_USER);
addQueryParams(query, uriBuilder);
ResponseEntity> response = restTemplate.exchange(uriBuilder.toUriString(),
HttpMethod.GET,
new HttpEntity<>(createHeaderWithLanguage()),
createPagedTypeReference());
return (PageableResult) response.getBody();
}
public Optional findByUsernameOrId(String usernameOrId) {
ResponseEntity response;
try {
response = restTemplate.exchange(createUriComponentsBuilder(baseAuthApiUrl)
.path(API_USER)
.path(usernameOrId)
.toUriString(),
HttpMethod.GET,
new HttpEntity<>(createHeaderWithLanguage()),
SimpleAppUserToken.class);
} catch (NotFoundException e) {
return Optional.empty();
}
return Optional.ofNullable(response.getBody());
}
protected ParameterizedTypeReference> createPagedTypeReference() {
return new ParameterizedTypeReference>() {
};
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy