1
- import createClient from 'openapi-fetch' ;
2
- import { obsidianFetch } from 'src/utils/Utils' ;
1
+ import { requestUrl } from 'obsidian' ;
3
2
import type MediaDbPlugin from '../../main' ;
4
3
import { GameModel } from '../../models/GameModel' ;
5
4
import type { MediaTypeModel } from '../../models/MediaTypeModel' ;
6
5
import { MovieModel } from '../../models/MovieModel' ;
7
6
import { SeriesModel } from '../../models/SeriesModel' ;
8
7
import { MediaType } from '../../utils/MediaType' ;
9
8
import { APIModel } from '../APIModel' ;
10
- import type { paths } from '../schemas/OMDb' ;
9
+
10
+ interface ErrorResponse {
11
+ Response : 'False' ;
12
+ Error : string ;
13
+ }
11
14
12
15
type SearchResponse =
13
16
| {
@@ -21,10 +24,7 @@ type SearchResponse =
21
24
Type : string ;
22
25
} [ ] ;
23
26
}
24
- | {
25
- Response : 'False' ;
26
- Error : string ;
27
- } ;
27
+ | ErrorResponse ;
28
28
29
29
type IdResponse =
30
30
| {
@@ -53,10 +53,7 @@ type IdResponse =
53
53
Production : string ;
54
54
Website : string ;
55
55
}
56
- | {
57
- Response : 'False' ;
58
- Error : string ;
59
- } ;
56
+ | ErrorResponse ;
60
57
61
58
export class OMDbAPI extends APIModel {
62
59
plugin : MediaDbPlugin ;
@@ -84,26 +81,19 @@ export class OMDbAPI extends APIModel {
84
81
throw new Error ( `MDB | API key for ${ this . apiName } missing.` ) ;
85
82
}
86
83
87
- const client = createClient < paths > ( { baseUrl : 'https://www.omdbapi.com/' } ) ;
88
-
89
- const response = await client . GET ( '/?s' , {
90
- params : {
91
- query : {
92
- s : title ,
93
- apikey : this . plugin . settings . OMDbKey ,
94
- } ,
95
- } ,
96
- fetch : obsidianFetch ,
84
+ const response = await requestUrl ( {
85
+ url : `https://www.omdbapi.com/?s=${ encodeURIComponent ( title ) } &apikey=${ this . plugin . settings . OMDbKey } ` ,
86
+ method : 'GET' ,
97
87
} ) ;
98
88
99
- if ( response . response . status === 401 ) {
89
+ if ( response . status === 401 ) {
100
90
throw Error ( `MDB | Authentication for ${ this . apiName } failed. Check the API key.` ) ;
101
91
}
102
- if ( response . response . status !== 200 ) {
103
- throw Error ( `MDB | Received status code ${ response . response . status } from ${ this . apiName } .` ) ;
92
+ if ( response . status !== 200 ) {
93
+ throw Error ( `MDB | Received status code ${ response . status } from ${ this . apiName } .` ) ;
104
94
}
105
95
106
- const data = response . data as SearchResponse | undefined ;
96
+ const data = response . json as SearchResponse | undefined ;
107
97
108
98
if ( ! data ) {
109
99
throw Error ( `MDB | No data received from ${ this . apiName } .` ) ;
@@ -175,26 +165,19 @@ export class OMDbAPI extends APIModel {
175
165
throw Error ( `MDB | API key for ${ this . apiName } missing.` ) ;
176
166
}
177
167
178
- const client = createClient < paths > ( { baseUrl : 'https://www.omdbapi.com/' } ) ;
179
-
180
- const response = await client . GET ( '/?i' , {
181
- params : {
182
- query : {
183
- i : id ,
184
- apikey : this . plugin . settings . OMDbKey ,
185
- } ,
186
- } ,
187
- fetch : obsidianFetch ,
168
+ const response = await requestUrl ( {
169
+ url : `https://www.omdbapi.com/?i=${ encodeURIComponent ( id ) } &apikey=${ this . plugin . settings . OMDbKey } ` ,
170
+ method : 'GET' ,
188
171
} ) ;
189
172
190
- if ( response . response . status === 401 ) {
173
+ if ( response . status === 401 ) {
191
174
throw Error ( `MDB | Authentication for ${ this . apiName } failed. Check the API key.` ) ;
192
175
}
193
- if ( response . response . status !== 200 ) {
194
- throw Error ( `MDB | Received status code ${ response . response . status } from ${ this . apiName } .` ) ;
176
+ if ( response . status !== 200 ) {
177
+ throw Error ( `MDB | Received status code ${ response . status } from ${ this . apiName } .` ) ;
195
178
}
196
179
197
- const result = response . data as IdResponse | undefined ;
180
+ const result = response . json as IdResponse | undefined ;
198
181
199
182
if ( ! result ) {
200
183
throw Error ( `MDB | No data received from ${ this . apiName } .` ) ;
0 commit comments