Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Ğ
Ğcli-v2s
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
clients
Rust
Ğcli-v2s
Commits
090980ca
Commit
090980ca
authored
1 year ago
by
Hugo Trentesaux
Browse files
Options
Downloads
Patches
Plain Diff
add indexer latest block
parent
0f6ef0ea
No related branches found
No related tags found
1 merge request
!5
add features and refac data
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
res/indexer-queries.graphql
+12
-6
12 additions, 6 deletions
res/indexer-queries.graphql
src/indexer.rs
+63
-5
63 additions, 5 deletions
src/indexer.rs
src/main.rs
+1
-1
1 addition, 1 deletion
src/main.rs
with
76 additions
and
12 deletions
res/indexer-queries.graphql
+
12
−
6
View file @
090980ca
query
IdentityNameByPubkey
(
$pubkey
:
String
!)
{
identity
(
where
:
{
pubkey
:
{
_eq
:
$pubkey
}
})
{
name
}
identity
(
where
:
{
pubkey
:
{
_eq
:
$pubkey
}
})
{
name
}
}
query
IdentityPubkeyByName
(
$name
:
String
!)
{
identity_by_pk
(
name
:
$name
)
{
pubkey
}
identity_by_pk
(
name
:
$name
)
{
pubkey
}
}
query
LatestBlock
{
parameters
(
where
:
{
key
:
{
_eq
:
"last_indexed_block_number"
}
})
{
value
}
}
This diff is collapsed.
Click to expand it.
src/indexer.rs
+
63
−
5
View file @
090980ca
pub
use
graphql_client
::{
reqwest
::
post_graphql
,
GraphQLQuery
};
use
graphql_client
::
reqwest
::
post_graphql
;
use
graphql_client
::
GraphQLQuery
;
use
anyhow
::
Result
;
use
crate
::
*
;
// type used in parameters query
#[allow(non_camel_case_types)]
type
jsonb
=
serde_json
::
Value
;
#[derive(GraphQLQuery)]
#[graphql(
...
...
@@ -16,14 +21,21 @@ pub struct IdentityNameByPubkey;
)]
pub
struct
IdentityPubkeyByName
;
#[derive(Clone)]
#[derive(GraphQLQuery)]
#[graphql(
schema_path
=
"res/indexer-schema.json"
,
query_path
=
"res/indexer-queries.graphql"
)]
pub
struct
LatestBlock
;
#[derive(Clone,
Debug)]
pub
struct
Indexer
{
pub
gql_client
:
reqwest
::
Client
,
pub
gql_url
:
String
,
}
impl
Indexer
{
pub
async
fn
username_by_pubkey
(
&
self
,
pubkey
:
&
str
)
->
Result
<
Option
<
String
>>
{
pub
async
fn
username_by_pubkey
(
&
self
,
pubkey
:
&
str
)
->
anyhow
::
Result
<
Option
<
String
>>
{
Ok
(
post_graphql
::
<
IdentityNameByPubkey
,
_
>
(
&
self
.gql_client
,
&
self
.gql_url
,
...
...
@@ -36,7 +48,7 @@ impl Indexer {
.and_then
(|
data
|
data
.identity
.into_iter
()
.next
()
.map
(|
idty
|
idty
.name
)))
}
pub
async
fn
pubkey_by_username
(
&
self
,
username
:
&
str
)
->
Result
<
Option
<
String
>>
{
pub
async
fn
pubkey_by_username
(
&
self
,
username
:
&
str
)
->
anyhow
::
Result
<
Option
<
String
>>
{
Ok
(
post_graphql
::
<
IdentityPubkeyByName
,
_
>
(
&
self
.gql_client
,
self
.gql_url
.clone
(),
...
...
@@ -48,4 +60,50 @@ impl Indexer {
.data
.and_then
(|
data
|
data
.identity_by_pk
.map
(|
idty
|
idty
.pubkey
)))
}
/// fetch latest block
pub
async
fn
fetch_latest_block
(
&
self
)
->
Result
<
u64
,
anyhow
::
Error
>
{
Ok
(
post_graphql
::
<
LatestBlock
,
_
>
(
&
self
.gql_client
,
self
.gql_url
.clone
(),
latest_block
::
Variables
{},
)
.await
?
.data
.unwrap
()
// must have a data field
.parameters
.first
()
.unwrap
()
// must have one and only one parameter matching request
.value
.clone
()
.unwrap
()
// must have a value field
.as_u64
()
.unwrap
())
// must be a Number of blocks
}
}
#[derive(Clone,
Default,
Debug,
clap::Parser)]
pub
enum
IndexerSubcommand
{
#[default]
/// Show indexer endpoint
ShowEndpoint
,
/// Fetch latest indexed block
LatestBlock
,
}
pub
async
fn
handle_command
(
data
:
Data
,
command
:
IndexerSubcommand
)
->
anyhow
::
Result
<
()
>
{
let
data
=
data
.build_indexer
()
?
;
match
command
{
IndexerSubcommand
::
ShowEndpoint
=>
{
println!
(
"indexer endpoint: {}"
,
data
.indexer
()
.gql_url
);
}
IndexerSubcommand
::
LatestBlock
=>
{
println!
(
"latest indexed block is: {}"
,
data
.indexer
()
.fetch_latest_block
()
.await
?
);
}
};
Ok
(())
}
This diff is collapsed.
Click to expand it.
src/main.rs
+
1
−
1
View file @
090980ca
...
...
@@ -564,7 +564,7 @@ async fn main() -> Result<(), GcliError> {
.await
.unwrap
(),
Subcommand
::
RuntimeInfo
=>
commands
::
runtime
::
runtime_info
(
data
)
.await
,
Subcommand
::
Indexer
(
subcommand
)
=>
indexer
::
handle_command
(
data
,
subcommand
)
?
,
Subcommand
::
Indexer
(
subcommand
)
=>
indexer
::
handle_command
(
data
,
subcommand
)
.await
?
,
}
Ok
(())
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment