Skip to content

Commit 047c0af

Browse files
committed
OkHttpClient -> HttpsUrlConnection
1 parent 9f36d35 commit 047c0af

File tree

2 files changed

+55
-68
lines changed

2 files changed

+55
-68
lines changed

firebase-segmentation/firebase-segmentation.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ dependencies {
5151
implementation 'androidx.appcompat:appcompat:1.0.2'
5252
implementation 'androidx.multidex:multidex:2.0.1'
5353
implementation 'com.google.android.gms:play-services-tasks:17.0.0'
54-
implementation 'com.squareup.okhttp:okhttp:2.7.5'
5554

5655
compileOnly "com.google.auto.value:auto-value-annotations:1.6.5"
5756
annotationProcessor "com.google.auto.value:auto-value:1.6.2"

firebase-segmentation/src/main/java/com/google/firebase/segmentation/remote/SegmentationServiceClient.java

Lines changed: 55 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
package com.google.firebase.segmentation.remote;
1616

1717
import androidx.annotation.NonNull;
18-
import com.squareup.okhttp.MediaType;
19-
import com.squareup.okhttp.OkHttpClient;
20-
import com.squareup.okhttp.Request;
21-
import com.squareup.okhttp.RequestBody;
22-
import com.squareup.okhttp.Response;
2318
import java.io.IOException;
19+
import java.io.OutputStream;
20+
import java.net.URL;
21+
import javax.net.ssl.HttpsURLConnection;
2422
import org.json.JSONException;
2523
import org.json.JSONObject;
2624

@@ -35,10 +33,6 @@ public class SegmentationServiceClient {
3533
"projects/%s/installations/%s/customSegmentationData:clear";
3634
private static final String FIREBASE_SEGMENTATION_API_VERSION = "v1alpha";
3735

38-
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
39-
40-
private final OkHttpClient httpClient;
41-
4236
public enum Code {
4337
OK,
4438

@@ -53,10 +47,6 @@ public enum Code {
5347
UNAUTHORIZED,
5448
}
5549

56-
public SegmentationServiceClient() {
57-
httpClient = new OkHttpClient();
58-
}
59-
6050
@NonNull
6151
public Code updateCustomInstallationId(
6252
long projectNumber,
@@ -66,36 +56,35 @@ public Code updateCustomInstallationId(
6656
@NonNull String firebaseInstanceIdToken) {
6757
String resourceName =
6858
String.format(UPDATE_REQUEST_RESOURCE_NAME_FORMAT, projectNumber, firebaseInstanceId);
69-
70-
RequestBody requestBody;
7159
try {
72-
requestBody =
73-
RequestBody.create(
74-
JSON,
75-
buildUpdateCustomSegmentationDataRequestBody(resourceName, customInstallationId)
76-
.toString());
77-
} catch (JSONException e) {
78-
// This should never happen
79-
throw new IllegalStateException(e);
80-
}
81-
82-
Request request =
83-
new Request.Builder()
84-
.url(
85-
String.format(
86-
"https://%s/%s/%s?key=%s",
87-
FIREBASE_SEGMENTATION_API_DOMAIN,
88-
FIREBASE_SEGMENTATION_API_VERSION,
89-
resourceName,
90-
apiKey))
91-
.header("Authorization", "FIREBASE_INSTALLATIONS_AUTH " + firebaseInstanceIdToken)
92-
.header("Content-Type", "application/json")
93-
.patch(requestBody)
94-
.build();
60+
URL url =
61+
new URL(
62+
String.format(
63+
"https://%s/%s/%s?key=%s",
64+
FIREBASE_SEGMENTATION_API_DOMAIN,
65+
FIREBASE_SEGMENTATION_API_VERSION,
66+
resourceName,
67+
apiKey));
68+
69+
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
70+
httpsURLConnection.setDoOutput(true);
71+
httpsURLConnection.setRequestMethod("PATCH");
72+
httpsURLConnection.addRequestProperty(
73+
"Authorization", "FIREBASE_INSTALLATIONS_AUTH " + firebaseInstanceIdToken);
74+
httpsURLConnection.addRequestProperty("Content-Type", "application/json");
75+
OutputStream os = httpsURLConnection.getOutputStream();
76+
try {
77+
os.write(
78+
buildUpdateCustomSegmentationDataRequestBody(resourceName, customInstallationId)
79+
.toString()
80+
.getBytes("UTF-8"));
81+
} catch (JSONException e) {
82+
throw new IllegalStateException(e);
83+
}
84+
httpsURLConnection.connect();
9585

96-
try {
97-
Response response = httpClient.newCall(request).execute();
98-
switch (response.code()) {
86+
int httpResponseCode = httpsURLConnection.getResponseCode();
87+
switch (httpResponseCode) {
9988
case 200:
10089
return Code.OK;
10190
case 401:
@@ -126,34 +115,33 @@ public Code clearCustomInstallationId(
126115
@NonNull String firebaseInstanceIdToken) {
127116
String resourceName =
128117
String.format(CLEAR_REQUEST_RESOURCE_NAME_FORMAT, projectNumber, firebaseInstanceId);
129-
130-
RequestBody requestBody;
131118
try {
132-
requestBody =
133-
RequestBody.create(
134-
JSON, buildClearCustomSegmentationDataRequestBody(resourceName).toString());
135-
} catch (JSONException e) {
136-
// This should never happen
137-
throw new IllegalStateException(e);
138-
}
139-
140-
Request request =
141-
new Request.Builder()
142-
.url(
143-
String.format(
144-
"https://%s/%s/%s?key=%s",
145-
FIREBASE_SEGMENTATION_API_DOMAIN,
146-
FIREBASE_SEGMENTATION_API_VERSION,
147-
resourceName,
148-
apiKey))
149-
.header("Authorization", "FIREBASE_INSTALLATIONS_AUTH " + firebaseInstanceIdToken)
150-
.header("Content-Type", "application/json")
151-
.post(requestBody)
152-
.build();
119+
URL url =
120+
new URL(
121+
String.format(
122+
"https://%s/%s/%s?key=%s",
123+
FIREBASE_SEGMENTATION_API_DOMAIN,
124+
FIREBASE_SEGMENTATION_API_VERSION,
125+
resourceName,
126+
apiKey));
127+
128+
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
129+
httpsURLConnection.setDoOutput(true);
130+
httpsURLConnection.setRequestMethod("POST");
131+
httpsURLConnection.addRequestProperty(
132+
"Authorization", "FIREBASE_INSTALLATIONS_AUTH " + firebaseInstanceIdToken);
133+
httpsURLConnection.addRequestProperty("Content-Type", "application/json");
134+
OutputStream os = httpsURLConnection.getOutputStream();
135+
try {
136+
os.write(
137+
buildClearCustomSegmentationDataRequestBody(resourceName).toString().getBytes("UTF-8"));
138+
} catch (JSONException e) {
139+
throw new IllegalStateException(e);
140+
}
141+
httpsURLConnection.connect();
153142

154-
try {
155-
Response response = httpClient.newCall(request).execute();
156-
switch (response.code()) {
143+
int httpResponseCode = httpsURLConnection.getResponseCode();
144+
switch (httpResponseCode) {
157145
case 200:
158146
return Code.OK;
159147
case 401:

0 commit comments

Comments
 (0)