1 /* 2 * Copyright (C) 2023 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.adservices.service.common.httpclient; 18 19 import android.annotation.NonNull; 20 import android.net.Uri; 21 22 import com.android.adservices.service.common.httpclient.AdServicesHttpUtil.HttpMethodType; 23 import com.android.adservices.service.devapi.DevContext; 24 25 import com.google.auto.value.AutoValue; 26 import com.google.common.collect.ImmutableMap; 27 import com.google.common.collect.ImmutableSet; 28 29 /** Input to the {@link AdServicesHttpsClient}, which makes request to download content */ 30 @AutoValue 31 public abstract class AdServicesHttpClientRequest { 32 33 /** @return uri that is used to make the request. */ getUri()34 public abstract Uri getUri(); 35 36 /** @return request properties that need to be piggybacked to the url connection */ getRequestProperties()37 public abstract ImmutableMap<String, String> getRequestProperties(); 38 39 /** @return set of keys that we want in the {@link AdServicesHttpClientResponse} */ getResponseHeaderKeys()40 public abstract ImmutableSet<String> getResponseHeaderKeys(); 41 42 /** @return boolean is the results should be cached or not */ getUseCache()43 public abstract boolean getUseCache(); 44 45 /** @return The http method type {@link HttpMethodType} */ getHttpMethodType()46 public abstract HttpMethodType getHttpMethodType(); 47 48 /** @return The byte array that will be used as a request body for this request. */ 49 @SuppressWarnings("mutable") getBodyInBytes()50 public abstract byte[] getBodyInBytes(); 51 52 /** @return DevContext associated with this call. */ 53 @NonNull getDevContext()54 public abstract DevContext getDevContext(); 55 56 /** 57 * @param uri see {@link #getUri()} 58 * @param requestProperties see {@link #getRequestProperties()} 59 * @param responseHeaderKeys see {@link #getResponseHeaderKeys()} 60 * @param useCache see {@link #getUseCache()} 61 * @param httpMethodType see {@link #getHttpMethodType()} 62 * @param body see {@link #getBodyInBytes()}. This must be an empty array of bytes for GET 63 * request. 64 * @return an instance of {@link AdServicesHttpClientRequest} 65 */ create( Uri uri, ImmutableMap<String, String> requestProperties, ImmutableSet<String> responseHeaderKeys, boolean useCache, @NonNull DevContext devContext, HttpMethodType httpMethodType, byte[] body)66 public static AdServicesHttpClientRequest create( 67 Uri uri, 68 ImmutableMap<String, String> requestProperties, 69 ImmutableSet<String> responseHeaderKeys, 70 boolean useCache, 71 @NonNull DevContext devContext, 72 HttpMethodType httpMethodType, 73 byte[] body) { 74 if (body.length > 0) { 75 if (httpMethodType != HttpMethodType.POST) { 76 throw new IllegalArgumentException( 77 "Request method does not allow request mBody: " + httpMethodType); 78 } 79 } 80 return builder() 81 .setUri(uri) 82 .setRequestProperties(requestProperties) 83 .setResponseHeaderKeys(responseHeaderKeys) 84 .setUseCache(useCache) 85 .setDevContext(devContext) 86 .setBodyInBytes(body) 87 .setHttpMethodType(httpMethodType) 88 .build(); 89 } 90 91 /** @return a builder that can be used to build an {@link AdServicesHttpClientRequest} */ builder()92 public static AdServicesHttpClientRequest.Builder builder() { 93 return new AutoValue_AdServicesHttpClientRequest.Builder() 94 .setRequestProperties(ImmutableMap.of()) 95 .setResponseHeaderKeys(ImmutableSet.of()) 96 .setUseCache(false) 97 .setHttpMethodType(HttpMethodType.GET) 98 .setBodyInBytes(new byte[0]); 99 } 100 101 /** Builder that cane be used to build an {@link AdServicesHttpClientRequest} */ 102 @AutoValue.Builder 103 public abstract static class Builder { 104 105 /** 106 * @param uri that is used to make the request 107 */ setUri(Uri uri)108 public abstract AdServicesHttpClientRequest.Builder setUri(Uri uri); 109 110 /** 111 * @param queryParams that need to be piggybacked to the url connection 112 */ setRequestProperties( ImmutableMap<String, String> queryParams)113 public abstract AdServicesHttpClientRequest.Builder setRequestProperties( 114 ImmutableMap<String, String> queryParams); 115 116 /** 117 * @param responseHeaderKeys set of keys that we want in the {@link 118 * AdServicesHttpClientResponse} 119 */ setResponseHeaderKeys( ImmutableSet<String> responseHeaderKeys)120 public abstract AdServicesHttpClientRequest.Builder setResponseHeaderKeys( 121 ImmutableSet<String> responseHeaderKeys); 122 123 /** 124 * @param useCache flag to cache the response of this request 125 */ setUseCache(boolean useCache)126 public abstract AdServicesHttpClientRequest.Builder setUseCache(boolean useCache); 127 128 /** 129 * @param methodType type of HTTP request 130 */ setHttpMethodType(HttpMethodType methodType)131 public abstract Builder setHttpMethodType(HttpMethodType methodType); 132 133 /** 134 * @param bodyInBytes byte array that is part of the request 135 */ setBodyInBytes(byte[] bodyInBytes)136 public abstract Builder setBodyInBytes(byte[] bodyInBytes); 137 138 /** 139 * @param devContext indicates the dev context associated with this request. 140 */ setDevContext(DevContext devContext)141 public abstract AdServicesHttpClientRequest.Builder setDevContext(DevContext devContext); 142 143 /** 144 * @return an {@link AdServicesHttpClientRequest} 145 */ build()146 public abstract AdServicesHttpClientRequest build(); 147 } 148 } 149