code quality fixes and cleanup

This commit is contained in:
Riccardo Balbo 2024-09-07 09:09:22 +00:00
commit 2bbbcd4d65
32 changed files with 4943 additions and 1368 deletions

View file

@ -1,54 +1,52 @@
# Data models for your extension
from sqlite3 import Row
from pydantic import BaseModel, Field
import time
import json
from typing import List, Dict, Any
from sqlite3 import Row
from typing import Any, Dict, List
from pydantic import BaseModel
class NWCKey(BaseModel):
pubkey: str
pubkey: str
wallet: str
description: str
description: str
expires_at: int
permissions: str
created_at: int
last_used: int
def getPermissions(cls) -> List[str]:
try:
return cls.permissions.split(" ")
except:
return []
created_at: int
last_used: int
def get_permissions(self) -> List[str]:
try:
return self.permissions.split(" ")
except Exception:
return []
@classmethod
def from_row(cls, row: Dict[str, Any]) -> "NWCKey":
return cls(**row)
class NWCBudget(BaseModel):
id: int
pubkey: str
budget_msats: int
refresh_window: int
created_at: int
id: int
pubkey: str
budget_msats: int
refresh_window: int
created_at: int
used_budget_msats: int = 0
def get_timestamp_range(cls):
def get_timestamp_range(self) -> tuple[int, int]:
c = int(time.time())
if cls.refresh_window <= 0: # never refresh
if self.refresh_window <= 0: # never refresh
# return a timestamp in the future
return c + 21000000
# calculate the next refresh timestamp
elapsed = c - cls.created_at
passed_cycles = elapsed // cls.refresh_window
last_cycle = cls.created_at + (passed_cycles * cls.refresh_window)
next_cycle = last_cycle + cls.refresh_window
return c, c + 21000000
# calculate the next refresh timestamp
elapsed = c - self.created_at
passed_cycles = elapsed // self.refresh_window
last_cycle = self.created_at + (passed_cycles * self.refresh_window)
next_cycle = last_cycle + self.refresh_window
return last_cycle, next_cycle
@classmethod
def from_row(cls, row: Row) -> "NWCBudget":
return cls(**dict(row))
@ -81,5 +79,3 @@ class NWCRegistrationRequest(BaseModel):
class NWCGetResponse(BaseModel):
data: NWCKey
budgets: List[NWCBudget]