Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some client examples to docs #669

Merged
merged 18 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 0 additions & 70 deletions bindings/nodejs/examples/client/00-get-info.ts

This file was deleted.

31 changes: 31 additions & 0 deletions bindings/nodejs/examples/how_tos/client/get-health.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2021-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Client, initLogger } from '@iota/sdk';
require('dotenv').config({ path: '.env' });

// Run with command:
// yarn run-example ./how_tos/client/get-health.ts

// In this example we will get the node health
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
}

const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
localPow: true,
});

try {
const isHealthy = await client.getHealth(process.env.NODE_URL);
console.log('Healthy: ', isHealthy);
} catch (error) {
console.error('Error: ', error);
}
}

run().then(() => process.exit());
31 changes: 31 additions & 0 deletions bindings/nodejs/examples/how_tos/client/get-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2021-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Client, initLogger } from '@iota/sdk';
require('dotenv').config({ path: '.env' });

// Run with command:
// yarn run-example ./how_tos/client/get-info.ts

// In this example we will get information about the node
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
}

const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
localPow: true,
});

try {
const nodeInfo = (await client.getInfo()).nodeInfo;
console.log(nodeInfo);
} catch (error) {
console.error('Error: ', error);
}
}

run().then(() => process.exit());
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Client, initLogger } from '@iota/sdk';
require('dotenv').config({ path: '.env' });

// Run with command:
// yarn run-example ./client/03-get-address-outputs.ts
// yarn run-example ./how_tos/client/get-outputs.ts

// In this example we will get the outputs of a known address
async function run() {
Expand All @@ -30,10 +30,11 @@ async function run() {
{ hasTimelock: false },
{ hasStorageDepositReturn: false },
]);
console.log('Output ids: ', outputIdsResponse, '\n');
console.log('First output of query:');
console.log('ID: ', outputIdsResponse.items[0]);

const addressOutputs = await client.getOutputs(outputIdsResponse.items);
console.log('Address outputs: ', addressOutputs);
const outputs = await client.getOutputs(outputIdsResponse.items);
console.log(outputs[0]);
} catch (error) {
console.error('Error: ', error);
}
Expand Down
15 changes: 15 additions & 0 deletions bindings/python/examples/how_tos/client/get_health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from iota_sdk import Client
from dotenv import load_dotenv
import json
import os

load_dotenv()

node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network')

# Create a Client instance
client = Client(nodes=[node_url])

# Get the node health
is_healthy = client.get_health(node_url)
print(f'Healthy: {is_healthy}')
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from iota_sdk import Client
from dotenv import load_dotenv
import json
import os

load_dotenv()
Expand All @@ -10,5 +11,5 @@
client = Client(nodes=[node_url])

# Get the node info
node_info = client.get_info()
print(f'{node_info}')
node_info = client.get_info()["nodeInfo"]
print(f'{json.dumps(node_info, indent=4)}')
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from iota_sdk import Client, NodeIndexerAPI
from dotenv import load_dotenv
import json
import os

load_dotenv()
Expand All @@ -18,8 +19,9 @@

# Get output ids of basic outputs that can be controlled by this address without further unlock constraints
output_ids_response = client.basic_output_ids(query_parameters)
print(f'{output_ids_response}')
print('First output of query:')
Thoralf-M marked this conversation as resolved.
Show resolved Hide resolved
print(f'ID: {output_ids_response.items[0]}')

# Get the outputs by their id
outputs = client.get_outputs(output_ids_response.items)
print(f'{outputs}')
print(f'{json.dumps(outputs[0], indent=4)}')
5 changes: 5 additions & 0 deletions documentation/sdk/docs/_admonitions/_account-client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:::tip Client in Wallet

If you are using a wallet you can always get the client by calling the `client()`/`getClient()`/`get_client()` method

:::
57 changes: 57 additions & 0 deletions documentation/sdk/docs/how-tos/client/get-health.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: Get Node Health
description: 'Check any nodes health.'
image: /img/logo/iota_mark_light.png
keywords:
- how to
- client
- load balancer
- node health
- nodejs
- python
- rust
---

import CodeBlock from '@theme/CodeBlock';
import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";
import NodejsCode from '!!raw-loader!../../../../../bindings/nodejs/examples/how_tos/client/get-health.ts';
import PythonCode from '!!raw-loader!../../../../../bindings/python/examples/how_tos/client/get_health.py';
import RustCode from '!!raw-loader!../../../../../sdk/examples/how_tos/client/get_health.rs';
import AccountClient from '../../_admonitions/_account-client.md'

You can check the health of any node.

<AccountClient/>

The following code example will:

1. Create a `Client` which will connect to the [Shimmer Testnet](https://api.testnet.shimmer.network).
2. Use the created client to get the health of the specified url.
3. Print the information to the console.

## Code Example

<Tabs groupId="language">
<TabItem value="rust" label="Rust">
<CodeBlock className="language-rust">
{RustCode}
</CodeBlock>
</TabItem>
<TabItem value="nodejs" label="Nodejs">
<CodeBlock className="language-typescript">
{NodejsCode}
</CodeBlock>
</TabItem>
<TabItem value="python" label="Python">
<CodeBlock className="language-python">
{PythonCode}
</CodeBlock>
</TabItem>
</Tabs>

## Expected Output

```bash
Healthy: true
```
Loading