-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fix for issue 6877: Allow users to customize the API Key #7720
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
Conversation
update jabref
update jabref
…he customization tab of the preferences and check this key. 2. Update API_URL's protocol of SpringerFetcher to HTTPS
2. Modify the method of obtaining JabrefPreferences in SpringerFetcher (not completely completed).
@calixtus Now I have some difficulties with the preferences, can you give me some help? I don't know how to get the content of preferences in SpringerFetcher. My current idea is to use the constructor to pass in How should I find the singleton of |
src/main/java/org/jabref/logic/preferences/SpringerApiKeyPreferences.java
Outdated
Show resolved
Hide resolved
Overall looks good to me, I just think the prefs can be made more generic to hold other custom api keys as well |
You can simply add the CustomApiKeyPreferences to the ImportFormatPreferences, then you can acess it from the importFormatPreferences and don't need to pass the whole PreferencesService, see: jabref/src/main/java/org/jabref/logic/importer/ImportFormatPreferences.java Lines 21 to 31 in bb011c9
|
Yes, PreferencesService should be injected in the constructor (= passed to the constructor). One thing I believe that could be an issue: Currently only a created ImportFormatPreferences object is passed through. But the list of the WebFetchers is created when the GUI starts. So either the list of WebFetchers must be recreated each time the preferences have changed or (what I like way more) you refactor the WebFetchers class to use just the PreferencesService object which should be passed through to those fetchers that needs them. |
Do not use ImportFormatPreferences to pass the API keys as this class should be disbanded soon. You can mark it as deprecated if you want. |
2. add the way of customize IEEE Api Key.
# Conflicts: # src/main/resources/l10n/JabRef_en.properties
Perhaps ImportFormatPreferences can be turned into CustomPreferences so that it can carry more user custom content without affecting the original dependencies. |
The new version of the code allows users to customize more API Keys. Currently, Springer and IEEE are added. Developers can easily add more options here: Just simply add the name and test URL(remove the value of API key from query parameters) corresponding to the API Key. |
As for now ImportFormatPreferences is not a real Preferences object, since it simply collects other preferences objects and works as rucksack, frozen in time of creation. See JabRefPreferences::getImportGormatPreferences. What should instead happen is to create a preferences object like ApiKeyPreferences that includes all the ApiKeys and related preferences, that is being constructed by JabRefPreferences and is being stored in JabRefPreferences. |
I like your approach to construct a CustomApiKeyPreferences object for each key. I would just ask to create the list of the available keys not in the viewmodel of the preferences tab, but maybe to return it out of a static method in CustomApiKeyPreferences, maybe called getAvailableKeys, - or - to introduce a new method in the fetcher classes that returns if this fetcher uses a customizable API key and how it is called. |
I think it would also make sense to rename the "customization" tab to "Web search" (Fetcher is our internal notion) since everything in the preferences is about customization. |
How about CustomApiKeyPreferences ( rename to ApiKeyPreferences) designed as a class that manages custom API keys? public class ApiKeyItem {
private final String name;
private boolean useCustom;
private String customApiKey;
private String defaultApiKey;
private String testUrlWithoutApiKey;
// ...
}
public class ApiKeyPreferences {
private final PreferencesService preferences;
private final TreeMap<String, ApiKeyItem> apiKeyItems;
// It is created in JabRefPreferences, and the JabRefPreferences object is passed to the constructor
// In this way it can use some of the services provided by JabRefPreferences,
// at the same time proxy some of the work of JabRefPreferences
public CustomApiKeyPreferences(PreferencesService preferences) {
this.preferences = preferences;
}
public static void registerApiKey(String name, String defaultApiKey, String testUrlWithoutApiKey) {
// Only useCustom and customApiKey attributes are valid
ApiKeyItem item = preferences.getCustomApiKeyPreferences(name);
item.setDefaultApiKey(defaultApiKey);
item.setTestUrlWithoutApiKey(testUrlWithoutApiKey);
apiKeyItems.put(name, item);
}
public static String getApiKey(String name) {
preferences.getCustomApiKeyPreferences(name);
ApiKeyItem item = apiKeyItems.get(name);
String apiKey = "";
if (item!=null) {
if (item.useCustom) {
apiKey = item.getCustomApiKey();
} else {
apiKey = item.getDefaultApiKey();
}
}
return apiKey;
}
// Usually CustomizationTabViewModel uses it
public static void updateApiKeyItem(ApiKeyItem item){
apiKeyItems.put(item.getName(), item);
preferences.storeCustomApiKeyPreferences(item);
}
// Usually CustomizationTabViewModel uses it
public static ArrayList<ApiKeyItem> getAvailableApiKey() {
return (ArrayList<String>) apiKeyItems.values();
}
} This design is more compatible with the previous model. In addition, I also noticed that |
Some API keys are deliberately stored in the GitHub secrets to hide them from public access, since we use private keys just for the ci tests, that should work, even when the public keys are locked out. |
If the design of ApiKeyItem and ApiKeyPreferences is feasible, I will start to modify and optimize the code. |
Sorry had no time yet to look into detail, will comment tonight. |
Just on a first look: why do you want to introduce this class ApiKeyPreferences? This seems to be way more complicated, than it has to be. It looks like you just put another layer around the keys. Just put the get and update and store methods in JabRefPreferences. If you have to, put the API keys into cache on lazy loading. |
This makes the logic of management API Key more centralized.
Now the custom API Key is only loaded when the preference tab dialog is opened (CustomizationTabViewModel#setValues()), and cannot be accessed directly through a static method. If ApiKeyPreferences is not needed, compare initApiKeyNameUrl and registerApiKey in CustomizationTabViewModel.
|
# Conflicts: # src/main/resources/l10n/JabRef_en.properties
# Conflicts: # src/test/java/org/jabref/logic/importer/fetcher/CompositeSearchBasedFetcherTest.java
src/main/java/org/jabref/logic/importer/fetcher/AstrophysicsDataSystem.java
Outdated
Show resolved
Hide resolved
src/main/java/org/jabref/logic/importer/fetcher/CustomizableKeyFetcher.java
Outdated
Show resolved
Hide resolved
if (!apiKey.isEmpty()) { | ||
URLDownload urlDownload; | ||
try { | ||
SSLSocketFactory defaultSslSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory(); |
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.
Not sure how, but I think this should take the custom SSL store into account
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.
I am not completely sure, but I strongly believe that SSL is handled by URLDownload or in the DefaultSSlSocketFactory internally. At least I have no place found in our codebase that handles the custom SSL store directly besides the custom SSL preferences. Also the pr that introduced custom SSL certificates did not introduce any custom SSL handling anywhere, but just removed the infamous bypassSSLVerification.
@ryyyc Thank you for your effort. We took the liberty to finalize the PR and include it so that it is available on our next release. |
Co-authored-by: Carl Christian Snethlage <[email protected]>
Fixes #6877
CHANGELOG.md
described in a way that is understandable for the average user (if applicable)Changed:
Initial default value, do not use custom API key:
Check success with a valid API key:
Check failed with an invalid API key:
Search using a custom Springer API key: