15
15
package com .google .firebase .segmentation .remote ;
16
16
17
17
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 ;
23
18
import java .io .IOException ;
19
+ import java .io .OutputStream ;
20
+ import java .net .URL ;
21
+ import javax .net .ssl .HttpsURLConnection ;
24
22
import org .json .JSONException ;
25
23
import org .json .JSONObject ;
26
24
@@ -35,10 +33,6 @@ public class SegmentationServiceClient {
35
33
"projects/%s/installations/%s/customSegmentationData:clear" ;
36
34
private static final String FIREBASE_SEGMENTATION_API_VERSION = "v1alpha" ;
37
35
38
- private static final MediaType JSON = MediaType .parse ("application/json; charset=utf-8" );
39
-
40
- private final OkHttpClient httpClient ;
41
-
42
36
public enum Code {
43
37
OK ,
44
38
@@ -53,10 +47,6 @@ public enum Code {
53
47
UNAUTHORIZED ,
54
48
}
55
49
56
- public SegmentationServiceClient () {
57
- httpClient = new OkHttpClient ();
58
- }
59
-
60
50
@ NonNull
61
51
public Code updateCustomInstallationId (
62
52
long projectNumber ,
@@ -66,36 +56,35 @@ public Code updateCustomInstallationId(
66
56
@ NonNull String firebaseInstanceIdToken ) {
67
57
String resourceName =
68
58
String .format (UPDATE_REQUEST_RESOURCE_NAME_FORMAT , projectNumber , firebaseInstanceId );
69
-
70
- RequestBody requestBody ;
71
59
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 ();
95
85
96
- try {
97
- Response response = httpClient .newCall (request ).execute ();
98
- switch (response .code ()) {
86
+ int httpResponseCode = httpsURLConnection .getResponseCode ();
87
+ switch (httpResponseCode ) {
99
88
case 200 :
100
89
return Code .OK ;
101
90
case 401 :
@@ -126,34 +115,33 @@ public Code clearCustomInstallationId(
126
115
@ NonNull String firebaseInstanceIdToken ) {
127
116
String resourceName =
128
117
String .format (CLEAR_REQUEST_RESOURCE_NAME_FORMAT , projectNumber , firebaseInstanceId );
129
-
130
- RequestBody requestBody ;
131
118
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 ();
153
142
154
- try {
155
- Response response = httpClient .newCall (request ).execute ();
156
- switch (response .code ()) {
143
+ int httpResponseCode = httpsURLConnection .getResponseCode ();
144
+ switch (httpResponseCode ) {
157
145
case 200 :
158
146
return Code .OK ;
159
147
case 401 :
0 commit comments