File size: 2,489 Bytes
62862ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
from fastapi import FastAPI, Header, status
from fastapi.middleware.cors import CORSMiddleware
from supabase import create_client, Client
from decouple import config
from schema import WalletSchema
from pydantic import BaseModel
from authcheck import auth_check


url = os.environ.get('SUPABASE_URL')
key = os.environ.get('SUPABASE_KEY')

app = FastAPI()
supabase: Client = create_client(url, key) 

origins = ["*"]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],  # Allow all methods or be specific e.g., ["GET", "POST"]
    allow_headers=["*"],  # Allow all headers or be specific e.g., ["Content-Type"]
)


@app.get("/")
async def read_root():
    return {"message": "Welcome to hushh's Wallet API"}



@app.get("/wallet/")
async def get_wallet(id: str = Header(None), authentication: str = Header(None)):
    """
    Use this endpoint to get wallet card information!

    Authorization token : Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
    """

    # Checking for authentication
    valid_auth = auth_check(authentication)
    if valid_auth.get("status") != 1:
        return valid_auth

    
    try:
        wallets = supabase.table('wallets').select('*').eq('id',id).execute()
        return wallets
    
    except:
        return {"message": f"An unexpected error occured for the specified ID '{id}'."}

class WalletSchema(BaseModel):
    id: int
    brand_name: str
    bg_image: str
    logo: str
    font_color: str
    bg_color: str

@app.post("/add_wallet/", status_code=status.HTTP_201_CREATED)
async def add_wallet(wallet: WalletSchema, authentication: str = Header(None)):
    """
    Use this endpoint to add wallet cards!

    Authorization token : Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
    """

    # Checking for authentication
    valid_auth = auth_check(authentication)
    if valid_auth.get("status") != 1:
        return valid_auth

    try:
        wallet = supabase.table('wallets').insert({
            "id": wallet.id,
            "brand_name": wallet.brand_name,
            "bg_image": wallet.bg_image,
            "logo": wallet.logo,
            "font_color": wallet.font_color,
            "bg_color": wallet.bg_color
        }).execute()

        return wallet
    
    except:
        return {"message": f"An unexpected error occured for the specified ID '{id}'."}




if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)