Skip to content

Commit f173f94

Browse files
committed
docs: tutorial for bitcoin public api parsing
1 parent 3e715aa commit f173f94

2 files changed

Lines changed: 94 additions & 1 deletion

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
title: Bitcoin API parser
3+
---
4+
5+
:::note
6+
7+
- **Prerequisite**: First, [setup **CHKware**](/setup) to continue
8+
- **Prerequisite**: Then, setup vscode extension for _CHKware_
9+
- Find [more `http` examples](/examples/http-examples), and [more `testcase` examples](/examples/testcase-examples) here.
10+
11+
:::
12+
13+
Let's look into a simple automation scenario. This example shall give an idea of how CHKware, specifically the `chk` command, can be used as a part of automation to do the heavy lifting for an API automation process.
14+
15+
We will create an HTTP spec. doc that calls a bitcoin API. Then, we will write a Python script to parse the response of the API call.
16+
17+
:arrow_upper_right: [Code sample](https://github.com/chkware/cli/tree/main/tests/resources/storage/sample_flow/python)
18+
19+
#### 1. HTTP spec. doc for public bitcoin API
20+
21+
Please follow the below steps:
22+
23+
- Open vscode, then create and save a new file called `bitcoin-usd.chk`
24+
> You can name the file anything. For this example we'll assume the file is as above mentioned one.
25+
- In the vscode command pallet start type: chkware. In the available command dropdown find `CHKware: Add Http spec. snippet`. Then select `http: Minimal request` from the next dropdown.
26+
- Change the `url: ...` to look like following
27+
28+
```yml
29+
...
30+
request:
31+
url: https://api.coinstats.app/public/v1/coins/bitcoin?currency=USD
32+
```
33+
34+
- Then add a `expose:` section to just expose the response body after request is successful.
35+
36+
```yml
37+
...
38+
expose:
39+
- $_response.body
40+
```
41+
42+
- Now the final file should look like this
43+
44+
```yml
45+
---
46+
version: default:http:0.7.2
47+
request:
48+
url: https://api.coinstats.app/public/v1/coins/bitcoin?currency=USD
49+
method: GET
50+
expose:
51+
- $_response.body
52+
```
53+
54+
- Now again in the command pallet search and hit `CHKware: Run file` to see the response as output. :smiley:
55+
56+
#### 2. Write _Python_ script to parse response
57+
58+
We are going to parse the json output that gets printed after `chk http bitcoin-usd.chk` get run, in the _Python_ script. There are many ways to do this. But, using _Python_ core modules we can write as following.
59+
60+
> Scripting can also be done in any other programming language, such as _Javascript_, _Java_, _Kotlin_, etc.
61+
62+
```python
63+
# bitcoin_sample_01.py
64+
65+
import subprocess
66+
import json
67+
68+
# setup, run the file with `chk` and get response
69+
70+
file_path = "tests/resources/storage/sample_flow/python/bitcoin-usd.chk"
71+
result = subprocess.check_output(["chk", "http", "--result", "--no-format", file_path])
72+
73+
# prepare and get bitcoin node in the response
74+
75+
output = json.loads(result.rstrip())
76+
btc = output[0]['coin']
77+
78+
# to print current bitcoin price
79+
80+
print(f"BTC price now ${ btc['price'] }")
81+
```
82+
83+
Run the script in the command line like
84+
85+
```sh
86+
python bitcoin_sample_01.py
87+
```
88+
89+
> ** Please create an github issue if you want to share your sample script included in the project.

sidebars.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ const sidebars = {
4141
{
4242
type: "category",
4343
label: "Tutorial",
44-
items: ["tutorials/http-client", "tutorials/feature-test"],
44+
items: [
45+
"tutorials/http-client",
46+
"tutorials/feature-test",
47+
"tutorials/bitcoin-api-parser",
48+
],
4549
},
4650
"examples/http-examples",
4751
"examples/testcase-examples",

0 commit comments

Comments
 (0)