|
| 1 | +--- |
| 2 | +title: Automate Chuck Norris jokes |
| 3 | +--- |
| 4 | + |
| 5 | +In this automation example we are going to automate a simple workflow from [Chucknorris.io](https://api.chucknorris.io). Read more about the API on the site. |
| 6 | + |
| 7 | +The workflow we are trying to automate is: |
| 8 | + |
| 9 | +1. Get all the jokes categories |
| 10 | +2. Get jokes by a given category, for all categories found on step 1. |
| 11 | + |
| 12 | +The process we are going to follow: |
| 13 | + |
| 14 | +1. Create a file called `joke_categories_rq.chk`; put a HTTP spec. doc that fetch all the joke categories. |
| 15 | +2. Create a file called `joke_for_category_rq.chk`; put a HTTP spec. doc that fetch the jokes under a given category. |
| 16 | +3. We'll write a _Python_ script that does following: |
| 17 | + 1. Call the `joke_categories_rq.chk` using `chk` command, parse the response |
| 18 | + 2. Prepare a list of all available jokes categories from the response, and randomly choose one category |
| 19 | + 3. Call `joke_for_category_rq.chk` using `chk` command and pass the chosen category, and parse the response |
| 20 | + 4. Print out some information found on response |
| 21 | + |
| 22 | +:arrow_upper_right: [Code sample](https://github.com/chkware/cli/tree/main/tests/resources/storage/sample_flow/python/chucknorris_io) |
| 23 | + |
| 24 | +#### 1. Fetch all the joke categories |
| 25 | + |
| 26 | +- We'll begin with vscode. Just create a new file called `joke_categories_rq.chk`. |
| 27 | +- 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. |
| 28 | +- Change the `url: ...` so the file content looks like following. Then save the file. |
| 29 | + |
| 30 | + ```yml |
| 31 | + --- |
| 32 | + version: default:http:0.7.2 |
| 33 | + request: |
| 34 | + url: https://api.chucknorris.io/jokes/categories |
| 35 | + method: GET |
| 36 | + ``` |
| 37 | +
|
| 38 | +- In the vscode command pallet search and hit `CHKware: Run file` to see the response as output. |
| 39 | + |
| 40 | +#### 2. Fetch jokes under a given category |
| 41 | + |
| 42 | +- Now create a new file called `joke_for_category_rq.chk`. |
| 43 | +- 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. |
| 44 | + |
| 45 | +- We are going to change the `url: ...` so that we get jokes by category. Which is as following |
| 46 | + |
| 47 | + ```http |
| 48 | + https://api.chucknorris.io/jokes/random?category=category_name |
| 49 | + ``` |
| 50 | + |
| 51 | + So, from the URL we can see that we can pass `category_name` as URL param or query string with `?category=`. To pass variable we need to define a `variables:` section on the spec. |
| 52 | + |
| 53 | + ```yml |
| 54 | + --- |
| 55 | + variables: |
| 56 | + category: "food" |
| 57 | +
|
| 58 | + request: |
| 59 | + url: https://api.chucknorris.io/jokes/random?category={$category} |
| 60 | + ``` |
| 61 | + |
| 62 | + The above `variables:` sections defines a variable called **`category`** with default value **"food"**. Then we use **`category`** variable's value in the URL with **`{$category}`**. So, if we don't pass any value to this file, then **"food"** will be used as default value. You will see below how you can pass value to this **`category`** variable from command-line. |
| 63 | + |
| 64 | + More docs on [passing query params](/examples/http-examples#request-with-query-string) here |
| 65 | + |
| 66 | + Now the `joke_for_category_rq.chk` file should look like: |
| 67 | + |
| 68 | + ```yml |
| 69 | + --- |
| 70 | + version: default:http:0.7.2 |
| 71 | +
|
| 72 | + variables: |
| 73 | + category: "food" |
| 74 | +
|
| 75 | + request: |
| 76 | + url: https://api.chucknorris.io/jokes/random?category={$category} |
| 77 | + method: GET |
| 78 | + ``` |
| 79 | + |
| 80 | +- In the vscode command pallet search and hit `CHKware: Run file` to see the response as output. |
| 81 | + |
| 82 | +#### 3. Write _Python_ script to automate the workflow |
| 83 | + |
| 84 | +We are going to parse the json output from `chk http joke_categories_rq.chk`, and `chk http joke_for_category_rq.chk` command run, in the _Python_ script. There are many ways to do this. But, using _Python_ core modules we can write as following. |
| 85 | + |
| 86 | +> Scripting can also be done in any other programming language, such as _Javascript_, _Java_, _Kotlin_, etc. |
| 87 | + |
| 88 | +```python |
| 89 | +# get_random_jokes_for_category.py |
| 90 | +
|
| 91 | +import subprocess |
| 92 | +import json |
| 93 | +import random |
| 94 | +
|
| 95 | +# set spec. for list of all categories |
| 96 | +joke_categories_f = ( |
| 97 | + "tests/resources/storage/sample_flow/python/chucknorris_io/joke_categories_rq.chk" |
| 98 | +) |
| 99 | +
|
| 100 | +# execute command and get response |
| 101 | +result = subprocess.check_output( |
| 102 | + ["chk", "http", "--result", "--no-format", joke_categories_f] |
| 103 | +) |
| 104 | +
|
| 105 | +# convert response to json |
| 106 | +response = json.loads(result.rstrip()) |
| 107 | +
|
| 108 | +# get response body, and get list of categories |
| 109 | +categories = response[0]["body"] |
| 110 | +
|
| 111 | +# choose one random category |
| 112 | +a_category = random.choice(categories) |
| 113 | +
|
| 114 | +# set random joke by for given category spec. |
| 115 | +joke_for_category_f = ( |
| 116 | + "tests/resources/storage/sample_flow/python/chucknorris_io/joke_for_category_rq.chk" |
| 117 | +) |
| 118 | +
|
| 119 | +# execute command |
| 120 | +# get response |
| 121 | +# similar to run: |
| 122 | +# chk http -r --nf chucknorris_io/joke_for_category_rq.chk category=a_category |
| 123 | +result = subprocess.check_output( |
| 124 | + [ |
| 125 | + "chk", |
| 126 | + "http", |
| 127 | + "--result", |
| 128 | + "--no-format", |
| 129 | + joke_for_category_f, |
| 130 | + f"category={ a_category }", # pass category value |
| 131 | + ] |
| 132 | +) |
| 133 | +
|
| 134 | +# convert response to json |
| 135 | +response = json.loads(result.rstrip()) |
| 136 | +
|
| 137 | +# get joke from response body |
| 138 | +joke = response[0]["body"] |
| 139 | +
|
| 140 | +# display the joke |
| 141 | +print( |
| 142 | + "\n", |
| 143 | + f":: This joke {joke['id']} was created on {joke['created_at']} ::\n", |
| 144 | + f":: Permalink {joke['url']} ::\n", |
| 145 | + f"{joke['value']}\n", |
| 146 | + "\n", |
| 147 | +) |
| 148 | +``` |
| 149 | + |
| 150 | +Run the script in the command line like |
| 151 | + |
| 152 | +```sh |
| 153 | +python get_random_jokes_for_category.py |
| 154 | +``` |
| 155 | + |
| 156 | +> \*\* Please create an github issue if you want to share your sample script included in the project. |
0 commit comments