Endpoints working

This commit is contained in:
ben 2022-12-08 13:45:38 +00:00
parent 8e6eb63c9e
commit c239c4f552
8 changed files with 147 additions and 124 deletions

View file

@ -9,6 +9,16 @@ from lnbits.tasks import catch_everything_and_restart
db = Database("ext_gerty") db = Database("ext_gerty")
gerty_static_files = [
{
"path": "/gerty/static",
"app": StaticFiles(packages=[("lnbits", "extensions/gerty/static")]),
"name": "gerty_static",
}
]
gerty_ext: APIRouter = APIRouter(prefix="/gerty", tags=["Gerty"]) gerty_ext: APIRouter = APIRouter(prefix="/gerty", tags=["Gerty"])

View file

@ -81,6 +81,7 @@ async def delete_gerty(gerty_id: str) -> None:
async def get_mempool_info(endPoint: str, gerty) -> Optional[Mempool]: async def get_mempool_info(endPoint: str, gerty) -> Optional[Mempool]:
logger.debug(endPoint)
endpoints = MempoolEndpoint() endpoints = MempoolEndpoint()
url = "" url = ""
for endpoint in endpoints: for endpoint in endpoints:
@ -96,17 +97,21 @@ async def get_mempool_info(endPoint: str, gerty) -> Optional[Mempool]:
if not row: if not row:
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
response = await client.get(gerty.mempool_endpoint + url) response = await client.get(gerty.mempool_endpoint + url)
logger.debug(gerty.mempool_endpoint + url)
mempool_id = urlsafe_short_hash()
await db.execute( await db.execute(
""" """
INSERT INTO gerty.mempool ( INSERT INTO gerty.mempool (
id,
data, data,
endpoint, endpoint,
time, time,
mempool_endpoint mempool_endpoint
) )
VALUES (?, ?, ?, ?) VALUES (?, ?, ?, ?, ?)
""", """,
( (
mempool_id,
json.dumps(response.json()), json.dumps(response.json()),
endPoint, endPoint,
int(time.time()), int(time.time()),

View file

@ -80,7 +80,7 @@ async def get_mining_dashboard(gerty):
if isinstance(gerty.mempool_endpoint, str): if isinstance(gerty.mempool_endpoint, str):
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
# current hashrate # current hashrate
r = await get_mempool_info("get_hashrate_1w", gerty) r = await get_mempool_info("hashrate_1w", gerty)
data = r data = r
hashrateNow = data["currentHashrate"] hashrateNow = data["currentHashrate"]
hashrateOneWeekAgo = data["hashrates"][6]["avgHashrate"] hashrateOneWeekAgo = data["hashrates"][6]["avgHashrate"]

View file

@ -42,7 +42,8 @@ async def m004_initial(db):
await db.execute( await db.execute(
""" """
CREATE TABLE gerty.mempool ( CREATE TABLE gerty.mempool (
mempool_endpoint TEXT PRIMARY KEY, id TEXT PRIMARY KEY,
mempool_endpoint TEXT NOT NULL,
endpoint TEXT NOT NULL, endpoint TEXT NOT NULL,
data TEXT NOT NULL, data TEXT NOT NULL,
time TIMESTAMP time TIMESTAMP

View file

@ -10,6 +10,7 @@ class Gerty(BaseModel):
name: str name: str
refresh_time: int = Query(None) refresh_time: int = Query(None)
utc_offset: int = Query(None) utc_offset: int = Query(None)
wallet: str = Query(None)
type: str type: str
lnbits_wallets: str = Query( lnbits_wallets: str = Query(
None None
@ -39,6 +40,7 @@ class MempoolEndpoint(BaseModel):
class Mempool(BaseModel): class Mempool(BaseModel):
id: str = Query(None)
mempool_endpoint: str = Query(None) mempool_endpoint: str = Query(None)
endpoint: str = Query(None) endpoint: str = Query(None)
data: str = Query(None) data: str = Query(None)

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

View file

@ -1,80 +1,17 @@
<q-expansion-item <q-card-section>
group="extras" <p>
icon="swap_vertical_circle" Gerty (your bitcoin assistant): Use the software Gerty or
label="API info" <a
:content-inset-level="0.5" target="_blank"
> href="https://shop.lnbits.com/product/gerty-a-bitcoin-assistant"
<q-btn flat label="Swagger API" type="a" href="../docs#/gerty"></q-btn> >hardware Gerty</a
<q-expansion-item group="api" dense expand-separator label="List Gerty"> ><br />
<q-card> <small>
<q-card-section> Created by, <a href="https://github.com/blackcoffeexbt">Black Coffee</a>,
<code><span class="text-blue">GET</span> /gerty/api/v1/gertys</code> <a href="https://github.com/benarc">Ben Arc</a></small
<h5 class="text-caption q-mt-sm q-mb-none">Headers</h5> >
<code>{"X-Api-Key": &lt;invoice_key&gt;}</code><br /> </p>
<h5 class="text-caption q-mt-sm q-mb-none">Body (application/json)</h5> <a href="https://shop.lnbits.com/product/gerty-a-bitcoin-assistant"
<h5 class="text-caption q-mt-sm q-mb-none"> ><img src="/gerty/static/gerty.jpg"
Returns 200 OK (application/json) /></a>
</h5> </q-card-section>
<code>[&lt;gerty_object&gt;, ...]</code>
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code
>curl -X GET {{ request.base_url }}gerty/api/v1/gertys -H "X-Api-Key:
&lt;invoice_key&gt;"
</code>
</q-card-section>
</q-card>
</q-expansion-item>
<q-expansion-item group="api" dense expand-separator label="Create a Gerty">
<q-card>
<q-card-section>
<code><span class="text-green">POST</span> /gerty/api/v1/gertys</code>
<h5 class="text-caption q-mt-sm q-mb-none">Headers</h5>
<code>{"X-Api-Key": &lt;invoice_key&gt;}</code><br />
<h5 class="text-caption q-mt-sm q-mb-none">Body (application/json)</h5>
<code
>{"name": &lt;string&gt;, "currency": &lt;string*ie USD*&gt;}</code
>
<h5 class="text-caption q-mt-sm q-mb-none">
Returns 201 CREATED (application/json)
</h5>
<code
>{"currency": &lt;string&gt;, "id": &lt;string&gt;, "name":
&lt;string&gt;, "wallet": &lt;string&gt;}</code
>
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code
>curl -X POST {{ request.base_url }}gerty/api/v1/gertys -d '{"name":
&lt;string&gt;, "currency": &lt;string&gt;}' -H "Content-type:
application/json" -H "X-Api-Key: &lt;admin_key&gt;"
</code>
</q-card-section>
</q-card>
</q-expansion-item>
<q-expansion-item
group="api"
dense
expand-separator
label="Delete a Gerty"
class="q-pb-md"
>
<q-card>
<q-card-section>
<code
><span class="text-pink">DELETE</span>
/gerty/api/v1/gertys/&lt;gerty_id&gt;</code
>
<h5 class="text-caption q-mt-sm q-mb-none">Headers</h5>
<code>{"X-Api-Key": &lt;admin_key&gt;}</code><br />
<h5 class="text-caption q-mt-sm q-mb-none">Returns 204 NO CONTENT</h5>
<code></code>
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code
>curl -X DELETE {{ request.base_url
}}gerty/api/v1/gertys/&lt;gerty_id&gt; -H "X-Api-Key:
&lt;admin_key&gt;"
</code>
</q-card-section>
</q-card>
</q-expansion-item>
</q-expansion-item>

View file

@ -107,9 +107,21 @@
<div class="col-12 col-md-5 q-gutter-y-md"> <div class="col-12 col-md-5 q-gutter-y-md">
<q-card> <q-card>
<q-card-section> <q-card-section>
<h6 class="text-subtitle1 q-my-none"> <div class="row">
{{ SITE_TITLE }} Gerty extension <div class="col-9">
</h6> <h6 class="text-subtitle1 q-my-none">
{{ SITE_TITLE }} Gerty extension
</h6>
</div>
<div class="col-3">
<q-btn
flat
label="Swagger API"
type="a"
href="../docs#/gerty"
></q-btn>
</div>
</div>
</q-card-section> </q-card-section>
<q-card-section class="q-pa-none"> <q-card-section class="q-pa-none">
<q-separator></q-separator> <q-separator></q-separator>
@ -309,12 +321,50 @@
>The amount of time in seconds between screen updates >The amount of time in seconds between screen updates
</q-tooltip> </q-tooltip>
</q-input> </q-input>
<q-separator class="q-mb-md" v-if="toggleStates.advanced"></q-separator> <q-separator
<p class="q-pl-sm q-mb-xs" v-if="toggleStates.advanced">Gerty Device Dashboards (for Gerty <a target="_blank" href="https://shop.lnbits.com/product/gerty-a-bitcoin-assistant">devices</a>):</p> class="q-mb-md"
<q-checkbox class="q-pl-md" v-if="toggleStates.advanced" size="xs" v-model="formDialog.data.display_preferences.dashboard" val="xs" label="LNbits"></q-checkbox> v-if="toggleStates.advanced"
<q-checkbox class="q-pl-md" v-if="toggleStates.advanced" size="xs" v-model="formDialog.data.display_preferences.dashboard_onchain" val="xs" label="Onchain"></q-checkbox> ></q-separator>
<q-checkbox class="q-pl-md" v-if="toggleStates.advanced" size="xs" v-model="formDialog.data.display_preferences.dashboard_mining" val="xs" label="Mining"></q-checkbox> <p class="q-pl-sm q-mb-xs" v-if="toggleStates.advanced">
<q-checkbox class="q-pl-md" v-if="toggleStates.advanced" size="xs" v-model="formDialog.data.display_preferences.lightning_dashboard" val="xs" label="Lightning"></q-checkbox> Gerty Device Dashboards (for Gerty
<a
target="_blank"
href="https://shop.lnbits.com/product/gerty-a-bitcoin-assistant"
>devices</a
>):
</p>
<q-checkbox
class="q-pl-md"
v-if="toggleStates.advanced"
size="xs"
v-model="formDialog.data.display_preferences.dashboard"
val="xs"
label="LNbits"
></q-checkbox>
<q-checkbox
class="q-pl-md"
v-if="toggleStates.advanced"
size="xs"
v-model="formDialog.data.display_preferences.dashboard_onchain"
val="xs"
label="Onchain"
></q-checkbox>
<q-checkbox
class="q-pl-md"
v-if="toggleStates.advanced"
size="xs"
v-model="formDialog.data.display_preferences.dashboard_mining"
val="xs"
label="Mining"
></q-checkbox>
<q-checkbox
class="q-pl-md"
v-if="toggleStates.advanced"
size="xs"
v-model="formDialog.data.display_preferences.lightning_dashboard"
val="xs"
label="Lightning"
></q-checkbox>
</div> </div>
</div> </div>
@ -570,6 +620,8 @@
formDialog: { formDialog: {
show: false, show: false,
data: { data: {
type: 'Mini Gerty',
exchange: 'USD',
utc_offset: new Date().getTimezoneOffset(), utc_offset: new Date().getTimezoneOffset(),
display_preferences: { display_preferences: {
dashboard: false, dashboard: false,
@ -597,41 +649,44 @@
} }
} }
}, },
mounted() {
console.log('this.formDialog', this.formDialog.data.display_preferences)
},
methods: { methods: {
setAdvanced: function(){ setAdvanced: function () {
self = this self = this
self.formDialog.data.mempool_endpoint = 'https://mempool.space' self.formDialog.data.mempool_endpoint = 'https://mempool.space'
self.formDialog.data.refresh_time = 300 self.formDialog.data.refresh_time = 300
}, },
setWallets: function() { setWallets: function () {
self = this self = this
if(!self.formDialog.data.display_preferences.lnbits_wallets_balance){ if (!self.formDialog.data.display_preferences.lnbits_wallets_balance) {
self.formDialog.data.lnbits_wallets = [] self.formDialog.data.lnbits_wallets = []
} }
}, },
setOnchain: function() { setOnchain: function () {
self = this self = this
self.formDialog.data.display_preferences.onchain_difficulty_epoch_progress = self.toggleStates.onchain self.formDialog.data.display_preferences.onchain_difficulty_epoch_progress =
self.formDialog.data.display_preferences.onchain_difficulty_retarget_date = self.toggleStates.onchain self.toggleStates.onchain
self.formDialog.data.display_preferences.onchain_difficulty_blocks_remaining = !self.toggleStates.onchain self.formDialog.data.display_preferences.onchain_difficulty_retarget_date =
self.formDialog.data.display_preferences.onchain_difficulty_epoch_time_remaining = self.toggleStates.onchain self.toggleStates.onchain
self.formDialog.data.display_preferences.onchain_block_height = self.toggleStates.onchain self.formDialog.data.display_preferences.onchain_difficulty_blocks_remaining = !self
}, .toggleStates.onchain
setMining: function() { self.formDialog.data.display_preferences.onchain_difficulty_epoch_time_remaining =
self = this self.toggleStates.onchain
self.formDialog.data.display_preferences.mining_current_hash_rate = self.toggleStates.mining self.formDialog.data.display_preferences.onchain_block_height =
self.formDialog.data.display_preferences.mining_current_difficulty = self.toggleStates.mining self.toggleStates.onchain
}, },
setMining: function () {
self = this
self.formDialog.data.display_preferences.mining_current_hash_rate =
self.toggleStates.mining
self.formDialog.data.display_preferences.mining_current_difficulty =
self.toggleStates.mining
},
closeFormDialog: function () { closeFormDialog: function () {
this.formDialog.data = { this.formDialog.data = {
utc_offset: 0, utc_offset: 0,
lnbits_wallets: [], lnbits_wallets: [],
mempool_endpoint: 'https://mempool.space', mempool_endpoint: 'https://mempool.space',
refresh_time: 300, refresh_time: 300,
type: 'Gerty',
display_preferences: {} display_preferences: {}
} }
}, },
@ -651,7 +706,6 @@
}, },
updateformDialog: function (formId) { updateformDialog: function (formId) {
var gerty = _.findWhere(this.gertys, {id: formId}) var gerty = _.findWhere(this.gertys, {id: formId})
console.log('gerty.display_preferences', gerty.display_preferences)
this.formDialog.data.id = gerty.id this.formDialog.data.id = gerty.id
this.formDialog.data.name = gerty.name this.formDialog.data.name = gerty.name
this.formDialog.data.type = gerty.type this.formDialog.data.type = gerty.type
@ -666,7 +720,6 @@
(this.formDialog.show = true) (this.formDialog.show = true)
}, },
sendFormDataGerty: function () { sendFormDataGerty: function () {
console.log(this.formDialog.data)
if (this.formDialog.data.id) { if (this.formDialog.data.id) {
this.updateGerty( this.updateGerty(
this.g.user.wallets[0].adminkey, this.g.user.wallets[0].adminkey,
@ -680,6 +733,14 @@
} }
}, },
createGerty: function () { createGerty: function () {
if (
this.formDialog.data.display_preferences.dashboard ||
this.formDialog.data.display_preferences.dashboard_onchain ||
this.formDialog.data.display_preferences.dashboard_onchain ||
this.formDialog.data.display_preferences.lightning_dashboard
) {
this.formDialog.data.type = 'Gerty'
}
var data = { var data = {
name: this.formDialog.data.name, name: this.formDialog.data.name,
utc_offset: this.formDialog.data.utc_offset, utc_offset: this.formDialog.data.utc_offset,
@ -693,7 +754,6 @@
) )
} }
var self = this var self = this
console.log(data)
LNbits.api LNbits.api
.request( .request(
'POST', 'POST',
@ -711,6 +771,14 @@
}, },
updateGerty: function (wallet, data) { updateGerty: function (wallet, data) {
var self = this var self = this
if (
this.formDialog.data.display_preferences.dashboard ||
this.formDialog.data.display_preferences.dashboard_onchain ||
this.formDialog.data.display_preferences.dashboard_onchain ||
this.formDialog.data.display_preferences.lightning_dashboard
) {
this.formDialog.data.type = 'Gerty'
}
data.utc_offset = this.formDialog.data.utc_offset data.utc_offset = this.formDialog.data.utc_offset
data.type = this.formDialog.data.type data.type = this.formDialog.data.type
data.lnbits_wallets = JSON.stringify( data.lnbits_wallets = JSON.stringify(
@ -734,8 +802,8 @@
}, },
deleteGerty: function (gertyId) { deleteGerty: function (gertyId) {
var self = this var self = this
var gerty = _.findWhere(this.gertys, {id: gertyId})
var gerty = _.findWhere(self.gertys, {id: gertyId})
LNbits.utils LNbits.utils
.confirmDialog('Are you sure you want to delete this Gerty?') .confirmDialog('Are you sure you want to delete this Gerty?')
.onOk(function () { .onOk(function () {