Skip to content

Commit 59d5283

Browse files
committed
- merges BaseGraphServiceClient and BaseClient
1 parent b637452 commit 59d5283

8 files changed

Lines changed: 259 additions & 297 deletions

File tree

‎src/main/java/com/microsoft/graph/concurrency/ChunkedUploadProvider.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import com.microsoft.graph.concurrency.ChunkedUploadResult;
2828
import com.microsoft.graph.concurrency.IUploadSession;
2929
import com.microsoft.graph.core.ClientException;
30-
import com.microsoft.graph.core.IBaseGraphServiceClient;
30+
import com.microsoft.graph.core.IBaseClient;
3131
import com.microsoft.graph.options.Option;
3232

3333
import java.io.IOException;
@@ -66,7 +66,7 @@ public class ChunkedUploadProvider<UploadType> {
6666
/**
6767
* The client
6868
*/
69-
private final IBaseGraphServiceClient client;
69+
private final IBaseClient client;
7070

7171
/**
7272
* The input stream
@@ -103,7 +103,7 @@ public class ChunkedUploadProvider<UploadType> {
103103
* @param uploadTypeClass the upload type class
104104
*/
105105
public ChunkedUploadProvider(@Nonnull final IUploadSession uploadSession,
106-
@Nonnull final IBaseGraphServiceClient client,
106+
@Nonnull final IBaseClient client,
107107
@Nonnull final InputStream inputStream,
108108
final long streamSize,
109109
@Nonnull final Class<UploadType> uploadTypeClass) {

‎src/main/java/com/microsoft/graph/concurrency/ChunkedUploadRequest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import com.microsoft.graph.core.ClientException;
1515
import com.microsoft.graph.http.BaseRequest;
1616
import com.microsoft.graph.http.HttpMethod;
17-
import com.microsoft.graph.core.IBaseGraphServiceClient;
17+
import com.microsoft.graph.core.IBaseClient;
1818
import com.microsoft.graph.options.Option;
1919

2020
/**
@@ -56,7 +56,7 @@ public class ChunkedUploadRequest<UploadType> {
5656
*/
5757
@SuppressWarnings("unchecked")
5858
public ChunkedUploadRequest(@Nonnull final String requestUrl,
59-
@Nonnull final IBaseGraphServiceClient client,
59+
@Nonnull final IBaseClient client,
6060
@Nullable final List<? extends Option> options,
6161
@Nonnull final byte[] chunk,
6262
final int chunkSize,

‎src/main/java/com/microsoft/graph/core/BaseClient.java‎

Lines changed: 222 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,238 @@
2222

2323
package com.microsoft.graph.core;
2424

25+
import com.google.gson.JsonObject;
26+
import com.microsoft.graph.http.CoreHttpProvider;
2527
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;
2631
import com.microsoft.graph.logger.ILogger;
32+
import com.microsoft.graph.serializer.DefaultSerializer;
2733
import com.microsoft.graph.serializer.ISerializer;
2834

2935
import javax.annotation.Nullable;
3036
import javax.annotation.Nonnull;
3137

38+
import okhttp3.OkHttpClient;
39+
3240
/**
3341
* A client that communications with an OData service
3442
*/
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+
36257
/**
37258
* The HTTP provider instance
38259
*/

0 commit comments

Comments
 (0)