|
4 | 4 | import java.io.IOException;
|
5 | 5 | import java.io.InputStream;
|
6 | 6 | import java.io.InputStreamReader;
|
| 7 | +import java.io.OutputStream; |
7 | 8 | import java.net.HttpURLConnection;
|
8 | 9 | import java.net.MalformedURLException;
|
9 | 10 | import java.net.ProtocolException;
|
10 | 11 | import java.net.URL;
|
| 12 | +import java.util.HashMap; |
11 | 13 | import java.util.Map;
|
12 | 14 | import java.util.Map.Entry;
|
13 | 15 |
|
| 16 | +import com.speechscrubber.Utils; |
| 17 | + |
14 | 18 | public class HttpUtils {
|
15 | 19 |
|
| 20 | + public Map<String, String> getAuthorizationHeader() { |
| 21 | + Map<String, String> requestProperties = new HashMap<String, String>(); |
| 22 | + requestProperties.put("Authorization", "Bearer " + Utils.getApiKey()); |
| 23 | + return requestProperties; |
| 24 | + } |
| 25 | + |
16 | 26 | public String sendGetRequest(String targetUrl) {
|
17 | 27 | return sendGetRequest(targetUrl, null);
|
18 | 28 | }
|
@@ -51,30 +61,66 @@ public String sendPostRequest(String targetUrl) {
|
51 | 61 | return sendPostRequest(targetUrl, null);
|
52 | 62 | }
|
53 | 63 |
|
54 |
| - HttpURLConnection createHttpUrlConnection(String targetUrl, Map<String, String> requestProperties, String requestType) throws MalformedURLException, IOException, ProtocolException { |
| 64 | + public String sendPostRequest(String targetUrl, Map<String, String> requestProperties, Object body) { |
| 65 | + HttpURLConnection connection = null; |
| 66 | + try { |
| 67 | + connection = createHttpUrlConnection(targetUrl, requestProperties, "POST"); |
| 68 | + connection.setDoOutput(true); |
| 69 | + // TODO - ayoho |
| 70 | + // See https://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests/2793153#2793153 |
| 71 | + // connection.setFixedLengthStreamingMode(contentLength); |
| 72 | + connection.setRequestProperty("Content-Type", "audio/mpeg"); |
| 73 | + |
| 74 | + try (OutputStream output = connection.getOutputStream()) { |
| 75 | + // output.write(query.getBytes(charset)); |
| 76 | + } |
| 77 | + |
| 78 | + return readConnectionResponse(connection); |
| 79 | + } catch (Exception e) { |
| 80 | + e.printStackTrace(); |
| 81 | + } finally { |
| 82 | + if (connection != null) { |
| 83 | + connection.disconnect(); |
| 84 | + } |
| 85 | + } |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + public HttpURLConnection createHttpUrlConnection(String targetUrl, Map<String, String> requestProperties, String requestType) throws MalformedURLException, IOException, ProtocolException { |
55 | 90 | URL url = new URL(targetUrl);
|
56 | 91 | HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
57 | 92 | connection.setRequestMethod(requestType);
|
58 | 93 | connection.setUseCaches(false);
|
59 |
| - connection.setDoOutput(false); |
| 94 | + if (requestType != null && "post".equalsIgnoreCase(requestType)) { |
| 95 | + connection.setDoOutput(true); |
| 96 | + } |
60 | 97 | if (requestProperties != null) {
|
61 | 98 | for (Entry<String, String> entry : requestProperties.entrySet()) {
|
62 | 99 | connection.setRequestProperty(entry.getKey(), entry.getValue());
|
63 | 100 | }
|
64 | 101 | }
|
65 |
| - // connection.setRequestProperty("Authorization", "Bearer " + getApiKey()); |
66 | 102 | return connection;
|
67 | 103 | }
|
68 | 104 |
|
69 |
| - String readConnectionResponse(HttpURLConnection connection) throws IOException { |
70 |
| - InputStream is = connection.getInputStream(); |
| 105 | + public String readConnectionResponse(HttpURLConnection connection) throws IOException { |
| 106 | + int responseStatus = connection.getResponseCode(); |
| 107 | + InputStream is; |
| 108 | + if (responseStatus > 299) { |
| 109 | + System.out.println("Got an error status (" + responseStatus + ")"); |
| 110 | + is = connection.getErrorStream(); |
| 111 | + } else { |
| 112 | + System.out.println("Got a successful status (" + responseStatus + ")"); |
| 113 | + is = connection.getInputStream(); |
| 114 | + } |
| 115 | + |
71 | 116 | BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
72 | 117 | StringBuilder response = new StringBuilder();
|
73 | 118 | String line;
|
74 | 119 | while ((line = reader.readLine()) != null) {
|
75 | 120 | response.append(line);
|
76 | 121 | }
|
77 | 122 | reader.close();
|
| 123 | + System.out.println("Response: " + response.toString()); |
78 | 124 | return response.toString();
|
79 | 125 | }
|
80 | 126 |
|
|
0 commit comments