Skip to content

Commit 4366636

Browse files
committed
fix omdb and id search
1 parent 1cd1b63 commit 4366636

File tree

5 files changed

+24
-490
lines changed

5 files changed

+24
-490
lines changed

automation/fetchSchemas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ async function fetchSchema() {
88
await $('bun openapi-typescript ./src/api/schemas/GiantBomb.json -o ./src/api/schemas/GiantBomb.ts');
99

1010
// https://www.omdbapi.com/swagger.json
11-
await $('bun openapi-typescript ./src/api/schemas/OMDb.json -o ./src/api/schemas/OMDb.ts');
11+
// await $('bun openapi-typescript ./src/api/schemas/OMDb.json -o ./src/api/schemas/OMDb.ts');
1212

1313
// https://github.com/internetarchive/openlibrary-api/blob/main/swagger.yaml
1414
await $('bun openapi-typescript ./src/api/schemas/OpenLibrary.json -o ./src/api/schemas/OpenLibrary.ts');

src/api/apis/OMDbAPI.ts

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import createClient from 'openapi-fetch';
2-
import { obsidianFetch } from 'src/utils/Utils';
1+
import { requestUrl } from 'obsidian';
32
import type MediaDbPlugin from '../../main';
43
import { GameModel } from '../../models/GameModel';
54
import type { MediaTypeModel } from '../../models/MediaTypeModel';
65
import { MovieModel } from '../../models/MovieModel';
76
import { SeriesModel } from '../../models/SeriesModel';
87
import { MediaType } from '../../utils/MediaType';
98
import { APIModel } from '../APIModel';
10-
import type { paths } from '../schemas/OMDb';
9+
10+
interface ErrorResponse {
11+
Response: 'False';
12+
Error: string;
13+
}
1114

1215
type SearchResponse =
1316
| {
@@ -21,10 +24,7 @@ type SearchResponse =
2124
Type: string;
2225
}[];
2326
}
24-
| {
25-
Response: 'False';
26-
Error: string;
27-
};
27+
| ErrorResponse;
2828

2929
type IdResponse =
3030
| {
@@ -53,10 +53,7 @@ type IdResponse =
5353
Production: string;
5454
Website: string;
5555
}
56-
| {
57-
Response: 'False';
58-
Error: string;
59-
};
56+
| ErrorResponse;
6057

6158
export class OMDbAPI extends APIModel {
6259
plugin: MediaDbPlugin;
@@ -84,26 +81,19 @@ export class OMDbAPI extends APIModel {
8481
throw new Error(`MDB | API key for ${this.apiName} missing.`);
8582
}
8683

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',
9787
});
9888

99-
if (response.response.status === 401) {
89+
if (response.status === 401) {
10090
throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
10191
}
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}.`);
10494
}
10595

106-
const data = response.data as SearchResponse | undefined;
96+
const data = response.json as SearchResponse | undefined;
10797

10898
if (!data) {
10999
throw Error(`MDB | No data received from ${this.apiName}.`);
@@ -175,26 +165,19 @@ export class OMDbAPI extends APIModel {
175165
throw Error(`MDB | API key for ${this.apiName} missing.`);
176166
}
177167

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',
188171
});
189172

190-
if (response.response.status === 401) {
173+
if (response.status === 401) {
191174
throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
192175
}
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}.`);
195178
}
196179

197-
const result = response.data as IdResponse | undefined;
180+
const result = response.json as IdResponse | undefined;
198181

199182
if (!result) {
200183
throw Error(`MDB | No data received from ${this.apiName}.`);

src/api/schemas/OMDb.json

Lines changed: 0 additions & 269 deletions
This file was deleted.

0 commit comments

Comments
 (0)