• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dynamotn / dybatpho / 13771606036

10 Mar 2025 06:03PM UTC coverage: 99.194% (-0.8%) from 100.0%
13771606036

push

github

dynamotn
style: follow my new style guide

2 of 3 new or added lines in 2 files covered. (66.67%)

1 existing line in 1 file now uncovered.

246 of 248 relevant lines covered (99.19%)

38.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.22
/src/network.sh
1
#!/usr/bin/env bash
2
# @file network.sh
3
# @brief Utilities for network
4
# @description
5
#   This module contains functions to work with network connection.
6
#
7
# **DYBATPHO_CURL_MAX_RETRIES** (number): Max number of retries when using `curl` failed
8
: "${DYBATPHO_DIR:?DYBATPHO_DIR must be set. Please source dybatpho/init.sh before other scripts from dybatpho.}"
134✔
9

10
DYBATPHO_CURL_MAX_RETRIES=${DYBATPHO_CURL_MAX_RETRIES:-5}
134✔
11

12
#######################################
13
# @description Get description of HTTP status code
14
# @arg $1 string Status code
15
# @stdout Description of status code
16
#######################################
17
function __get_http_code {
18
  local code
1✔
19
  dybatpho::expect_args code -- "$@"
1✔
20

21
  case "${code}" in
1✔
22
    # kcov(disabled)
23
    '100') echo '100 (continue)' ;;
24
    '101') echo '101 (switching protocols)' ;;
25
    # kcov(enabled)
UNCOV
26
    '200') echo 'done' ;;
×
27
    # kcov(disabled)
28
    '201') echo '201 (created)' ;;
29
    '202') echo '202 (accepted)' ;;
30
    '203') echo '203 (non-authoritative information)' ;;
31
    '204') echo '204 (no content)' ;;
32
    '205') echo '205 (reset content)' ;;
33
    '206') echo '206 (partial content)' ;;
34
    '300') echo '300 (multiple choices)' ;;
35
    '301') echo '301 (moved permanently)' ;;
36
    '302') echo '302 (found)' ;;
37
    '303') echo '303 (see other)' ;;
38
    '304') echo '304 (not modified)' ;;
39
    '305') echo '305 (use proxy)' ;;
40
    '306') echo '306 (switch proxy)' ;;
41
    '307') echo '307 (temporary redirect)' ;;
42
    '400') echo '400 (bad request)' ;;
43
    '401') echo '401 (unauthorized)' ;;
44
    '402') echo '402 (payment required)' ;;
45
    # kcov(enabled)
46
    '403') echo '403 (forbidden)' ;;
1✔
47
    # kcov(disabled)
48
    '404') echo '404 (not found)' ;;
49
    '405') echo '405 (method not allowed)' ;;
50
    '406') echo '406 (not acceptable)' ;;
51
    '407') echo '407 (proxy authentication required)' ;;
52
    '408') echo '408 (request timeout)' ;;
53
    '409') echo '409 (conflict)' ;;
54
    '410') echo '410 (gone)' ;;
55
    '411') echo '411 (length required)' ;;
56
    '412') echo '412 (precondition failed)' ;;
57
    '413') echo '413 (request entity too large)' ;;
58
    '414') echo '414 (request URI too long)' ;;
59
    '415') echo '415 (unsupported media type)' ;;
60
    '416') echo '416 (requested range)' ;;
61
    '417') echo '417 (expectation failed)' ;;
62
    '418') echo "418 (I'm a teapot)" ;;
63
    '419') echo '419 (authentication timeout)' ;;
64
    '420') echo '420 (enhance your calm)' ;;
65
    '426') echo '426 (upgrade required)' ;;
66
    '428') echo '428 (precondition required)' ;;
67
    '429') echo '429 (too many requests)' ;;
68
    '431') echo '431 (request header fields too large)' ;;
69
    '451') echo '451 (unavailable for legal reasons)' ;;
70
    '500') echo '500 (internal server error)' ;;
71
    '501') echo '501 (not implemented)' ;;
72
    '502') echo '502 (bad gateway)' ;;
73
    '503') echo '503 (service unavailable)' ;;
74
    '504') echo '504 (gateway timeout)' ;;
75
    '505') echo '505 (HTTP version not supported)' ;;
76
    '506') echo '506 (variant also negotiates)' ;;
77
    '510') echo '510 (not extended)' ;;
78
    '511') echo '511 (network authentication required)' ;;
79
    *) echo "${code} (unknown)" ;;
80
      # kcov(enabled)
81
  esac
82
}
83

84
#######################################
85
# @description Transfering data with URL by curl
86
# @example
87
#   dybatpho::curl_do <url> --output /tmp/1
88
#
89
# @arg $1 string URL
90
# @arg $2 string Location of curl output, default is `/dev/null`
91
# @arg $3 string Other options/arguments for curl
92
# @exitcode 0 Transfered data
93
# @exitcode 1 Unknown error
94
# @exitcode 3 First digit of HTTP error code 3xx
95
# @exitcode 4 First digit of HTTP error code 4xx
96
# @exitcode 5 First digit of HTTP error code 5xx
97
# @exitcode 127 Curl isn't installed
98
#######################################
99
function dybatpho::curl_do {
100
  local url
9✔
101
  dybatpho::expect_args url -- "$@"
9✔
102
  shift
8✔
103
  local output="/dev/null"
8✔
104
  if [ $# -ne 0 ]; then
8✔
105
    output="$1"
7✔
106
    shift
7✔
107
  fi
108

109
  local code
8✔
110
  # shellcheck disable=SC2317
111
  __request() {
112
    dybatpho::require curl
13✔
113
    # kcov(disabled)
114
    code=$(
115
      curl -fsSL "${url}" \
116
        -w '%{http_code}' \
117
        -o "${output}" \
118
        "$@" \
119
        2> /dev/null
120
    ) || true
121
    # kcov(enabled)
122

123
    local code_description
13✔
124
    code_description=$(_get_http_code "${code}")
26✔
125
    dybatpho::debug "Received HTTP status: ${code_description}"
13✔
126

127
    if [[ "${code}" =~ '2'.* ]] || [[ "${code}" =~ '4'.* ]]; then
22✔
128
      return 0
5✔
129
    else
130
      return 1
8✔
131
    fi
132
  }
133

134
  dybatpho::retry "${DYBATPHO_CURL_MAX_RETRIES}" __request
8✔
135

136
  # Return exit code based on HTTP status code
137
  case "${code}" in
8✔
138
    '2'*) return 0 ;;
4✔
139
    '3'*) return 3 ;;
1✔
140
    '4'*) return 4 ;;
1✔
141
    '5'*) return 5 ;;
1✔
142
    *) return 1 ;;
1✔
143
  esac
144
}
145

146
#######################################
147
# @description Download file
148
# @arg $1 URL
149
# @arg $2 Destination of file to download
150
# @see dybatpho::curl_do
151
# @exitcode 2 Can't create folder of destination file
152
#######################################
153
function dybatpho::curl_download {
154
  local url dst_file
3✔
155
  dybatpho::expect_args url dst_file -- "$@"
3✔
156
  dybatpho::progress "Downloading ${url}"
1✔
157

158
  # Create destination folder
159
  local dst_dir
1✔
160
  dst_dir=$(dirname "${dst_file}") || return 2
2✔
161
  mkdir -p "${dst_dir}" || return 2
1✔
162

163
  dybatpho::curl_do "${url}" "${dst_file}" || return
1✔
164
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc