11import base64
2+ import os
23import sqlite3
34from io import BytesIO
4- from fastapi import FastAPI
5+ from fastapi import FastAPI , HTTPException , Security , Depends
6+ from fastapi .security .api_key import APIKeyHeader
57from fastapi .staticfiles import StaticFiles
68from pathlib import Path
79from PIL import Image
10+ from starlette import status
811from _version import __version__
912
1013
1114DATA_PATH = 'data'
1215MAX_PREVIEW_IMAGE_HEIGHT = 54 * 4
1316
17+ API_KEY_NAME = "access_token"
18+ api_key_header = APIKeyHeader (name = API_KEY_NAME , auto_error = False )
19+ API_KEY = os .getenv ("API_KEY" )
20+
1421app = FastAPI (
1522 docs_url = None ,
1623 redoc_url = None ,
1724 openapi_url = None ,
1825)
1926
2027
28+ async def get_api_key (header_key : str = Security (api_key_header )):
29+ print (header_key )
30+ if header_key == API_KEY :
31+ return header_key
32+ raise HTTPException (
33+ status_code = status .HTTP_403_FORBIDDEN ,
34+ detail = "Could not validate API Key"
35+ )
36+
37+
2138def sqlite_connect (file ):
2239 file_uri = f"file:{ DATA_PATH } /{ file } ?mode=ro"
2340 return sqlite3 .connect (file_uri , uri = True )
@@ -28,7 +45,7 @@ def version():
2845 return __version__
2946
3047
31- @app .get ("/api/filelist" )
48+ @app .get ("/api/filelist" , dependencies = [ Depends ( get_api_key )] )
3249def filelist ():
3350 root = Path (DATA_PATH ).resolve ()
3451 db_files = []
@@ -42,7 +59,7 @@ def filelist():
4259 return db_files
4360
4461
45- @app .get ("/api/coins" )
62+ @app .get ("/api/coins" , dependencies = [ Depends ( get_api_key )] )
4663def coins (f , search = None , sort = None , reverse : bool = False , status_filter = None , country_filter = None , series_filter = None , type_filter = None ,
4764 period_filter = None , mint_filter = None ):
4865 file = f
@@ -97,7 +114,7 @@ def coins(f, search=None, sort=None, reverse: bool = False, status_filter=None,
97114 return data
98115
99116
100- @app .get ("/api/images" )
117+ @app .get ("/api/images" , dependencies = [ Depends ( get_api_key )] )
101118def coins (f ):
102119 file = f
103120 con = sqlite_connect (file )
@@ -120,7 +137,7 @@ def coins(f):
120137 return data
121138
122139
123- @app .get ("/api/filters" )
140+ @app .get ("/api/filters" , dependencies = [ Depends ( get_api_key )] )
124141def filters (f ):
125142 file = f
126143 con = sqlite_connect (file )
@@ -141,7 +158,7 @@ def filters(f):
141158 return result
142159
143160
144- @app .get ("/api/coin_data" )
161+ @app .get ("/api/coin_data" , dependencies = [ Depends ( get_api_key )] )
145162def coin_data (f , id ):
146163 info_fields = ('coins.title' , 'obverseimg.image' , 'reverseimg.image' ,
147164 'status' , 'region' , 'country' , 'period' , 'ruler' , 'value' , 'unit' , 'type' ,
@@ -170,7 +187,7 @@ def coin_data(f, id):
170187 return result
171188
172189
173- @app .get ("/api/photo" )
190+ @app .get ("/api/photo" , dependencies = [ Depends ( get_api_key )] )
174191def photo (f , id , type ):
175192 file = f
176193 coin_id = id
@@ -238,7 +255,7 @@ def photo(f, id, type):
238255 return result
239256
240257
241- @app .get ("/api/photos" )
258+ @app .get ("/api/photos" , dependencies = [ Depends ( get_api_key )] )
242259def photos (f , id ):
243260 file = f
244261 coin_id = id
@@ -266,7 +283,7 @@ def photos(f, id):
266283 return result
267284
268285
269- @app .get ("/api/settings" )
286+ @app .get ("/api/settings" , dependencies = [ Depends ( get_api_key )] )
270287def settings (f ):
271288 field_ids = {
272289 13 : 'status' ,
@@ -332,7 +349,7 @@ def settings(f):
332349 return collection_settings
333350
334351
335- @app .get ("/api/summary" )
352+ @app .get ("/api/summary" , dependencies = [ Depends ( get_api_key )] )
336353def summary (f ):
337354 collection_summary = {}
338355
0 commit comments