refactored connection classes to be more generic and accept credentials of different apps.
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package com.vaessl.app.connection;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
import com.vaessl.app.dto.ConnectionRequest;
|
||||
import com.vaessl.app.dto.ConnectionResponse;
|
||||
@@ -9,20 +12,20 @@ import com.vaessl.app.dto.ConnectionResponse;
|
||||
@Service
|
||||
public class ConnectionService {
|
||||
|
||||
private final RestClient.Builder restClientBuilder;
|
||||
private final Map<String, ConnectionProvider> providerRegistry;
|
||||
|
||||
public ConnectionService(RestClient.Builder restClientBuilder) {
|
||||
this.restClientBuilder = restClientBuilder;
|
||||
public ConnectionService(List<ConnectionProvider> providers) {
|
||||
this.providerRegistry = providers.stream()
|
||||
.collect(Collectors.toMap(ConnectionProvider::getServiceType, p -> p));
|
||||
}
|
||||
|
||||
public ConnectionResponse login(ConnectionRequest request) {
|
||||
// TODO: Look into Map<String, RestClient> to cache restclient requests.
|
||||
return restClientBuilder.baseUrl(request.appUrl())
|
||||
.build()
|
||||
.post()
|
||||
.uri("/api/v1/users/login")
|
||||
.body(request)
|
||||
.retrieve()
|
||||
.body(ConnectionResponse.class);
|
||||
ConnectionProvider provider = providerRegistry.get(request.serviceType().toUpperCase());
|
||||
|
||||
if (provider == null) {
|
||||
throw new IllegalArgumentException("Unknown provider: " + request.serviceType());
|
||||
}
|
||||
|
||||
return provider.authenticate(request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user