Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
icinga-passive-client
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
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Open Source
icinga-passive-client
Commits
20ae8a38
Commit
20ae8a38
authored
10 months ago
by
Hahn Axel (hahn)
Browse files
Options
Downloads
Patches
Plain Diff
update rest-api client
parent
19950729
No related branches found
No related tags found
1 merge request
!43
OP#7520 Icinga client: update rest api client https://projects.iml.unibe.ch/work_packages/7520
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
inc/rest-api-client.sh
+439
-126
439 additions, 126 deletions
inc/rest-api-client.sh
inc/update_rest-api-client.sh
+2
-0
2 additions, 0 deletions
inc/update_rest-api-client.sh
with
441 additions
and
126 deletions
inc/rest-api-client.sh
+
439
−
126
View file @
20ae8a38
...
...
@@ -8,26 +8,29 @@
# - curl
# - sha1sum (optional; for export functionality with AUTOFILE only)
# ----------------------------------------------------------------------
# License: GPL 3.0
# Source: <https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client>
# Docs: <https://os-docs.iml.unibe.ch/bash-rest-api-client/>
# ----------------------------------------------------------------------
# (1) source this script
# (2) enter "http.help" to get a list of available commands
# ----------------------------------------------------------------------
# 2020-02-07 v0.2 axel.hahn@iml.unibe.ch BETABETA
# 2020-02-12 v0.4 axel.hahn@iml.unibe.ch Caching
# 2020-03-02 v0.5 axel.hahn@iml.unibe.ch a few more response check functions
# 2021-01-21 v0.6 axel.hahn@iml.unibe.ch add Content-type in request header
# 2022-01-11 v0.7 axel.hahn@iml.unibe.ch fixes using shellcheck
# 2024-10-09 v0.8 axel.hahn@unibe.ch add setAuthorization; customize accept header, add custom request headers
# 2024-10-10 v0.9 axel.hahn@unibe.ch update docs
# ======================================================================
# --- fetch incoming params
RestApiCfg
=
$1
RestApiMethod
=
$2
ApiUrl
=
$3
Body
=
"
$4
"
http_cfg__about
=
"Bash REST API client v0.5"
http_cfg__about
=
"Bash REST API client v0.9"
typeset
-i
http_cfg__debug
=
0
typeset
-i
http_cfg__cacheTtl
=
0
http_cfg__cacheDir
=
/var/tmp/http-cache
http_cfg__UA
=
"
${
http_cfg__about
}
"
http_cfg__prjurl
=
"https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client"
http_cfg__docsurl
=
"https://os-docs.iml.unibe.ch/bash-rest-api-client/"
# --- curl meta infos to collect
# see variables in man curl --write-out param
...
...
@@ -67,10 +70,11 @@
# ......................................................................
#
#
w
rite a debug message to STDERR
#
W
rite a debug message to STDERR
# Do no not change the prefix - is is read in inc_functions
#
# params strings output message
#
function
http._wd
(){
if
[
$http_cfg__debug
-gt
0
]
;
then
echo
-e
"
\e
[33m# RESTAPI::DEBUG
$*
\e
[0m"
>
&2
...
...
@@ -79,10 +83,11 @@
# ......................................................................
#
#
w
rite an error message to STDERR
#
W
rite an error message to STDERR
# Do no not change the prefix - is is read in inc_functions
#
# params strings output message
#
function
http._we
(){
echo
-e
"
\e
[31m# RESTAPI::ERROR
$*
\e
[0m"
>
&2
}
...
...
@@ -101,21 +106,31 @@ EOH
# grep "function http.[a-z]" $0 | sort
}
# ......................................................................
#
# Initialize the client
#
# Initialize the client for a new request. Call this before any other
# function. It will reset all variables.
#
function
http.init
(){
http._wd
"
${
FUNCNAME
[0]
}
()"
which curl
>
/dev/null
||
http.quit
# request vars
http_req__auth
=
http_req__auth
=
http_req__authorization
=
http_req__accept
=
"application/json"
http_req__headers
=()
http_req__body
=
http_req__method
=
GET
http_req__url
=
http_req__fullurl
=
http_req__docs
=
http_req__dataprefix
=
"RESTAPICLIENTMETADATA_
`
date
+%s
`
_
$$
"
http_req__dataprefix
=
"RESTAPICLIENTMETADATA_
$(
date
+%s
)
_
$$
"
local
writevar
=
for
myvar
in
$curlMeta
do
...
...
@@ -124,7 +139,6 @@ EOH
http_curl__writeout
=
"
\\
n
${
http_req__dataprefix
}${
writevar
}
\\
n"
# cache
http_req__mode
=
undefined
http_cfg__cacheTtl
=
0
http_cfg__cacheFile
=
...
...
@@ -135,15 +149,28 @@ EOH
chmod
777
${
http_cfg__cacheDir
}
2>/dev/null
}
# execute the request
# ......................................................................
#
# Execute the request
#
# param string optional: method; GET|POST|PUT|DELETE|...
# param string optional: full url
# param string optional: request body
#
# description:
#
# This function does the following:
#
# 1. Check if to use a cache
# 2. If not cached, make the request
# 3. If cached, use the cached result
#
function
http.makeRequest
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http._wd
"
${
FUNCNAME
[0]
}
()"
# --- handle optional prams
if
[
$#
-ne
0
]
;
then
echo
$1
|
grep
"^[A-Z]*$"
>
/dev/null
if
[
$?
-eq
0
]
;
then
if
echo
"
$1
"
|
grep
-q
"^[A-Z]*$"
;
then
http.setMethod
"
$1
"
shift
1
fi
...
...
@@ -153,20 +180,19 @@ EOH
# test -z "$1" || http.setFullUrl "$1"
# --- detect caching
http_req__mode
=
REQUEST
useCache
=
0
makeRequest
=
1
if
[
$http_cfg__cacheTtl
-gt
0
-a
"
${
http_req__method
}
"
=
"GET"
]
;
then
local
useCache
=
0
local
makeRequest
=
1
if
[
$http_cfg__cacheTtl
-gt
0
]
&&
[
"
${
http_req__method
}
"
=
"GET"
]
;
then
useCache
=
1
test
-z
"
${
http_cfg__cacheFile
}
"
&&
http_cfg__cacheFile
=
`
http._genOutfilename
"
${
http_cfg__cacheDir
}
/AUTOFILE"
`
test
-z
"
${
http_cfg__cacheFile
}
"
&&
http_cfg__cacheFile
=
$(
http._genOutfilename
"
${
http_cfg__cacheDir
}
/AUTOFILE"
)
if
[
-f
"
${
http_cfg__cacheFile
}
"
]
;
then
http.responseImport
"
${
http_cfg__cacheFile
}
"
typeset
-i
local
iAge
=
`
http.getRequestAge
`
local
iAge
;
typeset
-i
iAge
iAge
=
$(
http.getRequestAge
)
http._wd
"INFO: Age of cache is
$iAge
sec - vs TTL
$http_cfg__cacheTtl
sec - file
$http_cfg__cacheFile
"
if
[
$iAge
-gt
0
-a
$iAge
-lt
$http_cfg__cacheTtl
]
;
then
if
[
$iAge
-gt
0
]
&&
[
$iAge
-lt
$http_cfg__cacheTtl
]
;
then
http._wd
"INFO: Using cache"
makeRequest
=
0
http_req__mode
=
CACHE
else
http._wd
"INFO: Cache file will be updated after making the request"
rm
-f
"
${
http_cfg__cacheFile
}
"
2>/dev/null
...
...
@@ -177,45 +203,54 @@ EOH
# --- make the request
if
[
$makeRequest
-eq
1
]
;
then
http_req__start
=
`
date
+%s
`
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)
${
http_req__method
}
${
http_req__fullurl
}
"
http_resp__all
=
$(
if
[
-z
"
${
http_req__body
}
"
]
;
then
curl
-k
-s
\
-A
"
${
http_cfg__UA
}
"
\
-w
"
${
http_curl__writeout
}
"
\
-H
'Accept: application/json'
\
${
http_req__auth
}
\
-i
"
${
http_req__fullurl
}
"
\
# build curl parameters
local
curl_params
=(
-k
-s
-i
"
${
http_req__fullurl
}
"
-X
"
${
http_req__method
}
"
else
curl
-k
-s
\
-A
"
${
http_cfg__UA
}
"
\
-w
"
${
http_curl__writeout
}
"
\
-H
'Accept: application/json'
\
${
http_req__auth
}
\
-i
"
${
http_req__fullurl
}
"
\
-X
"
${
http_req__method
}
"
\
-d
"
${
http_req__body
}
"
fi
)
||
http.quit
-w
"
${
http_curl__writeout
}
"
-A
"
${
http_cfg__UA
}
"
)
test
-n
"
$http_req__auth
"
&&
curl_params+
=(
-u
"
$http_req__auth
"
)
test
-n
"
$http_req__authorization
"
&&
curl_params+
=(
-H
"Authorization:
$http_req__authorization
"
)
test
-n
"
$http_req__accept
"
&&
curl_params+
=(
-H
"Accept:
$http_req__accept
"
)
test
-n
"
$http_req__body
"
&&
curl_params+
=(
-d
"
${
http_req__body
}
"
)
curl_params+
=(
"
${
http_req__headers
[@]
}
"
)
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)
${
http_req__method
}
${
http_req__fullurl
}
"
http_resp__all
=
$(
curl
"
${
curl_params
[@]
}
"
)
||
http.quit
http._wd
"OK - Curl finished the http request ... processing data"
http_resp__neutral
=
`
http._fetchAllAndReformat
`
http_resp__neutral
=
$(
http._fetchAllAndReformat
)
if
[
$useCache
-eq
1
]
;
then
http._wd
"INFO: writing cache ..."
http.responseExport
"
${
http_cfg__cacheFile
}
"
fi
fi
http._wd
"Request function finished; Code
`
http.getStatuscode
`
"
http._wd
"Request function finished; Code
$(
http.getStatuscode
)
"
}
# ......................................................................
#
# show error message with last return code and quit with this exitcode
# Show error message with last return code and quit with this exitcode
#
# This function is used to quit the script with a meaningful error message
# and the exit code of the last command.
#
# The message is printed to STDERR and contains the return code.
# If a documentation URL is known, it is printed as a hint.
#
# The exit code is the one of the last command.
# To prevent the script from exiting when this function is called from a
# sourced file, the exit is commented out.
#
# no params
#
function
http.quit
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
rc
=
$?
http._wd
"
${
FUNCNAME
[0]
}
()"
echo
>
&2
echo
-e
"
\e
[31m# ERROR: command FAILED with rc
$rc
.
\e
[0m"
>
&2
if
[
!
-z
"
${
RestApiDocs
}
"
]
;
then
...
...
@@ -226,7 +261,26 @@ EOH
}
# load a config file
# ......................................................................
#
# Load a config file
#
# This function is marked as deprecated.
# It will be removed in the future.
#
# param string config file name
#
# Sourcing that file will set the following vars
# - RestApiUser
# - RestApiPassword
# - RestApiBaseUrl
# - RestApiDocs
#
# The function will then set the "internal" vars
# - http_req__auth
# - http_req__fullurl
# - http_req__docs
#
function
http.loadcfg
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
) !!! DEPRECATED !!!"
# reset expected vars from config
...
...
@@ -251,6 +305,17 @@ EOH
# ======================================================================
# GETTER
# ======================================================================
# ......................................................................
#
# Get the response header or response body
#
# param string what to return; one of header|body
#
# Return the response header or the response body. The output is the same
# as that of http.getResponseHeader or http.getResponse. The difference is
# the implementation
#
function
http._fetchResponseHeaderOrBody
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
local
isheader
=
true
...
...
@@ -271,25 +336,40 @@ EOH
fi
done
}
# ......................................................................
#
# Get the response data
#
# Return the curl meta infos like http_code, http_connect, local_ip, ...
# as key/ value pairs. Each line is a single key value pair.
#
# The data is extracted from the response header. The format is:
# ${http_req__dataprefix}|key|value|key|value|...
#
function
http._fetchResponseData
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http._wd
"
${
FUNCNAME
[0]
}
()"
echo
"
${
http_resp__all
}
"
|
sed
"s#
${
http_req__dataprefix
}
#
\n
${
http_req__dataprefix
}
#"
|
grep
"
${
http_req__dataprefix
}
"
|
tail
-1
|
cut
-f
2-
-d
"|"
|
sed
"s#|#
\n
#g"
|
grep
-v
"
${
http_req__dataprefix
}
"
|
while
read
-r
line
;
do
echo
$line
echo
"
$line
"
done
}
# ......................................................................
#
# Generate the dump with request and response
function
http._fetchAllAndReformat
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http._wd
"
${
FUNCNAME
[0]
}
()"
IFS
=
''
line
=
"#------------------------------------------------------------"
echo
"#_META_|about:
$http_cfg__about
"
echo
"#_META_|host:
`
hostname
-f
`
"
echo
"#_META_|host:
$(
hostname
-f
)
"
echo
$line
echo
"#_REQUEST_|fullurl:
$http_req__fullurl
"
echo
"#_REQUEST_|method:
$http_req__method
"
echo
"#_REQUEST_|time:
`
date
`
"
echo
"#_REQUEST_|timestamp:
`
date
+%s
`
"
echo
"#_REQUEST_|auth:
`
echo
$http_req__auth
|
sed
's#:.*#:xxxxxxxx#'
`
"
echo
"#_REQUEST_|time:
$(
date
)
"
echo
"#_REQUEST_|timestamp:
$(
date
+%s
)
"
echo
"#_REQUEST_|auth:
$(
echo
$http_req__auth
|
sed
's#:.*#:xxxxxxxx#'
)
"
echo
"#_REQUEST_|body:
$http_req__body
"
echo
"#_REQUEST_|baseurl:
$http_req__baseurl
"
echo
"#_REQUEST_|url:
$http_req__url
"
...
...
@@ -303,6 +383,14 @@ EOH
echo
$line
END
}
# ......................................................................
#
# Get a section from dump data
#
# param string what to return; one of HEADER|DATA|BODY
#
# returns string the requested part of the response
#
function
http._getFilteredResponse
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
echo
"
${
http_resp__neutral
}
"
|
grep
"^#_
${
1
}
_|"
|
cut
-f
2-
-d
"|"
...
...
@@ -310,44 +398,97 @@ EOH
# ---------- PUBLIC REQUEST GETTER
# ......................................................................
#
# Get timestamp of the response as a Unix timestamp.
#
# no param
#
# returns string the timestamp of the response
#
function
http.getRequestTs
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http._wd
"
${
FUNCNAME
[0]
}
()"
http._getFilteredResponse REQUEST |
grep
"^timestamp"
|
cut
-f
2
-d
":"
}
# get age of the response in sec.
# ......................................................................
#
# Get age of the response in sec.
# It is especially useful after responseImport
#
# no param
#
# returns integer age of the response in sec
#
function
http.getRequestAge
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
typeset
-i
local
iAge
=
`
date
+%s
`
-
`
http.getRequestTs
`
echo
$iAge
http._wd
"
${
FUNCNAME
[0]
}
()"
local
iAge
;
typeset
-i
iAge
local
iTs
;
typeset
-i
iTs
iTs
=
$(
http.getRequestTs
)
iAge
=
$(
date
+%s
)
-
${
iTs
}
echo
"
$iAge
"
}
# ---------- PUBLIC RESPONSE GETTER
# get response body
# ......................................................................
#
# Get response body
#
# no param
#
# returns string the response body
#
function
http.getResponse
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http._wd
"
${
FUNCNAME
[0]
}
()"
http._getFilteredResponse BODY
}
# get curl data of this request with status, transferred bytes, speed, ...
# ......................................................................
#
# Get curl data of this request with status, transferred bytes, speed, ...
#
# no param
#
# returns string the response data
#
function
http.getResponseData
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http._wd
"
${
FUNCNAME
[0]
}
()"
http._getFilteredResponse DATA
}
# get response header
# ......................................................................
#
# Get response header
#
# no param
#
# returns string the response header
#
function
http.getResponseHeader
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http._wd
"
${
FUNCNAME
[0]
}
()"
http._getFilteredResponse HEADER
}
# get raw response (not available after import)
# ......................................................................
#
# Get raw response (not available after import)
#
# no params
#
# no param
#
function
http.getResponseRaw
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http._wd
"
${
FUNCNAME
[0]
}
()"
echo
"
${
http_resp__all
}
"
}
# get Http status as string OK|Redirect|Error
# ......................................................................
#
# Get Http status as string OK|Redirect|Error
#
# no params
#
function
http.getStatus
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http.isOk
>
/dev/null
&&
echo
OK
...
...
@@ -355,48 +496,85 @@ EOH
http.isError
>
/dev/null
&&
echo
Error
}
# get Http status code of the request as 3 digit number
# ......................................................................
#
# Get Http status code of the request as 3 digit number
#
# no params
#
function
http.getStatuscode
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
local
_filter
=
$1
http._wd
"
${
FUNCNAME
[0]
}
()"
http.getResponseData |
grep
"^http_code:"
|
cut
-f
2
-d
":"
}
# was response a 2xx status code?
# ......................................................................
#
# Check: was response a 2xx status code?
# output is a statuscode if it matches ... or empty
# Additionally you can verify the return code
#
# $? -eq 0 means YES
# $? -ne 0 means NO
#
# no params
#
function
http.isOk
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http._wd
"
${
FUNCNAME
[0]
}
()"
http.getStatuscode |
grep
'2[0-9][0-9]'
}
# was the repsonse a redirect?
# ......................................................................
#
# Was the repsonse a redirect?
#
# no params
#
function
http.isRedirect
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http._wd
"
${
FUNCNAME
[0]
}
()"
http.getStatuscode |
grep
'3[0-9][0-9]'
}
# was the repsonse a client error (4xx or 5xx)
# ......................................................................
#
# Was the repsonse a client error (4xx or 5xx)
#
# no params
#
function
http.isError
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http._wd
"
${
FUNCNAME
[0]
}
()"
http.getStatuscode |
grep
'[45][0-9][0-9]'
}
# was the repsonse a client error (4xx)
# ......................................................................
#
# Was the repsonse a client error (4xx)
#
# no params
#
function
http.isClientError
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http._wd
"
${
FUNCNAME
[0]
}
()"
http.getStatuscode |
grep
'4[0-9][0-9]'
}
# was the repsonse a client error (5xx)
# ......................................................................
#
# Was the repsonse a client error (5xx)
#
# no params
#
function
http.isServerError
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http._wd
"
${
FUNCNAME
[0]
}
()"
http.getStatuscode |
grep
'5[0-9][0-9]'
}
# dump information about request and response
# ......................................................................
#
# Dump information about request and response
#
# no params
#
function
http.dump
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http._wd
"
${
FUNCNAME
[0]
}
()"
http.responseExport
}
...
...
@@ -404,41 +582,56 @@ EOH
# Import/ Export
# ======================================================================
# helper to replace "AUTOFILE" with something uniq using full url
# ......................................................................
#
# Helper to replace "AUTOFILE" with something uniq using full url
#
# param string import or export filename
#
function
http._genOutfilename
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
echo
$1
|
grep
"AUTOFILE"
>
/dev/null
if
[
$?
-ne
0
]
;
then
echo
$1
if
echo
"
$1
"
|
grep
-q
"AUTOFILE"
;
then
echo
"
$1
"
else
local sum
=
`
echo
${
http_req__fullurl
}
|
sha1sum
`
local
autofile
=
`
echo
"
${
sum
}
__
${
http_req__fullurl
}
"
|
sed
"s#[^a-z0-9]#_#g"
`
echo
$1
|
sed
"s#AUTOFILE#
${
autofile
}
#"
local sum
sum
=
$(
echo
${
http_req__fullurl
}
|
sha1sum
)
local
autofile
autofile
=
$(
echo
"
${
sum
}
__
${
http_req__fullurl
}
"
|
sed
"s#[^a-z0-9]#_#g"
)
echo
"
$1
"
|
sed
"s#AUTOFILE#
${
autofile
}
#"
fi
}
# export to a file
# ......................................................................
#
# Export response to a file
#
# param string optional: custom filename
#
function
http.responseExport
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
if
[
-z
$1
]
;
then
if
[
-z
"
$1
"
]
;
then
echo
"
${
http_resp__neutral
}
"
else
local
outfile
=
`
http._genOutfilename
"
$1
"
`
local
outfile
outfile
=
$(
http._genOutfilename
"
$1
"
)
http._wd
"
${
FUNCNAME
[0]
}
(
$1
) writing to outfile
$outfile
"
echo
"
${
http_resp__neutral
}
"
>
$outfile
echo
"
${
http_resp__neutral
}
"
>
"
$outfile
"
fi
}
# import a former response from a file
# ......................................................................
#
# Import a former response from a file
#
# param string filename with stored response
#
function
http.responseImport
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
local
infile
=
`
http._genOutfilename
"
$1
"
`
local
infile
infile
=
$(
http._genOutfilename
"
$1
"
)
if
[
-r
"
${
infile
}
"
]
;
then
grep
"^#_META_|about:
$http_cfg__about
"
"
${
infile
}
"
>
/dev/null
if
[
$?
-eq
0
]
;
then
http_resp__neutral
=
`
cat
"
${
infile
}
"
`
if
grep
-q
"^#_META_|about:
$http_cfg__about
"
"
${
infile
}
"
;
then
http_resp__neutral
=
$(
cat
"
${
infile
}
"
)
else
echo
"ERROR: Ooops [
${
infile
}
] does not seem to be an export dump."
http.quit
...
...
@@ -448,16 +641,21 @@ EOH
http.quit
fi
}
# delete an exported file; this is especially useful if you use
# ......................................................................
#
# Delete an exported file; this is especially useful if you use
# AUTOFILE functionality
#
# param string filename with stored response
#
function
http.responseDelete
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
local
infile
=
`
http._genOutfilename
"
$1
"
`
local
infile
infile
=
$(
http._genOutfilename
"
$1
"
)
if
[
-r
"
${
infile
}
"
]
;
then
grep
"^#_META_|about:
$http_cfg__about
"
"
${
infile
}
"
>
/dev/null
if
[
$?
-eq
0
]
;
then
rm
-f
"
${
infile
}
"
if
[
$?
-eq
0
]
;
then
if
grep
-q
"^#_META_|about:
$http_cfg__about
"
"
${
infile
}
"
;
then
if
rm
-f
"
${
infile
}
"
;
then
http._wd
"OK,
${
infile
}
was deleted."
else
http._wd
"ERROR: unable to delete existing
${
infile
}
. Check permissions."
...
...
@@ -474,32 +672,95 @@ EOH
# SETTER
# ======================================================================
# set authentication
# param string USER:PASSWORD
# ......................................................................
#
# Add a line to the request header
#
# param string line to add, eg "Connection: keep-alive"
#
function
http.addHeader
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http_req__headers+
=(
-H
"
$1
"
)
}
# ......................................................................
#
# set Accept request header and override default
#
# param string accept header value, eg text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
#
function
http.setAccept
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
if
[
-z
"
$1
"
]
;
then
http_req__accept
=
else
http_req__accept
=
"
$1
"
fi
}
# ......................................................................
#
# Set basic authentication
# Without given parameter, authentication is removed
#
# param string optional: USER:PASSWORD
#
function
http.setAuth
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
if
[
-z
"
$1
"
]
;
then
http_req__auth
=
else
http_req__auth
=
"-u
$1
"
http_req__auth
=
"
$1
"
fi
}
# ......................................................................
#
# Set authentication via Athorization header
# Without given parameter, authorization is removed
#
# param string optional: type, eg. Basic|Bearer|Negotiate
# param string optional: token or encoded user + password
#
function
http.setAuthorization
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
$2
)"
if
[
-z
"
$1
"
]
;
then
http_req__authorization
=
else
http_req__authorization
=
"
${
1
}
${
2
}
"
fi
}
# set body to send for PUTs and POSTs
# ......................................................................
#
# Set body to send for PUTs and POSTs
#
# param string body
#
function
http.setBody
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http_req__body
=
$1
}
# set a base url of an API
# ......................................................................
#
# Set a base url of an API
# Remark: Then use http.setUrl to complet the url to request
#
# param string url
#
function
http.setBaseUrl
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http_req__baseurl
=
$1
http.setFullUrl
http.setFullUrl
""
}
# ......................................................................
#
# Enable or disable debug mode
#
# param integer 0|1
#
function
http.setDebug
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http_cfg__debug
=
$1
...
...
@@ -509,15 +770,23 @@ EOH
http_req__docs
=
$1
}
# set the method to use; GET|POST|PUT|DELETE
# ......................................................................
#
# Set the method to use; GET|POST|PUT|DELETE|...
#
# param string name of method
#
function
http.setMethod
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http_req__method
=
$1
}
# set a full url to request
# param string url
# ......................................................................
#
# Set a full url to request
#
# param string optional: url
#
function
http.setFullUrl
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
if
[
-z
"
$1
"
]
;
then
...
...
@@ -526,8 +795,13 @@ EOH
http_req__fullurl
=
$1
fi
}
# complete the base url
# ......................................................................
#
# Complete the base url
#
# param string url part behind base url
#
function
http.setUrl
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http_req__url
=
$1
...
...
@@ -536,16 +810,34 @@ EOH
# ----- caching
# ......................................................................
#
# Set cache ttl in seconds
#
# param integer ttl in seconds
#
function
http.setCacheTtl
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http_cfg__cacheTtl
=
$1
}
# ......................................................................
#
# Set cache file
#
# param string filename
#
function
http.setCacheFile
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
http_cfg__cacheFile
=
"
$1
"
}
# ......................................................................
#
# Flush the cache
#
# no params
#
function
http.flushCache
(){
http._wd
"
${
FUNCNAME
[0]
}
(
$1
)"
rm
-f
${
http_cfg__cacheDir
}
/
*
...
...
@@ -554,7 +846,9 @@ EOH
# ......................................................................
#
# show a help text
#
# no params
#
function
http.help
(){
cat
<<
EOH
...
...
@@ -562,7 +856,8 @@ $http_cfg__about
This is a bash solution to script REST API calls.
Source:
$http_cfg__prjurl
Source: <
$http_cfg__prjurl
>
Docs: <
$http_cfg__docsurl
>
License: GNU GPL 3
...
...
@@ -581,8 +876,19 @@ INSTRUCTION:
- initialize a request
setAccept ACCEPT
Set authentication with user and password for basic auth
Default:
$http_req__accept
setAuth AUTH:PASSWORD
set authentication
Set authentication with user and password for basic auth
Without given parameter, authentication is removed
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
Without given parameter, authorization is removed
http.setBody DATA
set a body for POST/ PUT requests.
...
...
@@ -604,6 +910,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
...
...
@@ -646,8 +956,11 @@ INSTRUCTION:
- check http status code
http.getStatus
Get the http status as string Ok|Redirect|Error
http.getStatuscode
Get the http status code of a request
Get the http status code of a request
as 3 digit integer
http.isOk
Check if the http response code is a 2xx
...
...
This diff is collapsed.
Click to expand it.
inc/update_rest-api-client.sh
0 → 100755
+
2
−
0
View file @
20ae8a38
#!/bin/sh
wget
-O
rest-api-client.sh https://git-repo.iml.unibe.ch/iml-open-source/bash-rest-api-client/-/raw/master/rest-api-client.sh
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