|
20 | 20 | */ |
21 | 21 | package com.adyen.httpclient; |
22 | 22 |
|
| 23 | +import com.adyen.Client; |
| 24 | +import com.adyen.Config; |
| 25 | +import org.apache.commons.codec.binary.Base64; |
| 26 | + |
23 | 27 | import java.io.IOException; |
24 | 28 | import java.io.InputStream; |
25 | 29 | import java.io.OutputStream; |
|
30 | 34 | import java.net.URLEncoder; |
31 | 35 | import java.util.Map; |
32 | 36 | import java.util.Scanner; |
33 | | -import org.apache.commons.codec.binary.Base64; |
34 | | -import com.adyen.Client; |
35 | | -import com.adyen.Config; |
36 | 37 |
|
37 | 38 | public class HttpURLConnectionClient implements ClientInterface { |
38 | 39 | private static final String CHARSET = "UTF-8"; |
39 | 40 | private Proxy proxy; |
40 | 41 |
|
| 42 | + /** |
| 43 | + * Does a POST request. |
| 44 | + * config is used to obtain basic auth username, password and User-Agent |
| 45 | + */ |
41 | 46 | /** |
42 | 47 | * Does a POST request. |
43 | 48 | * config is used to obtain basic auth username, password and User-Agent |
44 | 49 | */ |
45 | 50 | @Override |
46 | 51 | public String request(String requestUrl, String requestBody, Config config) throws IOException, HTTPClientException { |
| 52 | + return request(requestUrl, requestBody, config, false); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public String request(String requestUrl, String requestBody, Config config, boolean isApiKeyRequired) throws IOException, HTTPClientException { |
47 | 57 | HttpURLConnection httpConnection = createRequest(requestUrl, config.getApplicationName()); |
48 | | - setBasicAuthentication(httpConnection, config.getUsername(), config.getPassword()); |
| 58 | + if (isApiKeyRequired) { |
| 59 | + setApiKey(httpConnection, config.getApiKey()); |
| 60 | + } else { |
| 61 | + setBasicAuthentication(httpConnection, config.getUsername(), config.getPassword()); |
| 62 | + } |
49 | 63 | setContentType(httpConnection, "application/json"); |
50 | | - |
51 | 64 | String response = doPostRequest(httpConnection, requestBody); |
52 | 65 |
|
53 | 66 | return response; |
@@ -140,6 +153,16 @@ private HttpURLConnection setContentType(HttpURLConnection httpConnection, Strin |
140 | 153 | return httpConnection; |
141 | 154 | } |
142 | 155 |
|
| 156 | + /** |
| 157 | + * Sets api key |
| 158 | + */ |
| 159 | + private HttpURLConnection setApiKey(HttpURLConnection httpConnection, String apiKey) { |
| 160 | + if (apiKey != null && !apiKey.isEmpty()) { |
| 161 | + httpConnection.setRequestProperty("x-api-key", apiKey); |
| 162 | + } |
| 163 | + return httpConnection; |
| 164 | + } |
| 165 | + |
143 | 166 | /** |
144 | 167 | * Does a POST request with raw body |
145 | 168 | */ |
|
0 commit comments