Skip to content
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

feat: create form of Google Book API Key #83

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/apis/base_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface BaseBooksApiImpl {

export function factoryServiceProvider(settings: BookSearchPluginSettings): BaseBooksApiImpl {
if (settings.serviceProvider === ServiceProvider.google) {
return new GoogleBooksApi(settings.localePreference);
return new GoogleBooksApi(settings.localePreference, settings.apiKey);
}
if (settings.serviceProvider === ServiceProvider.naver) {
if (!settings.naverClientId || !settings.naverClientSecret) {
Expand Down
11 changes: 10 additions & 1 deletion src/apis/google_books_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import { Book } from '@models/book.model';
import { apiGet, BaseBooksApiImpl } from '@apis/base_api';
import { GoogleBooksResponse, VolumeInfo } from './models/google_books_response';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const Electron = require('electron');
const {
remote: { safeStorage },
} = Electron;

export class GoogleBooksApi implements BaseBooksApiImpl {
constructor(private readonly localePreference: string) {}
constructor(private readonly localePreference: string, private readonly apiKey?: string) {}

async getByQuery(query: string) {
try {
Expand All @@ -18,6 +24,9 @@ export class GoogleBooksApi implements BaseBooksApiImpl {
} else {
params['langRestrict'] = langRestrict;
}
if (this.apiKey !== '') {
params['key'] = safeStorage.isEncryptionAvailable() ? safeStorage.decryptString(this.apiKey) : this.apiKey;
}
const searchResults = await apiGet<GoogleBooksResponse>('https://www.googleapis.com/books/v1/volumes', params);
if (!searchResults?.totalItems) {
return [];
Expand Down
39 changes: 38 additions & 1 deletion src/settings/settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { App, PluginSettingTab, Setting } from 'obsidian';
import { App, PluginSettingTab, Setting, Notice } from 'obsidian';
import { replaceDateInString } from '@utils/utils';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const Electron = require('electron');
const {
remote: { safeStorage },
} = Electron;

import BookSearchPlugin from '../main';
import { FileNameFormatSuggest } from './suggesters/FileNameFormatSuggester';
import { FolderSuggest } from './suggesters/FolderSuggester';
Expand All @@ -27,6 +33,7 @@ export interface BookSearchPluginSettings {
naverClientId: string;
naverClientSecret: string;
localePreference: string;
apiKey: string;
openPageOnCompletion: boolean;
}

Expand All @@ -42,6 +49,7 @@ export const DEFAULT_SETTINGS: BookSearchPluginSettings = {
naverClientId: '',
naverClientSecret: '',
localePreference: 'default',
apiKey: '',
openPageOnCompletion: true,
};

Expand Down Expand Up @@ -277,6 +285,35 @@ export class BookSearchSettingTab extends PluginSettingTab {
});
}),
);

// API Settings
const APISettingsChildren: Setting[] = [];
createFoldingHeader(containerEl, 'Google API Settings', APISettingsChildren);
let tempKeyValue = '';
APISettingsChildren.push(
new Setting(containerEl)
.setClass('book-search-plugin__hide')
.setName('Google Book API Key')
.setDesc(
'Add your Books API key. **WARNING** please use this field after you must understand Google Cloud API, such as API key security.',
)
.addText(text => {
text.onChange(async value => {
if (safeStorage.isEncryptionAvailable()) {
tempKeyValue = safeStorage.enctyptString(value);
} else {
tempKeyValue = value;
}
});
})
.addButton(button => {
button.setButtonText('Save Key').onClick(async () => {
this.plugin.settings.apiKey = tempKeyValue;
await this.plugin.saveSettings();
new Notice('Apikey Saved');
});
}),
);
}
}

Expand Down
Loading