diff --git a/src/apis/base_api.ts b/src/apis/base_api.ts index 6c348f1..085f157 100644 --- a/src/apis/base_api.ts +++ b/src/apis/base_api.ts @@ -1,10 +1,23 @@ import { Book } from '@models/book.model'; +import { BookSearchPluginSettings } from '@settings/settings'; +import { ServiceProvider } from '@src/constants'; import { request } from 'obsidian'; +import { GoogleBooksApi } from './google_books_api'; +import { NaverBooksApi } from './naver_books_api'; export interface BaseBooksApiImpl { getByQuery(query: string): Promise; } +export function getServiceProvider(settings: BookSearchPluginSettings): BaseBooksApiImpl { + if (settings.serviceProvider === ServiceProvider.google) { + return new GoogleBooksApi(); + } + if (settings.serviceProvider === ServiceProvider.naver) { + return new NaverBooksApi(settings.naverClientId, settings.naverClientSecret); + } +} + export class BaseBooksApi implements BaseBooksApiImpl { getByQuery(_query: string): Promise { throw new Error('Method not implemented.'); diff --git a/src/apis/naver_books_api.ts b/src/apis/naver_books_api.ts index 9fd2d6d..efde058 100644 --- a/src/apis/naver_books_api.ts +++ b/src/apis/naver_books_api.ts @@ -7,6 +7,9 @@ export class NaverBooksApi extends BaseBooksApi { constructor(private readonly clientId, private readonly clientSecret: string) { super(); + if (!clientId || !clientSecret) { + throw new Error('네이버 개발자센터에서 발급받은 `Client ID`와 `Client Secret`이 설정되지 않았습니다.'); + } } async getByQuery(query: string) { diff --git a/src/main.ts b/src/main.ts index eb06a4b..536d733 100644 --- a/src/main.ts +++ b/src/main.ts @@ -108,7 +108,7 @@ export default class BookSearchPlugin extends Plugin { async openBookSearchModal(query = ''): Promise { return new Promise((resolve, reject) => { - new BookSearchModal(this.app, query, (error, results) => { + new BookSearchModal(this, query, (error, results) => { if (error) return reject(error); new BookSuggestModal(this.app, results, (error2, selectedBook) => { if (error2) return reject(error2); diff --git a/src/views/book_search_modal.ts b/src/views/book_search_modal.ts index 711e15a..9f1d6e2 100644 --- a/src/views/book_search_modal.ts +++ b/src/views/book_search_modal.ts @@ -1,17 +1,20 @@ -import { App, ButtonComponent, Modal, Setting, TextComponent } from 'obsidian'; -import { GoogleBooksApi } from '@apis/google_books_api'; +import { ButtonComponent, Modal, Setting, TextComponent } from 'obsidian'; import { Book } from '@models/book.model'; +import { BaseBooksApiImpl, getServiceProvider } from '@apis/base_api'; +import BookSearchPlugin from '@src/main'; export class BookSearchModal extends Modal { query: string; isBusy: boolean; okBtnRef: ButtonComponent; onSubmit: (err: Error, result?: Book[]) => void; + serviceProvider: BaseBooksApiImpl; - constructor(app: App, query: string, onSubmit?: (err: Error, result?: Book[]) => void) { - super(app); + constructor(context: BookSearchPlugin, query: string, onSubmit?: (err: Error, result?: Book[]) => void) { + super(context.app); this.query = query; this.onSubmit = onSubmit; + this.serviceProvider = getServiceProvider(context.settings); } async searchBook() { @@ -24,7 +27,7 @@ export class BookSearchModal extends Modal { this.isBusy = true; this.okBtnRef.setDisabled(false); this.okBtnRef.setButtonText('Requesting...'); - const searchResults = await new GoogleBooksApi().getByQuery(this.query); + const searchResults = await this.serviceProvider.getByQuery(this.query); this.onSubmit(null, searchResults); } catch (err) {