|
22 | 22 |
|
23 | 23 | package com.microsoft.graph.core; |
24 | 24 |
|
| 25 | +import com.google.gson.JsonObject; |
| 26 | +import com.microsoft.graph.http.CoreHttpProvider; |
25 | 27 | import com.microsoft.graph.http.IHttpProvider; |
| 28 | +import com.microsoft.graph.httpcore.HttpClients; |
| 29 | +import com.microsoft.graph.httpcore.ICoreAuthenticationProvider; |
| 30 | +import com.microsoft.graph.logger.DefaultLogger; |
26 | 31 | import com.microsoft.graph.logger.ILogger; |
| 32 | +import com.microsoft.graph.serializer.DefaultSerializer; |
27 | 33 | import com.microsoft.graph.serializer.ISerializer; |
28 | 34 |
|
29 | 35 | import javax.annotation.Nullable; |
30 | 36 | import javax.annotation.Nonnull; |
31 | 37 |
|
| 38 | +import okhttp3.OkHttpClient; |
| 39 | + |
32 | 40 | /** |
33 | 41 | * A client that communications with an OData service |
34 | 42 | */ |
35 | | -public abstract class BaseClient implements IBaseClient { |
| 43 | +public class BaseClient implements IBaseClient { |
| 44 | + /** |
| 45 | + * Restricted constructor |
| 46 | + */ |
| 47 | + protected BaseClient() { |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * The default endpoint for the Microsoft Graph Service |
| 52 | + */ |
| 53 | + public static final String DEFAULT_GRAPH_ENDPOINT = "https://graph.microsoft.com/v1.0"; |
| 54 | + |
| 55 | + /** |
| 56 | + * The current endpoint |
| 57 | + */ |
| 58 | + private String endpoint; |
| 59 | + |
| 60 | + @Override |
| 61 | + @Nonnull |
| 62 | + public String getServiceRoot() { |
| 63 | + if (endpoint == null) { |
| 64 | + endpoint = DEFAULT_GRAPH_ENDPOINT; |
| 65 | + } |
| 66 | + return endpoint; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void setServiceRoot(@Nonnull final String value) { |
| 71 | + endpoint = value; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Send a custom request to Graph |
| 76 | + * |
| 77 | + * @param url |
| 78 | + * the full URL to make a request with |
| 79 | + * @param responseType |
| 80 | + * the response class to deserialize the response into |
| 81 | + * @return the instance of this builder |
| 82 | + */ |
| 83 | + @Nonnull |
| 84 | + public <T> CustomRequestBuilder<T> customRequest(@Nonnull final String url, @Nonnull final Class<T> responseType) { |
| 85 | + return new CustomRequestBuilder<T>(getServiceRoot() + url, this, null, responseType); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Send a custom request to Graph |
| 90 | + * |
| 91 | + * @param url |
| 92 | + * the full URL to make a request with |
| 93 | + * @return the instance of this builder |
| 94 | + */ |
| 95 | + @Nonnull |
| 96 | + public CustomRequestBuilder<JsonObject> customRequest(@Nonnull final String url) { |
| 97 | + return new CustomRequestBuilder<JsonObject>(getServiceRoot() + url, this, null, |
| 98 | + JsonObject.class); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Gets the builder to start configuring the client |
| 103 | + * |
| 104 | + * @return builder to start configuring the client |
| 105 | + */ |
| 106 | + @Nonnull |
| 107 | + public static Builder<OkHttpClient> builder() { |
| 108 | + return new Builder<OkHttpClient>(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Builder to help configure the Graph service client |
| 113 | + */ |
| 114 | + public static final class Builder<httpClientType> { |
| 115 | + private ISerializer serializer; |
| 116 | + private IHttpProvider httpProvider; |
| 117 | + private ILogger logger; |
| 118 | + private httpClientType httpClient; |
| 119 | + private ICoreAuthenticationProvider auth; |
| 120 | + |
| 121 | + private ICoreAuthenticationProvider getAuth() { |
| 122 | + if(auth == null) { |
| 123 | + throw new NullPointerException("auth"); // TODO initialize to default once moved to core |
| 124 | + } else { |
| 125 | + return auth; |
| 126 | + } |
| 127 | + } |
| 128 | + private ILogger getLogger() { |
| 129 | + if(logger == null) { |
| 130 | + return new DefaultLogger(); |
| 131 | + } else { |
| 132 | + return logger; |
| 133 | + } |
| 134 | + } |
| 135 | + private ISerializer getSerializer() { |
| 136 | + if(serializer == null) { |
| 137 | + return new DefaultSerializer(getLogger()); |
| 138 | + } else { |
| 139 | + return serializer; |
| 140 | + } |
| 141 | + } |
| 142 | + @SuppressWarnings("unchecked") |
| 143 | + private httpClientType getHttpClient() { |
| 144 | + if(httpClient == null) { |
| 145 | + return (httpClientType)HttpClients.createDefault(getAuth()); |
| 146 | + } else { |
| 147 | + return httpClient; |
| 148 | + } |
| 149 | + } |
| 150 | + private IHttpProvider getHttpProvider() { |
| 151 | + if(httpProvider == null) { |
| 152 | + return new CoreHttpProvider(getSerializer(), getLogger(), (OkHttpClient)getHttpClient()); |
| 153 | + } else { |
| 154 | + return httpProvider; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Sets the serializer. |
| 160 | + * |
| 161 | + * @param serializer |
| 162 | + * the serializer |
| 163 | + * @return the instance of this builder |
| 164 | + */ |
| 165 | + @Nonnull |
| 166 | + public Builder<httpClientType> serializer(@Nonnull final ISerializer serializer) { |
| 167 | + checkNotNull(serializer, "serializer"); |
| 168 | + this.serializer = serializer; |
| 169 | + return this; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Sets the httpProvider |
| 174 | + * |
| 175 | + * @param httpProvider |
| 176 | + * the httpProvider |
| 177 | + * @return the instance of this builder |
| 178 | + */ |
| 179 | + @Nonnull |
| 180 | + public Builder<httpClientType> httpProvider(@Nonnull final IHttpProvider httpProvider) { |
| 181 | + checkNotNull(httpProvider, "httpProvider"); |
| 182 | + this.httpProvider = httpProvider; |
| 183 | + return this; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Sets the logger |
| 188 | + * |
| 189 | + * @param logger |
| 190 | + * the logger |
| 191 | + * @return the instance of this builder |
| 192 | + */ |
| 193 | + @Nonnull |
| 194 | + public Builder<httpClientType> logger(@Nonnull final ILogger logger) { |
| 195 | + checkNotNull(logger, "logger"); |
| 196 | + this.logger = logger; |
| 197 | + return this; |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Sets the http client |
| 202 | + * |
| 203 | + * @param client the http client |
| 204 | + * |
| 205 | + * @return the instance of this builder |
| 206 | + */ |
| 207 | + @Nonnull |
| 208 | + public Builder<httpClientType> httpClient(@Nonnull final httpClientType client) { |
| 209 | + checkNotNull(client, "client"); |
| 210 | + this.httpClient = client; |
| 211 | + return this; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Sets the authentication provider |
| 216 | + * |
| 217 | + * @param auth the authentication provider |
| 218 | + * @return the instance of this builder |
| 219 | + */ |
| 220 | + @Nonnull |
| 221 | + public Builder<httpClientType> authenticationProvider(@Nonnull final ICoreAuthenticationProvider auth) { |
| 222 | + checkNotNull(auth, "auth"); |
| 223 | + this.auth = auth; |
| 224 | + return this; |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Builds and returns the Graph service client. |
| 229 | + * |
| 230 | + * @return the Graph service client object |
| 231 | + * @throws ClientException |
| 232 | + * if there was an exception creating the client |
| 233 | + */ |
| 234 | + @Nonnull |
| 235 | + public IBaseClient buildClient() throws ClientException { |
| 236 | + BaseClient client = new BaseClient(); |
| 237 | + client.setHttpProvider(this.getHttpProvider()); |
| 238 | + client.setLogger(this.getLogger()); |
| 239 | + client.setSerializer(this.getSerializer()); |
| 240 | + client.validate(); |
| 241 | + return client; |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * Checks whether the provided object is null or not and throws an exception if it is |
| 247 | + * |
| 248 | + * @param o object to check |
| 249 | + * @param name name to use in the exception message |
| 250 | + */ |
| 251 | + protected static void checkNotNull(@Nullable final Object o, @Nonnull final String name) { |
| 252 | + if (o==null) { |
| 253 | + throw new NullPointerException(name + " cannot be null"); |
| 254 | + } |
| 255 | + } |
| 256 | + |
36 | 257 | /** |
37 | 258 | * The HTTP provider instance |
38 | 259 | */ |
|
0 commit comments