File size: 1,827 Bytes
6104dd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from supabase import create_client, Client
from PIL import Image
from io import BytesIO
import requests
import os


# function to add watermark to images
def addWatermark(background: Image.Image, logo: Image.Image) -> Image.Image:
    background = background.convert("RGBA")
    logo = logo.convert("RGBA").resize((int(0.08 * background.size[0]), int(0.08 * background.size[0])))
    background.paste(logo, (10, background.size[1] - logo.size[1] - 10), logo)
    return background


# function to download an image from url and return as bytes objects
def returnBytesData(url: str) -> BytesIO:
    response = requests.get(url)
    return BytesIO(response.content)


# function to get public URLs of paths
def supabaseGetPublicURL(path: str) -> str:
    url_string = f"https://lvuhhlrkcuexzqtsbqyu.supabase.co/storage/v1/object/public/Stores/{path}"
    return url_string.replace(" ", "%20")


# function to deduct credit
def deductAndTrackCredit(storename: str, endpoint: str) -> str:
    url: str = os.environ["SUPABASE_URL"]
    key: str = os.environ["SUPABASE_KEY"]
    supabase: Client = create_client(url, key)
    current, _ = supabase.table('ClientConfig').select('CreditBalance').eq("StoreName", f"{storename}").execute()
    if current[1] == []:
        return "Not Found"
    else:
        current = current[1][0]["CreditBalance"]
        if current > 0:
            data, _ = supabase.table('ClientConfig').update({'CreditBalance': current - 1}).eq("StoreName",
                                                                                               f"{storename}").execute()
            data, _ = supabase.table('UsageHistory').insert(
                {'StoreName': f"{storename}", 'APIEndpoint': f"{endpoint}"}).execute()
            return "Success"
        else:
            return "No Credits Available"