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")
gerty_static_files = [
{
"path": "/gerty/static",
"app": StaticFiles(packages=[("lnbits", "extensions/gerty/static")]),
"name": "gerty_static",
}
]
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]:
logger.debug(endPoint)
endpoints = MempoolEndpoint()
url = ""
for endpoint in endpoints:
@ -96,17 +97,21 @@ async def get_mempool_info(endPoint: str, gerty) -> Optional[Mempool]:
if not row:
async with httpx.AsyncClient() as client:
response = await client.get(gerty.mempool_endpoint + url)
logger.debug(gerty.mempool_endpoint + url)
mempool_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO gerty.mempool (
id,
data,
endpoint,
time,
mempool_endpoint
)
VALUES (?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?)
""",
(
mempool_id,
json.dumps(response.json()),
endPoint,
int(time.time()),

View file

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

View file

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

View file

@ -10,6 +10,7 @@ class Gerty(BaseModel):
name: str
refresh_time: int = Query(None)
utc_offset: int = Query(None)
wallet: str = Query(None)
type: str
lnbits_wallets: str = Query(
None
@ -39,6 +40,7 @@ class MempoolEndpoint(BaseModel):
class Mempool(BaseModel):
id: str = Query(None)
mempool_endpoint: str = Query(None)
endpoint: 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
group="extras"
icon="swap_vertical_circle"
label="API info"
:content-inset-level="0.5"
>
<q-btn flat label="Swagger API" type="a" href="../docs#/gerty"></q-btn>
<q-expansion-item group="api" dense expand-separator label="List Gerty">
<q-card>
<q-card-section>
<code><span class="text-blue">GET</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>
<h5 class="text-caption q-mt-sm q-mb-none">
Returns 200 OK (application/json)
</h5>
<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>
<q-card-section>
<p>
Gerty (your bitcoin assistant): Use the software Gerty or
<a
target="_blank"
href="https://shop.lnbits.com/product/gerty-a-bitcoin-assistant"
>hardware Gerty</a
><br />
<small>
Created by, <a href="https://github.com/blackcoffeexbt">Black Coffee</a>,
<a href="https://github.com/benarc">Ben Arc</a></small
>
</p>
<a href="https://shop.lnbits.com/product/gerty-a-bitcoin-assistant"
><img src="/gerty/static/gerty.jpg"
/></a>
</q-card-section>

View file

@ -107,9 +107,21 @@
<div class="col-12 col-md-5 q-gutter-y-md">
<q-card>
<q-card-section>
<h6 class="text-subtitle1 q-my-none">
{{ SITE_TITLE }} Gerty extension
</h6>
<div class="row">
<div class="col-9">
<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 class="q-pa-none">
<q-separator></q-separator>
@ -309,12 +321,50 @@
>The amount of time in seconds between screen updates
</q-tooltip>
</q-input>
<q-separator class="q-mb-md" v-if="toggleStates.advanced"></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>
<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>
<q-separator
class="q-mb-md"
v-if="toggleStates.advanced"
></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>
<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>
@ -570,6 +620,8 @@
formDialog: {
show: false,
data: {
type: 'Mini Gerty',
exchange: 'USD',
utc_offset: new Date().getTimezoneOffset(),
display_preferences: {
dashboard: false,
@ -597,41 +649,44 @@
}
}
},
mounted() {
console.log('this.formDialog', this.formDialog.data.display_preferences)
},
methods: {
setAdvanced: function(){
self = this
self.formDialog.data.mempool_endpoint = 'https://mempool.space'
self.formDialog.data.refresh_time = 300
},
setWallets: function() {
self = this
if(!self.formDialog.data.display_preferences.lnbits_wallets_balance){
self.formDialog.data.lnbits_wallets = []
}
},
setOnchain: function() {
self = this
self.formDialog.data.display_preferences.onchain_difficulty_epoch_progress = self.toggleStates.onchain
self.formDialog.data.display_preferences.onchain_difficulty_retarget_date = self.toggleStates.onchain
self.formDialog.data.display_preferences.onchain_difficulty_blocks_remaining = !self.toggleStates.onchain
self.formDialog.data.display_preferences.onchain_difficulty_epoch_time_remaining = self.toggleStates.onchain
self.formDialog.data.display_preferences.onchain_block_height = 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
},
setAdvanced: function () {
self = this
self.formDialog.data.mempool_endpoint = 'https://mempool.space'
self.formDialog.data.refresh_time = 300
},
setWallets: function () {
self = this
if (!self.formDialog.data.display_preferences.lnbits_wallets_balance) {
self.formDialog.data.lnbits_wallets = []
}
},
setOnchain: function () {
self = this
self.formDialog.data.display_preferences.onchain_difficulty_epoch_progress =
self.toggleStates.onchain
self.formDialog.data.display_preferences.onchain_difficulty_retarget_date =
self.toggleStates.onchain
self.formDialog.data.display_preferences.onchain_difficulty_blocks_remaining = !self
.toggleStates.onchain
self.formDialog.data.display_preferences.onchain_difficulty_epoch_time_remaining =
self.toggleStates.onchain
self.formDialog.data.display_preferences.onchain_block_height =
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 () {
this.formDialog.data = {
utc_offset: 0,
lnbits_wallets: [],
mempool_endpoint: 'https://mempool.space',
refresh_time: 300,
type: 'Gerty',
display_preferences: {}
}
},
@ -651,7 +706,6 @@
},
updateformDialog: function (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.name = gerty.name
this.formDialog.data.type = gerty.type
@ -666,7 +720,6 @@
(this.formDialog.show = true)
},
sendFormDataGerty: function () {
console.log(this.formDialog.data)
if (this.formDialog.data.id) {
this.updateGerty(
this.g.user.wallets[0].adminkey,
@ -680,6 +733,14 @@
}
},
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 = {
name: this.formDialog.data.name,
utc_offset: this.formDialog.data.utc_offset,
@ -693,7 +754,6 @@
)
}
var self = this
console.log(data)
LNbits.api
.request(
'POST',
@ -711,6 +771,14 @@
},
updateGerty: function (wallet, data) {
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.type = this.formDialog.data.type
data.lnbits_wallets = JSON.stringify(
@ -734,8 +802,8 @@
},
deleteGerty: function (gertyId) {
var self = this
var gerty = _.findWhere(this.gertys, {id: gertyId})
var gerty = _.findWhere(self.gertys, {id: gertyId})
LNbits.utils
.confirmDialog('Are you sure you want to delete this Gerty?')
.onOk(function () {