|
| 1 | +create or replace package body slack_util_pkg |
| 2 | +as |
| 3 | + |
| 4 | + /* |
| 5 | + |
| 6 | + Purpose: Package handles Slack API |
| 7 | + |
| 8 | + Remarks: see https://api.slack.com/ |
| 9 | + |
| 10 | + Who Date Description |
| 11 | + ------ ---------- -------------------------------- |
| 12 | + MBR 17.02.2018 Created |
| 13 | + |
| 14 | + */ |
| 15 | + |
| 16 | + g_api_base_url string_util_pkg.t_max_db_varchar2 := 'https://slack.com/api'; |
| 17 | + |
| 18 | + g_webhook_host string_util_pkg.t_max_db_varchar2 := 'https://hooks.slack.com'; |
| 19 | + g_webhook_path string_util_pkg.t_max_db_varchar2; |
| 20 | + |
| 21 | + g_wallet_path string_util_pkg.t_max_db_varchar2; |
| 22 | + g_wallet_password string_util_pkg.t_max_db_varchar2; |
| 23 | + |
| 24 | + |
| 25 | +procedure assert (p_condition in boolean, |
| 26 | + p_error_message in varchar2) |
| 27 | +as |
| 28 | +begin |
| 29 | + |
| 30 | + /* |
| 31 | + |
| 32 | + Purpose: assert condition is true, otherwise raise an error |
| 33 | + |
| 34 | + Remarks: |
| 35 | + |
| 36 | + Who Date Description |
| 37 | + ------ ---------- -------------------------------- |
| 38 | + MBR 20.02.2018 Created |
| 39 | + |
| 40 | + */ |
| 41 | + |
| 42 | + if (p_condition is null) or (not p_condition) then |
| 43 | + raise_application_error (-20000, p_error_message); |
| 44 | + end if; |
| 45 | + |
| 46 | +end assert; |
| 47 | + |
| 48 | + |
| 49 | +function make_request (p_url in varchar2, |
| 50 | + p_body in clob := null, |
| 51 | + p_http_method in varchar2 := 'POST') return clob |
| 52 | +as |
| 53 | + l_http_status_code pls_integer; |
| 54 | + l_returnvalue clob; |
| 55 | +begin |
| 56 | + |
| 57 | + /* |
| 58 | + |
| 59 | + Purpose: make HTTP request |
| 60 | + |
| 61 | + Remarks: |
| 62 | + |
| 63 | + Who Date Description |
| 64 | + ------ ---------- -------------------------------- |
| 65 | + MBR 17.02.2018 Created |
| 66 | + |
| 67 | + */ |
| 68 | + |
| 69 | + apex_web_service.g_request_headers.delete; |
| 70 | + |
| 71 | + apex_web_service.g_request_headers(1).name := 'Content-Type'; |
| 72 | + apex_web_service.g_request_headers(1).value := 'application/json'; |
| 73 | + |
| 74 | + l_returnvalue := apex_web_service.make_rest_request( |
| 75 | + p_url => p_url, |
| 76 | + p_http_method => p_http_method, |
| 77 | + p_body => p_body, |
| 78 | + p_wallet_path => g_wallet_path, |
| 79 | + p_wallet_pwd => g_wallet_password |
| 80 | + ); |
| 81 | + |
| 82 | + l_http_status_code := apex_web_service.g_status_code; |
| 83 | + |
| 84 | + -- for possible error codes, see https://api.slack.com/changelog/2016-05-17-changes-to-errors-for-incoming-webhooks |
| 85 | + assert (l_http_status_code = 200, 'Request failed with HTTP error code ' || l_http_status_code || '. First 1K of response body: ' || substr(l_returnvalue, 1, 1000) ); |
| 86 | + |
| 87 | + return l_returnvalue; |
| 88 | + |
| 89 | +end make_request; |
| 90 | + |
| 91 | + |
| 92 | +procedure set_api_base_url (p_url in varchar2) |
| 93 | +as |
| 94 | +begin |
| 95 | + |
| 96 | + /* |
| 97 | + |
| 98 | + Purpose: set API base URL |
| 99 | + |
| 100 | + Remarks: useful if you need to use a proxy for HTTPS requests from the database |
| 101 | + see http://blog.rhjmartens.nl/2015/07/making-https-webservice-requests-from.html |
| 102 | + see http://ora-00001.blogspot.com/2016/04/how-to-set-up-iis-as-ssl-proxy-for-utl-http-in-oracle-xe.html |
| 103 | + |
| 104 | + Who Date Description |
| 105 | + ------ ---------- -------------------------------- |
| 106 | + MBR 17.02.2018 Created |
| 107 | + |
| 108 | + */ |
| 109 | + |
| 110 | + g_api_base_url := p_url; |
| 111 | + |
| 112 | +end set_api_base_url; |
| 113 | + |
| 114 | + |
| 115 | +procedure set_wallet (p_wallet_path in varchar2, |
| 116 | + p_wallet_password in varchar2) |
| 117 | +as |
| 118 | +begin |
| 119 | + |
| 120 | + /* |
| 121 | + |
| 122 | + Purpose: set SSL wallet properties |
| 123 | + |
| 124 | + Remarks: |
| 125 | + |
| 126 | + Who Date Description |
| 127 | + ------ ---------- -------------------------------- |
| 128 | + MBR 17.02.2018 Created |
| 129 | + |
| 130 | + */ |
| 131 | + |
| 132 | + g_wallet_path := p_wallet_path; |
| 133 | + g_wallet_password := p_wallet_password; |
| 134 | + |
| 135 | +end set_wallet; |
| 136 | + |
| 137 | + |
| 138 | +procedure set_webhook_host (p_host in varchar2) |
| 139 | +as |
| 140 | +begin |
| 141 | + |
| 142 | + /* |
| 143 | + |
| 144 | + Purpose: set webhook host |
| 145 | + |
| 146 | + Remarks: |
| 147 | + |
| 148 | + Who Date Description |
| 149 | + ------ ---------- -------------------------------- |
| 150 | + MBR 17.02.2018 Created |
| 151 | + |
| 152 | + */ |
| 153 | + |
| 154 | + g_webhook_host := p_host; |
| 155 | + |
| 156 | +end set_webhook_host; |
| 157 | + |
| 158 | + |
| 159 | +procedure set_webhook_path (p_path in varchar2) |
| 160 | +as |
| 161 | +begin |
| 162 | + |
| 163 | + /* |
| 164 | + |
| 165 | + Purpose: set webhook path |
| 166 | + |
| 167 | + Remarks: |
| 168 | + |
| 169 | + Who Date Description |
| 170 | + ------ ---------- -------------------------------- |
| 171 | + MBR 17.02.2018 Created |
| 172 | + |
| 173 | + */ |
| 174 | + |
| 175 | + g_webhook_path := p_path; |
| 176 | + |
| 177 | +end set_webhook_path; |
| 178 | + |
| 179 | + |
| 180 | +procedure send_message (p_text in varchar2) |
| 181 | +as |
| 182 | + l_response clob; |
| 183 | +begin |
| 184 | + |
| 185 | + /* |
| 186 | + |
| 187 | + Purpose: send message |
| 188 | + |
| 189 | + Remarks: |
| 190 | + |
| 191 | + Who Date Description |
| 192 | + ------ ---------- -------------------------------- |
| 193 | + MBR 17.02.2018 Created |
| 194 | + |
| 195 | + */ |
| 196 | + |
| 197 | + assert (g_webhook_host is not null, 'Webhook host not defined!'); |
| 198 | + assert (g_webhook_path is not null, 'Webhook path not defined!'); |
| 199 | + |
| 200 | + l_response := make_request ( |
| 201 | + p_url => g_webhook_host || g_webhook_path, |
| 202 | + p_body => '{ "text": ' || apex_json.stringify (p_text) || ' }' |
| 203 | + ); |
| 204 | + |
| 205 | + |
| 206 | +end send_message; |
| 207 | + |
| 208 | + |
| 209 | +end slack_util_pkg; |
| 210 | +/ |
| 211 | + |
0 commit comments