1
- import { requestUrl } from 'obsidian' ;
1
+ import createClient from 'openapi-fetch' ;
2
+ import { obsidianFetch } from 'src/utils/Utils' ;
2
3
import type MediaDbPlugin from '../../main' ;
3
4
import { GameModel } from '../../models/GameModel' ;
4
5
import type { MediaTypeModel } from '../../models/MediaTypeModel' ;
5
6
import { MediaType } from '../../utils/MediaType' ;
6
7
import { APIModel } from '../APIModel' ;
8
+ import type { paths } from '../schemas/GiantBomb' ;
7
9
8
10
export class GiantBombAPI extends APIModel {
9
11
plugin : MediaDbPlugin ;
@@ -18,42 +20,50 @@ export class GiantBombAPI extends APIModel {
18
20
this . apiUrl = 'https://www.giantbomb.com/api' ;
19
21
this . types = [ MediaType . Game ] ;
20
22
}
23
+
21
24
async searchByTitle ( title : string ) : Promise < MediaTypeModel [ ] > {
22
25
console . log ( `MDB | api "${ this . apiName } " queried by Title` ) ;
23
26
24
27
if ( ! this . plugin . settings . GiantBombKey ) {
25
28
throw Error ( `MDB | API key for ${ this . apiName } missing.` ) ;
26
29
}
27
30
28
- const searchUrl = `${ this . apiUrl } /games?api_key=${ this . plugin . settings . GiantBombKey } &filter=name:${ encodeURIComponent ( title ) } &format=json` ;
29
- const fetchData = await requestUrl ( {
30
- url : searchUrl ,
31
+ const client = createClient < paths > ( { baseUrl : 'https://www.giantbomb.com/api/' } ) ;
32
+ const response = await client . GET ( '/games' , {
33
+ params : {
34
+ query : {
35
+ api_key : this . plugin . settings . GiantBombKey ,
36
+ filter : `name:${ encodeURIComponent ( title ) } ` ,
37
+ format : 'json' ,
38
+ limit : 20 ,
39
+ } ,
40
+ } ,
41
+ fetch : obsidianFetch ,
31
42
} ) ;
32
43
33
- // console.debug(fetchData);
34
-
35
- if ( fetchData . status === 401 ) {
44
+ if ( response . response . status === 401 ) {
36
45
throw Error ( `MDB | Authentication for ${ this . apiName } failed. Check the API key.` ) ;
37
46
}
38
- if ( fetchData . status === 429 ) {
47
+ if ( response . response . status === 429 ) {
39
48
throw Error ( `MDB | Too many requests for ${ this . apiName } , you've exceeded your API quota.` ) ;
40
49
}
41
- if ( fetchData . status !== 200 ) {
42
- throw Error ( `MDB | Received status code ${ fetchData . status } from ${ this . apiName } .` ) ;
50
+ if ( response . response . status !== 200 ) {
51
+ throw Error ( `MDB | Received status code ${ response . response . status } from ${ this . apiName } .` ) ;
43
52
}
44
53
45
- const data = await fetchData . json ;
46
- // console.debug(data);
54
+ const data = response . data ?. results ;
55
+
47
56
const ret : MediaTypeModel [ ] = [ ] ;
48
- for ( const result of data . results ) {
57
+ for ( const result of data ?? [ ] ) {
58
+ const year = result . original_release_date ? new Date ( result . original_release_date ) . getFullYear ( ) . toString ( ) : undefined ;
59
+
49
60
ret . push (
50
61
new GameModel ( {
51
- type : MediaType . Game ,
52
62
title : result . name ,
53
63
englishTitle : result . name ,
54
- year : new Date ( result . original_release_date ) . getFullYear ( ) . toString ( ) ,
64
+ year : year ,
55
65
dataSource : this . apiName ,
56
- id : result . guid ,
66
+ id : result . guid ?. toString ( ) ,
57
67
} ) ,
58
68
) ;
59
69
}
@@ -68,36 +78,79 @@ export class GiantBombAPI extends APIModel {
68
78
throw Error ( `MDB | API key for ${ this . apiName } missing.` ) ;
69
79
}
70
80
71
- const searchUrl = `${ this . apiUrl } /game/${ encodeURIComponent ( id ) } /?api_key=${ this . plugin . settings . GiantBombKey } &format=json` ;
72
- const fetchData = await requestUrl ( {
73
- url : searchUrl ,
81
+ const client = createClient < paths > ( { baseUrl : 'https://www.giantbomb.com/api/' } ) ;
82
+ const response = await client . GET ( '/game/{guid}' , {
83
+ params : {
84
+ path : {
85
+ guid : id ,
86
+ } ,
87
+ query : {
88
+ api_key : this . plugin . settings . GiantBombKey ,
89
+ format : 'json' ,
90
+ } ,
91
+ } ,
92
+ fetch : obsidianFetch ,
74
93
} ) ;
75
- console . debug ( fetchData ) ;
76
94
77
- if ( fetchData . status !== 200 ) {
78
- throw Error ( `MDB | Received status code ${ fetchData . status } from ${ this . apiName } .` ) ;
95
+ if ( response . response . status === 401 ) {
96
+ throw Error ( `MDB | Authentication for ${ this . apiName } failed. Check the API key.` ) ;
97
+ }
98
+ if ( response . response . status === 429 ) {
99
+ throw Error ( `MDB | Too many requests for ${ this . apiName } , you've exceeded your API quota.` ) ;
100
+ }
101
+ if ( response . response . status !== 200 ) {
102
+ throw Error ( `MDB | Received status code ${ response . response . status } from ${ this . apiName } .` ) ;
103
+ }
104
+
105
+ const result = response . data ?. results ;
106
+
107
+ if ( ! result ) {
108
+ throw Error ( `MDB | No results found for ID ${ id } in ${ this . apiName } .` ) ;
79
109
}
80
110
81
- const data = await fetchData . json ;
82
- // console.debug(data);
83
- const result = data . results ;
111
+ console . log ( result ) ;
112
+
113
+ // sadly the only OpenAPI definition I could find doesn't have the right types
114
+ const year = result . original_release_date ? new Date ( result . original_release_date ) . getFullYear ( ) . toString ( ) : undefined ;
115
+ const developers = result . developers as
116
+ | {
117
+ name : string ;
118
+ } [ ]
119
+ | undefined ;
120
+ const publishers = result . publishers as
121
+ | {
122
+ name : string ;
123
+ } [ ]
124
+ | undefined ;
125
+ const genres = result . genres as
126
+ | {
127
+ name : string ;
128
+ } [ ]
129
+ | undefined ;
130
+ const image = result . image as
131
+ | {
132
+ small_url : string ;
133
+ medium_url : string ;
134
+ super_url : string ;
135
+ }
136
+ | undefined ;
84
137
85
138
return new GameModel ( {
86
139
type : MediaType . Game ,
87
140
title : result . name ,
88
141
englishTitle : result . name ,
89
- year : new Date ( result . original_release_date ) . getFullYear ( ) . toString ( ) ,
142
+ year : year ,
90
143
dataSource : this . apiName ,
91
144
url : result . site_detail_url ,
92
- id : result . guid ,
93
- developers : result . developers ?. map ( ( x : any ) => x . name ) ?? [ ] ,
94
- publishers : result . publishers ?. map ( ( x : any ) => x . name ) ?? [ ] ,
95
- genres : result . genres ?. map ( ( x : any ) => x . name ) ?? [ ] ,
145
+ id : result . guid ?. toString ( ) ,
146
+ developers : developers ?. map ( x => x . name ) ,
147
+ publishers : publishers ?. map ( x => x . name ) ,
148
+ genres : genres ?. map ( x => x . name ) ,
96
149
onlineRating : 0 ,
97
- image : result . image ?. super_url ?? '' ,
150
+ image : image ?. super_url ,
98
151
99
152
released : true ,
100
- releaseDate : result . original_release_date ?? 'unknown' ,
153
+ releaseDate : result . original_release_date ,
101
154
102
155
userData : {
103
156
played : false ,
@@ -107,6 +160,6 @@ export class GiantBombAPI extends APIModel {
107
160
} ) ;
108
161
}
109
162
getDisabledMediaTypes ( ) : MediaType [ ] {
110
- return this . plugin . settings . GiantBombAPI_disabledMediaTypes as MediaType [ ] ;
163
+ return this . plugin . settings . GiantBombAPI_disabledMediaTypes ;
111
164
}
112
165
}
0 commit comments