1- import { requestUrl } from 'obsidian' ;
1+ import createClient from 'openapi-fetch' ;
2+ import { obsidianFetch } from 'src/utils/Utils' ;
23import type MediaDbPlugin from '../../main' ;
34import { GameModel } from '../../models/GameModel' ;
45import type { MediaTypeModel } from '../../models/MediaTypeModel' ;
56import { MediaType } from '../../utils/MediaType' ;
67import { APIModel } from '../APIModel' ;
8+ import type { paths } from '../schemas/GiantBomb' ;
79
810export class GiantBombAPI extends APIModel {
911 plugin : MediaDbPlugin ;
@@ -18,42 +20,50 @@ export class GiantBombAPI extends APIModel {
1820 this . apiUrl = 'https://www.giantbomb.com/api' ;
1921 this . types = [ MediaType . Game ] ;
2022 }
23+
2124 async searchByTitle ( title : string ) : Promise < MediaTypeModel [ ] > {
2225 console . log ( `MDB | api "${ this . apiName } " queried by Title` ) ;
2326
2427 if ( ! this . plugin . settings . GiantBombKey ) {
2528 throw Error ( `MDB | API key for ${ this . apiName } missing.` ) ;
2629 }
2730
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 ,
3142 } ) ;
3243
33- // console.debug(fetchData);
34-
35- if ( fetchData . status === 401 ) {
44+ if ( response . response . status === 401 ) {
3645 throw Error ( `MDB | Authentication for ${ this . apiName } failed. Check the API key.` ) ;
3746 }
38- if ( fetchData . status === 429 ) {
47+ if ( response . response . status === 429 ) {
3948 throw Error ( `MDB | Too many requests for ${ this . apiName } , you've exceeded your API quota.` ) ;
4049 }
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 } .` ) ;
4352 }
4453
45- const data = await fetchData . json ;
46- // console.debug(data);
54+ const data = response . data ?. results ;
55+
4756 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+
4960 ret . push (
5061 new GameModel ( {
51- type : MediaType . Game ,
5262 title : result . name ,
5363 englishTitle : result . name ,
54- year : new Date ( result . original_release_date ) . getFullYear ( ) . toString ( ) ,
64+ year : year ,
5565 dataSource : this . apiName ,
56- id : result . guid ,
66+ id : result . guid ?. toString ( ) ,
5767 } ) ,
5868 ) ;
5969 }
@@ -68,36 +78,79 @@ export class GiantBombAPI extends APIModel {
6878 throw Error ( `MDB | API key for ${ this . apiName } missing.` ) ;
6979 }
7080
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 ,
7493 } ) ;
75- console . debug ( fetchData ) ;
7694
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 } .` ) ;
79109 }
80110
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 ;
84137
85138 return new GameModel ( {
86139 type : MediaType . Game ,
87140 title : result . name ,
88141 englishTitle : result . name ,
89- year : new Date ( result . original_release_date ) . getFullYear ( ) . toString ( ) ,
142+ year : year ,
90143 dataSource : this . apiName ,
91144 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 ) ,
96149 onlineRating : 0 ,
97- image : result . image ?. super_url ?? '' ,
150+ image : image ?. super_url ,
98151
99152 released : true ,
100- releaseDate : result . original_release_date ?? 'unknown' ,
153+ releaseDate : result . original_release_date ,
101154
102155 userData : {
103156 played : false ,
@@ -107,6 +160,6 @@ export class GiantBombAPI extends APIModel {
107160 } ) ;
108161 }
109162 getDisabledMediaTypes ( ) : MediaType [ ] {
110- return this . plugin . settings . GiantBombAPI_disabledMediaTypes as MediaType [ ] ;
163+ return this . plugin . settings . GiantBombAPI_disabledMediaTypes ;
111164 }
112165}
0 commit comments