Skip to content

Commit

Permalink
Supabase API
Browse files Browse the repository at this point in the history
  • Loading branch information
mouredev committed Feb 15, 2024
1 parent 07947ea commit b0c1667
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 46 deletions.
17 changes: 17 additions & 0 deletions link_bio/link_bio/api/ConfigCatAPI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
import dotenv
import configcatclient


class ConfigCatAPI:

dotenv.load_dotenv()

CONFIGCAT_SDK_KEY = os.environ.get("CONFIGCAT_SDK_KEY")

def __init__(self) -> None:
if self.CONFIGCAT_SDK_KEY != None:
self.configcat = configcatclient.get(self.CONFIGCAT_SDK_KEY)

def schedule(self) -> str:
self.configcat.get_value("live_schedule", "")
11 changes: 9 additions & 2 deletions link_bio/link_bio/api/SupabaseAPI.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import dotenv
from supabase import create_client, Client
from link_bio.model.Featured import Featured


class SupabaseAPI:
Expand All @@ -16,14 +17,20 @@ def __init__(self) -> None:
self.SUPABASE_URL, self.SUPABASE_KEY
)

def featured(self) -> list:
def featured(self) -> list[Featured]:

response = self.supabase.table("featured").select("*").execute()

featured_data = []

if len(response.data) > 0:
for featured_item in response.data:
featured_data.append(featured_item)
featured_data.append(
Featured(
title=featured_item["title"],
image=featured_item["image"],
url=featured_item["url"]
)
)

return featured_data
7 changes: 4 additions & 3 deletions link_bio/link_bio/api/TwitchAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import dotenv
import requests
import time
from link_bio.model.Live import Live


class TwitchAPI:
Expand Down Expand Up @@ -37,7 +38,7 @@ def generate_token(self):
def token_valid(self) -> bool:
return time.time() < self.token_exp

def live(self, user: str) -> dict:
def live(self, user: str) -> Live:

if not self.token_valid():
self.generate_token()
Expand All @@ -52,6 +53,6 @@ def live(self, user: str) -> dict:

if response.status_code == 200 and response.json()["data"]:
data = response.json()["data"]
return {"live": True, "title": data[0]["title"]}
return Live(live=True, title=data[0]["title"])

return {"live": False, "title": ""}
return Live(live=False, title="")
6 changes: 4 additions & 2 deletions link_bio/link_bio/api/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import link_bio.constants as const
from link_bio.model.Featured import Featured
from link_bio.model.Live import Live
from .TwitchAPI import TwitchAPI
from .SupabaseAPI import SupabaseAPI

Expand All @@ -10,9 +12,9 @@ async def repo() -> str:
return const.REPO_URL


async def live(user: str) -> dict:
async def live(user: str) -> Live:
return TWITCH_API.live(user)


async def featured() -> list:
async def featured() -> list[Featured]:
return SUPABASE_API.featured()
23 changes: 23 additions & 0 deletions link_bio/link_bio/components/featured_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import reflex as rx
import link_bio.styles.styles as styles
from link_bio.styles.styles import Size
from link_bio.model.Featured import Featured


def featured_link(featured: Featured) -> rx.Component:
return rx.link(
rx.vstack(
rx.image(
src=featured.image,
border_radius=Size.DEFAULT.value
),
rx.text(
featured.title,
style=styles.button_body_style
),
spacing=Size.SMALL.value,
align_items="start"
),
href=featured.url,
is_external=True
)
7 changes: 7 additions & 0 deletions link_bio/link_bio/model/Featured.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import reflex as rx


class Featured(rx.Base):
title: str
image: str
url: str
6 changes: 6 additions & 0 deletions link_bio/link_bio/model/Live.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import reflex as rx


class Live(rx.Base):
live: bool
title: str
2 changes: 1 addition & 1 deletion link_bio/link_bio/pages/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def courses() -> rx.Component:
rx.vstack(
header(
False,
PageState.is_live
live_status=PageState.live_status
),
courses_links(),
sponsors(),
Expand Down
5 changes: 1 addition & 4 deletions link_bio/link_bio/pages/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ def index() -> rx.Component:
navbar(),
rx.center(
rx.vstack(
header(
live=PageState.is_live,
live_title=PageState.live_title
),
header(live_status=PageState.live_status),
index_links(PageState.featured_info),
sponsors(),
max_width=styles.MAX_WIDTH,
Expand Down
12 changes: 6 additions & 6 deletions link_bio/link_bio/state/PageState.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from enum import Flag
import reflex as rx
from link_bio.api.api import live, featured
from link_bio.model.Featured import Featured
from link_bio.model.Live import Live

USER = "mouredev"


class PageState(rx.State):

is_live: bool
live_title: str
featured_info: list[dict]
live_status = Live(live=False, title="")
featured_info: list[Featured]

async def check_live(self):
live_data = await live(USER)
self.is_live = live_data["live"]
self.live_title = live_data["title"]
self.live_status = await live(USER)

async def featured_links(self):
self.featured_info = await featured()
9 changes: 5 additions & 4 deletions link_bio/link_bio/views/header.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import reflex as rx
import datetime
import link_bio.constants as const
from link_bio.model.Live import Live
from link_bio.styles.styles import Size
from link_bio.styles.colors import Color, TextColor
from link_bio.components.link_icon import link_icon
from link_bio.components.info_text import info_text
from link_bio.components.link_button import link_button


def header(details=True, live=False, live_title="") -> rx.Component:
def header(details=True, live_status=Live(live=False, title="")) -> rx.Component:
return rx.vstack(
rx.hstack(
rx.avatar(
rx.cond(
live,
live_status.live,
rx.link(
rx.avatar_badge(
rx.image(
Expand Down Expand Up @@ -102,10 +103,10 @@ def header(details=True, live=False, live_title="") -> rx.Component:
width="100%"
),
rx.cond(
live,
live_status.live,
link_button(
"En directo",
live_title,
live_status.title,
"/icons/twitch.svg",
const.TWITCH_URL,
highlight_color=Color.PURPLE.value
Expand Down
41 changes: 18 additions & 23 deletions link_bio/link_bio/views/index_links.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import reflex as rx
import link_bio.constants as const
from link_bio.components.featured_link import featured_link
from link_bio.model.Featured import Featured
from link_bio.routes import Route
from link_bio.components.link_button import link_button
from link_bio.components.title import title
from link_bio.styles.styles import Size, Color


def index_links(featured=[]) -> rx.Component:
def index_links(featured: list[Featured]) -> rx.Component:
return rx.vstack(
title("Comunidad"),
link_button(
Expand Down Expand Up @@ -42,28 +44,21 @@ def index_links(featured=[]) -> rx.Component:
const.YOUTUBE_SECONDARY_URL
),

# rx.cond(
# len(featured) > 0,
# rx.vstack(
# title("Destacado"),
# rx.foreach(
# featured,
# lambda item: rx.responsive_grid(
# rx.text(item)
# rx.link(
# rx.image(
# src=item["image"]
# ),
# rx.text(
# item["title"]
# ),
# href=item["url"],
# is_external=True
# )
# )
# )
# )
# ),
rx.cond(
featured,
rx.vstack(
title("Destacado"),
rx.responsive_grid(
rx.foreach(
featured,
featured_link
),
columns=[1, 2],
spacing=Size.DEFAULT.value
),
spacing=Size.DEFAULT.value
)
),

title("Recursos y más"),
link_button(
Expand Down
3 changes: 2 additions & 1 deletion link_bio/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
reflex==0.3.10
python-dotenv
supabase
supabase
configcat-client

0 comments on commit b0c1667

Please sign in to comment.