Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame^] | 1 | package com.mirantis.mk |
| 2 | /** |
| 3 | * |
| 4 | * HTTP functions |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Make generic HTTP call and return parsed JSON |
| 10 | * |
| 11 | * @param url URL to make the request against |
| 12 | * @param method HTTP method to use (default GET) |
| 13 | * @param data JSON data to POST or PUT |
| 14 | * @param headers Map of additional request headers |
| 15 | */ |
| 16 | @NonCPS |
| 17 | def sendHttpRequest(url, method = 'GET', data = null, headers = [:]) { |
| 18 | |
| 19 | def connection = new URL(url).openConnection() |
| 20 | if (method != 'GET') { |
| 21 | connection.setRequestMethod(method) |
| 22 | } |
| 23 | |
| 24 | if (data) { |
| 25 | headers['Content-Type'] = 'application/json' |
| 26 | } |
| 27 | |
| 28 | headers['User-Agent'] = 'jenkins-groovy' |
| 29 | headers['Accept'] = 'application/json' |
| 30 | |
| 31 | for (header in headers) { |
| 32 | connection.setRequestProperty(header.key, header.value) |
| 33 | } |
| 34 | |
| 35 | if (data) { |
| 36 | connection.setDoOutput(true) |
| 37 | if (data instanceof String) { |
| 38 | dataStr = data |
| 39 | } else { |
| 40 | dataStr = new groovy.json.JsonBuilder(data).toString() |
| 41 | } |
| 42 | def output = new OutputStreamWriter(connection.outputStream) |
| 43 | //infoMsg("[HTTP] Request URL: ${url}, method: ${method}, headers: ${headers}, content: ${dataStr}") |
| 44 | output.write(dataStr) |
| 45 | output.close() |
| 46 | } |
| 47 | |
| 48 | if ( connection.responseCode == 200 ) { |
| 49 | response = connection.inputStream.text |
| 50 | try { |
| 51 | response_content = new groovy.json.JsonSlurperClassic().parseText(response) |
| 52 | } catch (groovy.json.JsonException e) { |
| 53 | response_content = response |
| 54 | } |
| 55 | //successMsg("[HTTP] Response: code ${connection.responseCode}") |
| 56 | return response_content |
| 57 | } else { |
| 58 | //errorMsg("[HTTP] Response: code ${connection.responseCode}") |
| 59 | throw new Exception(connection.responseCode + ": " + connection.inputStream.text) |
| 60 | } |
| 61 | |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Make HTTP GET request |
| 66 | * |
| 67 | * @param url URL which will requested |
| 68 | * @param data JSON data to PUT |
| 69 | */ |
| 70 | def sendHttpGetRequest(url, data = null, headers = [:]) { |
| 71 | return sendHttpRequest(url, 'GET', data, headers) |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Make HTTP POST request |
| 76 | * |
| 77 | * @param url URL which will requested |
| 78 | * @param data JSON data to PUT |
| 79 | */ |
| 80 | def sendHttpPostRequest(url, data = null, headers = [:]) { |
| 81 | return sendHttpRequest(url, 'POST', data, headers) |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Make HTTP PUT request |
| 86 | * |
| 87 | * @param url URL which will requested |
| 88 | * @param data JSON data to PUT |
| 89 | */ |
| 90 | def sendHttpPutRequest(url, data = null, headers = [:]) { |
| 91 | return sendHttpRequest(url, 'PUT', data, headers) |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Make HTTP DELETE request |
| 96 | * |
| 97 | * @param url URL which will requested |
| 98 | * @param data JSON data to PUT |
| 99 | */ |
| 100 | def sendHttpDeleteRequest(url, data = null, headers = [:]) { |
| 101 | return sendHttpRequest(url, 'DELETE', data, headers) |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Make generic call using Salt REST API and return parsed JSON |
| 106 | * |
| 107 | * @param master Salt connection object |
| 108 | * @param uri URI which will be appended to Salt server base URL |
| 109 | * @param method HTTP method to use (default GET) |
| 110 | * @param data JSON data to POST or PUT |
| 111 | * @param headers Map of additional request headers |
| 112 | */ |
| 113 | def restCall(master, uri, method = 'GET', data = null, headers = [:]) { |
| 114 | def connection = new URL("${master.url}${uri}").openConnection() |
| 115 | if (method != 'GET') { |
| 116 | connection.setRequestMethod(method) |
| 117 | } |
| 118 | |
| 119 | connection.setRequestProperty('User-Agent', 'jenkins-groovy') |
| 120 | connection.setRequestProperty('Accept', 'application/json') |
| 121 | if (master.authToken) { |
| 122 | // XXX: removeme |
| 123 | connection.setRequestProperty('X-Auth-Token', master.authToken) |
| 124 | } |
| 125 | |
| 126 | for (header in headers) { |
| 127 | connection.setRequestProperty(header.key, header.value) |
| 128 | } |
| 129 | |
| 130 | if (data) { |
| 131 | connection.setDoOutput(true) |
| 132 | if (data instanceof String) { |
| 133 | dataStr = data |
| 134 | } else { |
| 135 | connection.setRequestProperty('Content-Type', 'application/json') |
| 136 | dataStr = new groovy.json.JsonBuilder(data).toString() |
| 137 | } |
| 138 | def out = new OutputStreamWriter(connection.outputStream) |
| 139 | out.write(dataStr) |
| 140 | out.close() |
| 141 | } |
| 142 | |
| 143 | if ( connection.responseCode >= 200 && connection.responseCode < 300 ) { |
| 144 | res = connection.inputStream.text |
| 145 | try { |
| 146 | return new groovy.json.JsonSlurperClassic().parseText(res) |
| 147 | } catch (Exception e) { |
| 148 | return res |
| 149 | } |
| 150 | } else { |
| 151 | throw new Exception(connection.responseCode + ": " + connection.inputStream.text) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Make GET request using Salt REST API and return parsed JSON |
| 157 | * |
| 158 | * @param master Salt connection object |
| 159 | * @param uri URI which will be appended to Salt server base URL |
| 160 | */ |
| 161 | def restGet(master, uri, data = null) { |
| 162 | return restCall(master, uri, 'GET', data) |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Make POST request using Salt REST API and return parsed JSON |
| 167 | * |
| 168 | * @param master Salt connection object |
| 169 | * @param uri URI which will be appended to Docker server base URL |
| 170 | * @param data JSON Data to PUT |
| 171 | */ |
| 172 | def restPost(master, uri, data = null) { |
| 173 | return restCall(master, uri, 'POST', data, ['Accept': '*/*']) |
| 174 | } |