27
27
import java .io .ByteArrayOutputStream ;
28
28
import java .io .IOException ;
29
29
import java .io .InputStream ;
30
+ import java .io .OutputStream ;
30
31
import java .io .UnsupportedEncodingException ;
31
32
import java .util .HashMap ;
32
33
import java .util .Map ;
33
- import java .util .zip .GZIPOutputStream ;
34
34
import javax .net .ssl .HttpsURLConnection ;
35
35
import org .json .JSONException ;
36
36
import org .json .JSONObject ;
37
37
38
38
/** Client that makes FIS-authenticated GET and POST requests to the App Distribution Tester API. */
39
39
class TesterApiHttpClient {
40
40
41
+ /** Functional interface for a function that writes a request body to an output stream */
42
+ interface RequestBodyWriter {
43
+ void write (OutputStream os ) throws IOException ;
44
+ }
45
+
41
46
private static final String APP_TESTERS_HOST = "firebaseapptesters.googleapis.com" ;
42
47
private static final String REQUEST_METHOD_GET = "GET" ;
43
48
private static final String REQUEST_METHOD_POST = "POST" ;
44
49
private static final String CONTENT_TYPE_HEADER_KEY = "Content-Type" ;
45
50
private static final String JSON_CONTENT_TYPE = "application/json" ;
46
- private static final String CONTENT_ENCODING_HEADER_KEY = "Content-Encoding" ;
47
- private static final String GZIP_CONTENT_ENCODING = "gzip" ;
48
51
private static final String API_KEY_HEADER = "x-goog-api-key" ;
49
52
private static final String INSTALLATION_AUTH_HEADER = "X-Goog-Firebase-Installations-Auth" ;
50
53
private static final String X_ANDROID_PACKAGE_HEADER_KEY = "X-Android-Package" ;
@@ -110,7 +113,8 @@ JSONObject makePostRequest(String tag, String path, String token, String request
110
113
throw new FirebaseAppDistributionException (
111
114
"Unsupported encoding: " + UTF_8 , Status .UNKNOWN , e );
112
115
}
113
- return makePostRequest (tag , path , token , bytes , new HashMap <>());
116
+ return makePostRequest (
117
+ tag , path , token , new HashMap <>(), outputStream -> outputStream .write (bytes ));
114
118
}
115
119
116
120
/**
@@ -120,34 +124,38 @@ JSONObject makePostRequest(String tag, String path, String token, String request
120
124
*
121
125
* @return the response body
122
126
*/
123
- JSONObject makeUploadRequest (String tag , String path , String token , byte [] requestBody )
127
+ JSONObject makeUploadRequest (
128
+ String tag , String path , String token , RequestBodyWriter requestBodyWriter )
124
129
throws FirebaseAppDistributionException {
125
130
Map <String , String > extraHeaders = new HashMap <>();
126
131
extraHeaders .put (X_GOOG_UPLOAD_PROTOCOL_HEADER , X_GOOG_UPLOAD_PROTOCOL_RAW );
127
132
extraHeaders .put (X_GOOG_UPLOAD_FILE_NAME_HEADER , X_GOOG_UPLOAD_FILE_NAME );
128
- return makePostRequest (tag , path , token , requestBody , extraHeaders );
133
+ return makePostRequest (tag , path , token , extraHeaders , requestBodyWriter );
129
134
}
130
135
131
136
private JSONObject makePostRequest (
132
- String tag , String path , String token , byte [] requestBody , Map <String , String > extraHeaders )
137
+ String tag ,
138
+ String path ,
139
+ String token ,
140
+ Map <String , String > extraHeaders ,
141
+ RequestBodyWriter requestBodyWriter )
133
142
throws FirebaseAppDistributionException {
134
143
HttpsURLConnection connection = null ;
135
144
try {
136
145
connection = openHttpsUrlConnection (getTesterApiUrl (path ), token );
137
146
connection .setDoOutput (true );
138
147
connection .setRequestMethod (REQUEST_METHOD_POST );
139
148
connection .addRequestProperty (CONTENT_TYPE_HEADER_KEY , JSON_CONTENT_TYPE );
140
- connection .addRequestProperty (CONTENT_ENCODING_HEADER_KEY , GZIP_CONTENT_ENCODING );
141
149
for (Map .Entry <String , String > e : extraHeaders .entrySet ()) {
142
150
connection .addRequestProperty (e .getKey (), e .getValue ());
143
151
}
144
- GZIPOutputStream gzipOutputStream = new GZIPOutputStream ( connection .getOutputStream () );
152
+ OutputStream outputStream = connection .getOutputStream ();
145
153
try {
146
- gzipOutputStream .write (requestBody );
154
+ requestBodyWriter .write (outputStream );
147
155
} catch (IOException e ) {
148
- throw getException (tag , "Error compressing network request body" , Status .UNKNOWN , e );
156
+ throw getException (tag , "Error writing network request body" , Status .UNKNOWN , e );
149
157
} finally {
150
- gzipOutputStream .close ();
158
+ outputStream .close ();
151
159
}
152
160
return readResponse (tag , connection );
153
161
} catch (IOException e ) {
0 commit comments