diff --git a/README.md b/README.md index e984b10127053adad219a8232fe4308b3e0fe126..4ec4a684930f31d9cb2b0b5b5a6bf1c49657cbc4 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# REST API CLIENT +# Bash REST API client This is a bash solution to script REST API calls. @@ -12,16 +12,21 @@ The specialties of this component are * The response can be stored ... and reimported later. After import you can use the http.get* functions to fetch results from the former request. * Caching support for GET requests with a given TTL: if you repeat a request to the same url and ttl was not reached yet then you continue with the cached result -Source: <https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client> +👤 Author: Axel Hahn; Institute for Medical Education; University of Bern \ +📄 Source: <https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client> \ +📜 License: GNU GPL 3.0 \ +📗 Docs: <https://os-docs.iml.unibe.ch/bash-rest-api-client/> -License: GNU GPL 3 -## Requirements +```text +http.init +http.makeRequest 'http://www.example.com/' +``` -* Bash as shell (runs on Linux - or with Cygwin or other Bash implementations on MS Windows too.) -* Curl must be in the path -* optional: sha1sum (for export function with autogenerated filenames only) +This stores the response in a variable and has no output. +Now you can get its data, eg ---- - -[Installation](docs/10_Installation.md) | [Usage](docs/20_Usage.md) | [Examples](docs/30_Examples.md) \ No newline at end of file +* http.getStatuscode - This returns the Http status code +* http.getResponseHeader - print Http response header +* http.getResponseData - get data from curl +* http.getResponse - get response body diff --git a/docs/10_Installation.md b/docs/10_Installation.md deleted file mode 100644 index 1e67cab7fe0ba7a37b6c9299a00e8fc7a161bb3b..0000000000000000000000000000000000000000 --- a/docs/10_Installation.md +++ /dev/null @@ -1,19 +0,0 @@ -# Download - - -Download the archive i.e. as zip: - -https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client/-/archive/master/bash-rest-api-client-master.zip - -(see other formats at the download button on https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client) - -# OR: Git clone - -```sh -git clone https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client.git -``` - -# Copy somewhere - -Copy the file `rest-api-client.sh` anywhere you want. -If you need it in a single script then copy it to its directory. If you need it more often then copy it to a folter of your PATH environment. diff --git a/docs/10_Introduction.md b/docs/10_Introduction.md new file mode 100644 index 0000000000000000000000000000000000000000..fbdf5069a7a65e4aec9242106cd6ea16820e1c10 --- /dev/null +++ b/docs/10_Introduction.md @@ -0,0 +1,21 @@ +## Introduction + +### Description + +This is a bash solution to script REST API calls. + +The specialties of this component are + +* It was build to simplify http calls and handle http response for scripts +* After making a request the response stays is in memory. There is no output to any file to grep something or to cleanup after usage. +* Its functions feel a bit like class methods, i.e. http.getResponse to get the response body or http.getResponseHeader for the Http response header +* This component wraps curl - ist supports any http method +* works with anonymous requests and Basic Authentication +* The response can be stored ... and reimported later. After import you can use the http.get* functions to fetch results from the former request. +* Caching support for GET requests with a given TTL: if you repeat a request to the same url and ttl was not reached yet then you continue with the cached result + +### Requirements + +* Bash as shell (runs on Linux - or with Cygwin or other Bash implementations on MS Windows too.) +* Curl must be in the path +* optional: sha1sum (for export function with autogenerated filenames only) diff --git a/docs/20_Installation.md b/docs/20_Installation.md new file mode 100644 index 0000000000000000000000000000000000000000..4dc5de18f30ba8395c3213facd8092b4b9c0aec2 --- /dev/null +++ b/docs/20_Installation.md @@ -0,0 +1,26 @@ +## Installation + +### Get all files + +#### Git clone + +```sh +git clone https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client.git +``` + +#### Download + +Download the archive i.e. as zip: + +<https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client/-/archive/master/bash-rest-api-client-master.zip> + +(see other formats at the download button on <https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client>) + +#### Copy shell script somewhere + +Copy the file `rest-api-client.sh` anywhere you need it. +If you need it in a single script then copy it to its directory. If you need it more often then copy it to a folter of your PATH environment. + +### Get the script only + +`wget https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client/-/raw/master/rest-api-client.sh` diff --git a/docs/20_Usage.md b/docs/30_Usage.md similarity index 84% rename from docs/20_Usage.md rename to docs/30_Usage.md index 591344e563bf463fa7b67e9be9ad8bde33bc4fc8..d421e151b74bfe0cdd79040dcc08867c934f2f65 100644 --- a/docs/20_Usage.md +++ b/docs/30_Usage.md @@ -7,7 +7,7 @@ The "methods" start with "http" dot "[method]". You should start with *http.help* to get an overwiew. -```sh +```text # # step one: source the shell script # @@ -18,6 +18,15 @@ $ . ./rest-api-client.sh # $ http.help + +Bash REST API client v0.8 + +This is a bash solution to script REST API calls. + +Source:https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client +License: GNU GPL 3 + + INSTRUCTION: - Source the file once @@ -31,11 +40,19 @@ INSTRUCTION: Enable or disable debugging infos during processing. It is written to STDERR. - - initialize a request + setAccept ACCEPT + Set authentication with user and password for basic auth + Default: application/json + setAuth AUTH:PASSWORD - set authentication + Set authentication with user and password for basic auth + + setAuthorization TYPE TOKEN|HASH + Set authentication with Authorization header. + As TYPE you can use Basic|Bearer|Negotiate|... + 2nd param is the token or hased user+password http.setBody DATA set a body for POST/ PUT requests. @@ -57,6 +74,10 @@ INSTRUCTION: Set a relative url for a request. This requires to use http.setBaseUrl before. + http.addHeader HEADER_LINE + Add a header line to the request. + This command can be repeated multiple times. + - caching functions http.setCacheTtl SECONDS @@ -103,7 +124,7 @@ INSTRUCTION: Get the http status as string Ok|Redirect|Error http.getStatuscode - Get the http status code of a request as integer + Get the http status code of a request as 3 digit integer http.isOk Check if the http response code is a 2xx diff --git a/docs/30_Examples.md b/docs/40_Examples.md similarity index 81% rename from docs/30_Examples.md rename to docs/40_Examples.md index c0ca4d1aca570d535f2837227c01df9debbe7db0..e91c6e7c503638e2d22073beeb9dfd892b830db6 100644 --- a/docs/30_Examples.md +++ b/docs/40_Examples.md @@ -1,4 +1,4 @@ -# Preparation +## Preparation A required step is sourcing the script to make the http.* function available in the current shell. @@ -9,7 +9,7 @@ A required step is sourcing the script to make the http.* function available in Then you can follow the examples. -# A first example +## A first example This is quick introduction: we make a request and have a look to a few results. @@ -67,7 +67,7 @@ url_effective:http://www.iml.unibe.ch/ ``` -# Icinga2 API +## Icinga2 API In that example we have a config for an api access with url, user and password. The docs url is not required for functionality. It will be shown on errors. @@ -113,3 +113,32 @@ if [ $? -ne 0 ]; then fi echo "OK, ${myHost} was found." ``` + +## Authentication with JWT token + +The snippet shows how to fetch a token first that will be added to be used in all following requests. + +```sh +# ---------- settings +URL=https://www.example.com/ +USER=... +PASSWORD=... + +# ---------- init +http.init +http.setBaseUrl "${URL}" + +# ---------- 1st request: get a token +http.setAuth "${USER}:${PASSWORD}" +http.makeRequest GET "token" + +# fetch token +ACCESS_TOKEN=$(http.getResponse | jq -r .access_token) + +# Set token for authorization +http.setAuthorization "Bearer ${ACCESS_TOKEN}" + +# ---------- Requests to app or api +http.makeRequest GET "api" +http.makeRequest GET "app" +``` diff --git a/docs/_index.md b/docs/_index.md index 5541390c9c284956728423f483189877201c6c62..2300b3ad7ebfe98cdcef1677fa3e1cec39c5dc8c 100644 --- a/docs/_index.md +++ b/docs/_index.md @@ -1,23 +1,11 @@ -# Description - -This is a bash solution to script REST API calls. - -The specialties of this component are - -* It was build to simplify http calls and handle http response for scripts -* After making a request the response stays is in memory. There is no output to any file to grep something or to cleanup after usage. -* Its functions feel a bit like class methods, i.e. http.getResponse to get the response body or http.getResponseHeader for the Http response header -* This component wraps curl - ist supports any http method -* works with anonymous requests and Basic Authentication -* The response can be stored ... and reimported later. After import you can use the http.get* functions to fetch results from the former request. -* Caching support for GET requests with a given TTL: if you repeat a request to the same url and ttl was not reached yet then you continue with the cached result - -Source: https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client - -License: GNU GPL 3 - -# Requirements - -* Bash as shell (runs on Linux - or with Cygwin or other Bash implementations on MS Windows too.) -* Curl must be in the path -* optional: sha1sum (for export function with autogenerated filenames only) +<html> +<div class="hero"> + <h2>Bash REST API client</h2> + +</div> +</html> + +👤 Author: Axel Hahn; Institute for Medical Education; University of Bern \ +📄 Source: <https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client> \ +📜 License: GNU GPL 3.0 \ +📗 Docs: <https://os-docs.iml.unibe.ch/bash-rest-api-client/>