-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ea 4015 set client code #277
Changes from all commits
c7137ae
8c5decf
5724d0a
7fa4917
915dfd7
b7f4ba7
9bfdd58
6eeefd0
291f697
be938be
47b5f56
b23ea1e
8812fe6
ac9dc4b
fa4667a
1ec71f6
0f60373
dc4a230
ee7594b
ca4857b
a6b9ca0
aa169ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package eu.europeana.set.client; | ||
|
||
import eu.europeana.set.client.config.ClientConfiguration; | ||
import eu.europeana.set.client.exception.SetApiClientException; | ||
import eu.europeana.set.client.model.result.RecordPreview; | ||
import eu.europeana.set.client.web.SearchUserSetApi; | ||
import eu.europeana.set.client.web.WebUserSetApi; | ||
import eu.europeana.set.definitions.model.UserSet; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Implementation of client api | ||
* | ||
* @author GordeaS | ||
* Refractored by Srishti Singh | ||
*/ | ||
|
||
public class UserSetApiClient extends BaseUserSetApi { | ||
|
||
private final WebUserSetClient webUserSetClient; | ||
private final SearchUserSetClient searchUserSetClient; | ||
|
||
public UserSetApiClient(ClientConfiguration configuration) throws SetApiClientException { | ||
super(configuration); | ||
this.webUserSetClient = new WebUserSetClient(); | ||
this.searchUserSetClient = new SearchUserSetClient(); | ||
} | ||
|
||
public WebUserSetApi getWebUserSetApi() { | ||
return webUserSetClient; | ||
} | ||
|
||
public SearchUserSetApi getSearchUserSetApi() { | ||
return searchUserSetClient; | ||
} | ||
|
||
/** | ||
* Web User Set Client class | ||
*/ | ||
private class WebUserSetClient implements WebUserSetApi { | ||
@Override | ||
public UserSet createUserSet(String set, String profile) throws SetApiClientException { | ||
return getApiConnection().createUserSet(set, profile); | ||
|
||
} | ||
|
||
@Override | ||
public String deleteUserSet(String identifier) throws SetApiClientException { | ||
return getApiConnection().deleteUserSet(identifier); | ||
} | ||
|
||
@Override | ||
public UserSet getUserSet(String identifier, String profile) throws SetApiClientException { | ||
return getApiConnection().getUserSet(identifier, profile); | ||
} | ||
|
||
@Override | ||
public UserSet updateUserSet(String identifier, String set, String profile) throws SetApiClientException { | ||
return getApiConnection().updateUserSet(identifier, set, profile); | ||
} | ||
|
||
@Override | ||
public List<RecordPreview> getPaginationUserSet(String identifier, String sort, String sortOrder, String page, String pageSize, String profile) throws SetApiClientException { | ||
return getApiConnection().getPaginationUserSet(identifier, sort, sortOrder, page, pageSize, profile); | ||
} | ||
} | ||
|
||
private class SearchUserSetClient implements SearchUserSetApi { | ||
|
||
@Override | ||
public List<? extends UserSet> searchUserSet(String query, String[] qf, | ||
String sort, String page, String pageSize, String facet, int facetLimit, String profile) throws SetApiClientException { | ||
return getApiConnection().searchUserSet(query, qf, sort, page, pageSize, facet, facetLimit, profile); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,126 +1,76 @@ | ||
package eu.europeana.set.client.config; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Properties; | ||
|
||
import eu.europeana.set.client.exception.TechnicalRuntimeException; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
/** | ||
* configuration for accessing remote api | ||
* | ||
* @author GordeaS | ||
* | ||
*/ | ||
|
||
public final class ClientConfiguration { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(ClientConfiguration.class); | ||
|
||
protected static final String SET_CLIENT_PROPERTIES_FILE = "/set-client.user.properties"; | ||
protected static final String PROP_SET_API_KEY = "set.api.key"; | ||
protected static final String PROP_SET_SERVICE_URI = "set.service.uri"; | ||
protected static final String PROP_OAUTH_SERVICE_URI = "oauth.service.uri"; | ||
protected static final String PROP_OAUTH_REQUEST_PARAMS = "oauth.token.request.params"; | ||
public static final String PROP_SET_API_KEY = "set.api.key"; | ||
public static final String PROP_SET_SERVICE_URI = "set.service.uri"; | ||
public static final String PROP_OAUTH_SERVICE_URI = "oauth.service.uri"; | ||
public static final String PROP_OAUTH_REQUEST_PARAMS = "oauth.token.request.params"; | ||
|
||
private Properties properties; | ||
private static ClientConfiguration singleton; | ||
|
||
/** | ||
* Hide the default constructor | ||
*/ | ||
private ClientConfiguration() { | ||
} | ||
|
||
/** | ||
* Accessor method for the singleton | ||
* | ||
* @return | ||
* Creates ClientConfiguration instance with set client properties | ||
*/ | ||
public static synchronized ClientConfiguration getInstance() { | ||
if (singleton == null) { | ||
singleton = new ClientConfiguration(); | ||
singleton.loadProperties(); | ||
} | ||
|
||
return singleton; | ||
public ClientConfiguration() { | ||
loadProperties(SET_CLIENT_PROPERTIES_FILE); | ||
} | ||
|
||
/** | ||
* Laizy loading of configuration properties | ||
* CConstructor to inject properties | ||
* @param properties | ||
*/ | ||
public synchronized void loadProperties() { | ||
|
||
try { | ||
properties = new Properties(); | ||
InputStream resourceAsStream = getClass().getResourceAsStream(SET_CLIENT_PROPERTIES_FILE); | ||
if (resourceAsStream == null) { | ||
throw new TechnicalRuntimeException( | ||
"No properties file found in classpath! " + SET_CLIENT_PROPERTIES_FILE); | ||
} | ||
getProperties().load(resourceAsStream); | ||
|
||
} catch (RuntimeException | IOException e) { | ||
throw new TechnicalRuntimeException("Cannot read configuration file: " + SET_CLIENT_PROPERTIES_FILE, e); | ||
} | ||
|
||
public ClientConfiguration(Properties properties) { | ||
this.properties = properties; | ||
} | ||
|
||
/** | ||
* provides access to the configuration properties. It is not recommended to use | ||
* the properties directly, but the | ||
* | ||
* @return | ||
*/ | ||
Properties getProperties() { | ||
return properties; | ||
private Properties loadProperties(String propertiesFile) { | ||
try { | ||
properties = new Properties(); | ||
properties.load(getClass().getResourceAsStream(propertiesFile)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should support externalization of configurations, meaning to be able to handle absolute file path using the file protocol see: https://stackoverflow.com/questions/8406025/local-file-protocol-for-java-net-url. Third parties should be able to provide the location of configuration files. (absolute file path should use the fille:// protocol) |
||
} catch (IOException e) { | ||
LOGGER.error("Error loading the properties file {}", propertiesFile); | ||
} | ||
return properties; | ||
} | ||
|
||
/** | ||
* | ||
* @return the name of the file storing the client configuration | ||
*/ | ||
String getConfigurationFile() { | ||
return SET_CLIENT_PROPERTIES_FILE; | ||
} | ||
|
||
/** | ||
* This method provides access to the API key defined in the configuration file | ||
* | ||
* @see PROP_EUROPEANA_API_KEY | ||
* | ||
* @return | ||
*/ | ||
public String getApiKey() { | ||
return getProperties().getProperty(PROP_SET_API_KEY); | ||
return getProperty(PROP_SET_API_KEY); | ||
} | ||
|
||
/** | ||
* This method provides access to the search uri value defined in the | ||
* configuration file | ||
* | ||
* @see PROP_EUROPEANA_SEARCH_URI | ||
* | ||
* @return | ||
*/ | ||
|
||
public String getServiceUri() { | ||
return getProperties().getProperty(PROP_SET_SERVICE_URI); | ||
return getProperty(PROP_SET_SERVICE_URI); | ||
} | ||
|
||
/** | ||
* This method returns the uri of the oauth service as configured in | ||
* | ||
* @return | ||
*/ | ||
public String getOauthServiceUri() { | ||
return getProperties().getProperty(PROP_OAUTH_SERVICE_URI); | ||
return getProperty(PROP_OAUTH_SERVICE_URI); | ||
} | ||
|
||
/** | ||
* This method returns the request params needed to acquire a new token | ||
* | ||
* @return | ||
*/ | ||
public String getOauthRequestParams() { | ||
return getProperties().getProperty(PROP_OAUTH_REQUEST_PARAMS); | ||
return getProperty(PROP_OAUTH_REQUEST_PARAMS); | ||
} | ||
|
||
private String getProperty(String propertyName) { | ||
return properties.getProperty(propertyName); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason to change the configuration to not be a singleton anymore? We should avoid reading the properties files multiple times as that can generate major performance loss
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, the filePath for the configuration file should be provided as parameter and stored as class attribute
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually, we use spring boot and create a bean, only a single instance is always created.
Also, when creating a client for any API, we create an instance of the Client class, and that class should be able to accept a configuration class.
public UserSetApiClient(ClientConfiguration configuration)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, but the properties still need to be read and currently we only support properties from the classpath