use of org.thoughtcrime.securesms.net.StandardUserAgentInterceptor in project Signal-Android by signalapp.
the class SubmitDebugLogRepository method uploadContent.
@WorkerThread
@NonNull
private String uploadContent(@NonNull String contentType, @NonNull RequestBody requestBody) throws IOException {
try {
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new StandardUserAgentInterceptor()).dns(SignalServiceNetworkAccess.DNS).build();
Response response = client.newCall(new Request.Builder().url(API_ENDPOINT).get().build()).execute();
ResponseBody body = response.body();
if (!response.isSuccessful() || body == null) {
throw new IOException("Unsuccessful response: " + response);
}
JSONObject json = new JSONObject(body.string());
String url = json.getString("url");
JSONObject fields = json.getJSONObject("fields");
String item = fields.getString("key");
MultipartBody.Builder post = new MultipartBody.Builder();
Iterator<String> keys = fields.keys();
post.addFormDataPart("Content-Type", contentType);
while (keys.hasNext()) {
String key = keys.next();
post.addFormDataPart(key, fields.getString(key));
}
post.addFormDataPart("file", "file", requestBody);
Response postResponse = client.newCall(new Request.Builder().url(url).post(post.build()).build()).execute();
if (!postResponse.isSuccessful()) {
throw new IOException("Bad response: " + postResponse);
}
return API_ENDPOINT + "/" + item;
} catch (JSONException e) {
Log.w(TAG, "Error during upload.", e);
throw new IOException(e);
}
}
use of org.thoughtcrime.securesms.net.StandardUserAgentInterceptor in project Signal-Android by WhisperSystems.
the class SubmitDebugLogRepository method uploadContent.
@WorkerThread
@NonNull
private String uploadContent(@NonNull String contentType, @NonNull RequestBody requestBody) throws IOException {
try {
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new StandardUserAgentInterceptor()).dns(SignalServiceNetworkAccess.DNS).build();
Response response = client.newCall(new Request.Builder().url(API_ENDPOINT).get().build()).execute();
ResponseBody body = response.body();
if (!response.isSuccessful() || body == null) {
throw new IOException("Unsuccessful response: " + response);
}
JSONObject json = new JSONObject(body.string());
String url = json.getString("url");
JSONObject fields = json.getJSONObject("fields");
String item = fields.getString("key");
MultipartBody.Builder post = new MultipartBody.Builder();
Iterator<String> keys = fields.keys();
post.addFormDataPart("Content-Type", contentType);
while (keys.hasNext()) {
String key = keys.next();
post.addFormDataPart(key, fields.getString(key));
}
post.addFormDataPart("file", "file", requestBody);
Response postResponse = client.newCall(new Request.Builder().url(url).post(post.build()).build()).execute();
if (!postResponse.isSuccessful()) {
throw new IOException("Bad response: " + postResponse);
}
return API_ENDPOINT + "/" + item;
} catch (JSONException e) {
Log.w(TAG, "Error during upload.", e);
throw new IOException(e);
}
}
Aggregations