• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.libraries.entitlement.http;
18 
19 import android.net.Network;
20 
21 import androidx.annotation.Nullable;
22 
23 import com.android.libraries.entitlement.CarrierConfig;
24 
25 import com.google.auto.value.AutoValue;
26 import com.google.common.collect.ImmutableListMultimap;
27 
28 import org.json.JSONObject;
29 
30 import java.util.List;
31 
32 /** The parameters of an http request. */
33 @AutoValue
34 public abstract class HttpRequest {
35     /** The URL. */
url()36     public abstract String url();
37 
38     /** The HTTP request method, like "GET" or "POST". */
requestMethod()39     public abstract String requestMethod();
40 
41     /** For "POST" request method, the body of the request in JSON format. */
postData()42     public abstract JSONObject postData();
43 
44     /** HTTP header fields. */
requestProperties()45     public abstract ImmutableListMultimap<String, String> requestProperties();
46 
47     /** The client side timeout, in seconds. See {@link Builder#setTimeoutInSec}. */
timeoutInSec()48     public abstract int timeoutInSec();
49 
50     /** The network used for this HTTP connection. See {@link Builder#setNetwork}. */
51     @Nullable
network()52     public abstract Network network();
53 
54     /** Builder of {@link HttpRequest}. */
55     @AutoValue.Builder
56     public abstract static class Builder {
build()57         public abstract HttpRequest build();
58 
59         /** Sets the URL. */
setUrl(String url)60         public abstract Builder setUrl(String url);
61 
62         /**
63          * Sets the HTTP request method, like "GET" or "POST".
64          *
65          * @see HttpConstants.RequestMethod
66          */
setRequestMethod(String requestMethod)67         public abstract Builder setRequestMethod(String requestMethod);
68 
69         /** For "POST" request method, sets the body of the request in JSON format. */
setPostData(JSONObject postData)70         public abstract Builder setPostData(JSONObject postData);
71 
requestPropertiesBuilder()72         abstract ImmutableListMultimap.Builder<String, String> requestPropertiesBuilder();
73 
74         /** Adds an HTTP header field. */
addRequestProperty(String key, String value)75         public Builder addRequestProperty(String key, String value) {
76             requestPropertiesBuilder().put(key, value);
77             return this;
78         }
79 
80         /**
81           * Adds an HTTP header field with multiple values. Equivalent to calling
82           * {@link #addRequestProperty(String, String)} multiple times with the same key and
83           * one value at a time.
84           */
addRequestProperty(String key, List<String> value)85         public Builder addRequestProperty(String key, List<String> value) {
86             requestPropertiesBuilder().putAll(key, value);
87             return this;
88         }
89 
90         /**
91          * Sets the client side timeout for HTTP connection. Default to
92          * {@link com.android.libraries.entitlement.CarrierConfig#DEFAULT_TIMEOUT_IN_SEC}.
93          *
94          * <p>This timeout is used by both {@link java.net.URLConnection#setConnectTimeout} and
95          * {@link java.net.URLConnection#setReadTimeout}.
96          */
setTimeoutInSec(int timeoutInSec)97         public abstract Builder setTimeoutInSec(int timeoutInSec);
98 
99         /**
100          * Sets the network used for this HTTP connection. If not set, the device default network
101          * is used.
102          */
setNetwork(@ullable Network network)103         public abstract Builder setNetwork(@Nullable Network network);
104     }
105 
builder()106     public static Builder builder() {
107         return new AutoValue_HttpRequest.Builder()
108                 .setUrl("")
109                 .setRequestMethod("")
110                 .setPostData(new JSONObject())
111                 .setTimeoutInSec(CarrierConfig.DEFAULT_TIMEOUT_IN_SEC);
112     }
113 }
114