1- import { Image , Size } from "./model" ;
2-
3- // CONSTANTS
4- // const API_URL = "http://127.0.0.1:8000";
5- const API_URL = "https://rex-api-505972842640.europe-west2.run.app" ;
6-
7- const THUMBNAIL_BUCKET = "rex-thumbnails" ;
1+ import { API_URL , Image , Size , THUMBNAIL_BUCKET , TokenResponse } from "./model" ;
82
93// UTILITY FUNCS
104export const getThumbnailURL = ( image_name : string ) : string => {
@@ -17,17 +11,36 @@ export const getThumbnailURL = (image_name: string): string => {
1711 * Wake's up the API.
1812 */
1913export async function wakeUp ( ) {
20- try {
21- const response = await fetch ( `${ API_URL } /` ) ;
22- if ( ! response . ok ) {
23- throw new Error ( `api did not wake up!` ) ;
24- }
25- } catch ( error ) {
26- console . log ( error ) ;
27- throw error ;
14+ const response = await fetch ( `${ API_URL } /` ) ;
15+ if ( ! response . ok ) {
16+ console . error ( `Rex API did not wake up... ${ response . status } ` ) ;
2817 }
2918}
3019
20+ // - AUTH
21+
22+ /**
23+ * Authenticates and fetches Bearer Token.
24+ *
25+ * @param {FormData } authForm
26+ *
27+ * @returns {TokenResponse } Token Response.
28+ */
29+ export async function getToken ( authForm : FormData ) : Promise < TokenResponse > {
30+ const response = await fetch ( `${ API_URL } /token` , {
31+ method : "POST" ,
32+ body : authForm ,
33+ } ) ;
34+
35+ if ( ! response . ok ) {
36+ throw new Error ( `Fetching Token failed | status: ${ response . status } ` ) ;
37+ }
38+
39+ return await response . json ( ) ;
40+ }
41+
42+ // - PHOTOGRAPHY
43+
3144/**
3245 * Fetches a photo from a given album.
3346 *
@@ -37,22 +50,63 @@ export async function wakeUp() {
3750 * @returns {Image } The desired photo.
3851 */
3952export async function getPhoto (
53+ token : string ,
4054 image_name : string ,
41- size : Size = Size . LARGE
55+ size : Size = Size . MEDIUM ,
4256) : Promise < Image > {
4357 const encoded_path = encodeURI ( `${ image_name } ?size=${ size } ` ) ;
4458 const url = `${ API_URL } /photography/${ encoded_path } ` ;
4559
46- try {
47- const response = await fetch ( url ) ;
48- if ( ! response . ok ) {
49- throw new Error (
50- `failed to fetch photo '${ image_name } ' | status: ${ response . status } `
51- ) ;
52- }
53- return await response . json ( ) ;
54- } catch ( error ) {
55- console . log ( error ) ;
56- throw error ;
60+ const response = await fetch ( url , {
61+ headers : { Authorization : `Bearer ${ token } ` } ,
62+ } ) ;
63+
64+ if ( response . status == 401 ) {
65+ throw new Error ( `Fetching Photo failed | unauthenticated` ) ;
66+ } else if ( ! response . ok ) {
67+ throw new Error (
68+ `failed to fetch photo '${ image_name } ' | status: ${ response . status } ` ,
69+ ) ;
5770 }
71+
72+ return await response . json ( ) ;
73+ }
74+
75+ /**
76+ * Downloads a photo.
77+ *
78+ * @param {string } image_name
79+ * @param {Size } [size=Size.MEDIUM] Optional
80+ *
81+ * @returns {Image } The desired photo.
82+ */
83+ export async function downloadPhoto (
84+ token : string ,
85+ image_name : string ,
86+ size : Size = Size . MEDIUM ,
87+ ) : Promise < void > {
88+ const encoded_path = encodeURI ( `${ image_name } /download?size=${ size } ` ) ;
89+ const url = `${ API_URL } /photography/${ encoded_path } ` ;
90+
91+ const response = await fetch ( url , {
92+ headers : { Authorization : `Bearer ${ token } ` } ,
93+ } ) ;
94+
95+ if ( response . status == 401 ) {
96+ throw new Error ( `Fetching Photo failed | unauthenticated` ) ;
97+ } else if ( ! response . ok ) {
98+ throw new Error (
99+ `failed to download photo '${ image_name } ' | status: ${ response . status } ` ,
100+ ) ;
101+ }
102+
103+ const blob = await response . blob ( ) ;
104+ const blobUrl = URL . createObjectURL ( blob ) ;
105+ const link = document . createElement ( "a" ) ;
106+ link . href = blobUrl ;
107+ link . download = image_name ;
108+ document . body . appendChild ( link ) ;
109+ link . click ( ) ;
110+ document . body . removeChild ( link ) ;
111+ URL . revokeObjectURL ( blobUrl ) ;
58112}
0 commit comments