Skip to content

Commit 4580157

Browse files
author
Caitlin Bales (MSFT)
authored
Merge pull request #60 from davidmoten/rework-client-2
rework GraphServiceClient
2 parents 1758380 + 846c4d0 commit 4580157

File tree

9 files changed

+424
-76
lines changed

9 files changed

+424
-76
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,10 @@ For an example of authentication in a client application, see the [MSGraph SDK A
4343
After you have set the correct application ID and URL, you must get a **GraphServiceClient** object to make requests against the service. The SDK stores the account information for you, but when a user signs in for the first time, it invokes the UI to get the user's account information.
4444

4545
```java
46-
IClientConfig clientConfig =
47-
DefaultClientConfig.createWithAuthenticationProvider(mAuthenticationProvider);
48-
4946
IGraphServiceClient graphClient =
50-
GraphServiceClient.builder()
51-
.fromConfig(mClientConfig)
47+
GraphServiceClient
48+
.builder()
49+
.authenticationProvider(authenticationProvider)
5250
.buildClient();
5351
```
5452

src/main/java/com/microsoft/graph/concurrency/DefaultExecutors.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package com.microsoft.graph.concurrency;
2424

2525
import com.microsoft.graph.logger.ILogger;
26+
import com.google.common.annotations.VisibleForTesting;
2627
import com.microsoft.graph.core.ClientException;
2728

2829
import java.util.concurrent.Executors;
@@ -139,4 +140,9 @@ public void run() {
139140
});
140141
}
141142

143+
@VisibleForTesting
144+
public ILogger getLogger() {
145+
return logger;
146+
}
147+
142148
}

src/main/java/com/microsoft/graph/core/DefaultClientConfig.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@
3737
*/
3838
public abstract class DefaultClientConfig implements IClientConfig {
3939

40-
/**
41-
* The authentication provider instance
42-
*/
43-
private IAuthenticationProvider authenticationProvider;
44-
4540
/**
4641
* The executors instance
4742
*/
@@ -72,8 +67,12 @@ public static IClientConfig createWithAuthenticationProvider(
7267
final IAuthenticationProvider authenticationProvider
7368
) {
7469
DefaultClientConfig config = new DefaultClientConfig() {
70+
71+
@Override
72+
public IAuthenticationProvider getAuthenticationProvider() {
73+
return authenticationProvider;
74+
}
7575
};
76-
config.authenticationProvider = authenticationProvider;
7776
config.getLogger()
7877
.logDebug(
7978
"Using provided auth provider "
@@ -90,10 +89,7 @@ public static IClientConfig createWithAuthenticationProvider(
9089
* @return the authentication provider
9190
*/
9291
@Override
93-
public IAuthenticationProvider getAuthenticationProvider() {
94-
return authenticationProvider;
95-
}
96-
92+
public abstract IAuthenticationProvider getAuthenticationProvider();
9793
/**
9894
* Gets the HTTP provider
9995
*

src/main/java/com/microsoft/graph/http/DefaultHttpProvider.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,4 +453,19 @@ static boolean hasHeader(List<HeaderOption> headers, String header) {
453453
}
454454
return false;
455455
}
456+
457+
@VisibleForTesting
458+
public ILogger getLogger() {
459+
return logger;
460+
}
461+
462+
@VisibleForTesting
463+
public IExecutors getExecutors() {
464+
return executors;
465+
}
466+
467+
@VisibleForTesting
468+
public IAuthenticationProvider getAuthenticationProvider() {
469+
return authenticationProvider;
470+
}
456471
}

src/main/java/com/microsoft/graph/requests/extensions/GraphServiceClient.java

Lines changed: 135 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -27,124 +27,199 @@ public class GraphServiceClient extends BaseGraphServiceClient implements IGraph
2727
*/
2828
protected GraphServiceClient() {
2929
}
30-
30+
3131
/**
3232
* Send a custom request to Graph
3333
*
34-
* @param url the full URL to make a request with
35-
* @param responseType the response class to deserialize the response into
34+
* @param url
35+
* the full URL to make a request with
36+
* @param responseType
37+
* the response class to deserialize the response into
3638
* @return the instance of this builder
3739
*/
38-
public CustomRequestBuilder customRequest(final String url, final Class responseType) {
39-
return new CustomRequestBuilder(getServiceRoot() + url, (IGraphServiceClient)this, null, responseType);
40+
public <T> CustomRequestBuilder<T> customRequest(final String url, final Class<T> responseType) {
41+
return new CustomRequestBuilder<T>(getServiceRoot() + url, (IGraphServiceClient) this, null, responseType);
4042
}
41-
43+
4244
/**
4345
* Send a custom request to Graph
4446
*
45-
* @param url the full URL to make a request with
47+
* @param url
48+
* the full URL to make a request with
4649
* @return the instance of this builder
4750
*/
48-
public CustomRequestBuilder customRequest(final String url) {
49-
return new CustomRequestBuilder(getServiceRoot() + url, (IGraphServiceClient)this, null, JsonObject.class);
50-
}
51-
52-
public static Builder builder() {
53-
return new Builder();
51+
public CustomRequestBuilder<JsonObject> customRequest(final String url) {
52+
return new CustomRequestBuilder<JsonObject>(getServiceRoot() + url, (IGraphServiceClient) this, null,
53+
JsonObject.class);
5454
}
5555

5656
/**
57-
* The builder for this GraphServiceClient
57+
* Returns a Graph service client using the given configuration.
58+
*
59+
* @param config
60+
* the client configuration
61+
* @return a Graph service client
5862
*/
59-
public static class Builder {
63+
public static IGraphServiceClient fromConfig(final IClientConfig config) {
64+
GraphServiceClient client = new GraphServiceClient();
65+
client.setAuthenticationProvider(config.getAuthenticationProvider());
66+
client.setExecutors(config.getExecutors());
67+
client.setHttpProvider(config.getHttpProvider());
68+
client.setLogger(config.getLogger());
69+
client.setSerializer(config.getSerializer());
70+
client.validate();
71+
return client;
72+
}
73+
74+
public static Builder builder() {
75+
return new Builder();
76+
}
77+
78+
public static final class Builder {
6079

6180
Builder() {
62-
// ensure instantiation only from static factory method
81+
// restrict instantiation
6382
}
6483

6584
/**
66-
* The client under construction
67-
*/
68-
private final GraphServiceClient client = new GraphServiceClient();
69-
70-
/**
71-
* Sets the serializer
85+
* Sets the authentication provider
7286
*
73-
* @param serializer the serializer
74-
* @return the instance of this builder
87+
* @param authenticationProvider
88+
* the authentication provider
89+
* @return a new builder that allows specification of other aspects of the GraphServiceClient
7590
*/
76-
public Builder serializer(final ISerializer serializer) {
77-
client.setSerializer(serializer);
78-
return this;
91+
public Builder2 authenticationProvider(IAuthenticationProvider authenticationProvider) {
92+
checkNotNull(authenticationProvider, "authenticationProvider");
93+
return new Builder2(authenticationProvider);
94+
}
95+
}
96+
97+
/**
98+
* The builder for this GraphServiceClient
99+
*/
100+
public static final class Builder2 {
101+
102+
private final IAuthenticationProvider authenticationProvider;
103+
private ISerializer serializer;
104+
private IHttpProvider httpProvider;
105+
private IExecutors executors;
106+
private ILogger logger;
107+
108+
109+
Builder2(IAuthenticationProvider authenticationProvider) {
110+
this.authenticationProvider = authenticationProvider;
79111
}
80112

81113
/**
82-
* Sets the httpProvider
114+
* Sets the serializer.
83115
*
84-
* @param httpProvider the httpProvider
116+
* @param serializer
117+
* the serializer
85118
* @return the instance of this builder
86119
*/
87-
public Builder httpProvider(final IHttpProvider httpProvider) {
88-
client.setHttpProvider(httpProvider);
120+
public Builder2 serializer(final ISerializer serializer) {
121+
checkNotNull(serializer, "serializer");
122+
this.serializer = serializer;
89123
return this;
90124
}
91125

92126
/**
93-
* Sets the authentication provider
127+
* Sets the httpProvider
94128
*
95-
* @param authenticationProvider the authentication provider
129+
* @param httpProvider
130+
* the httpProvider
96131
* @return the instance of this builder
97132
*/
98-
public Builder authenticationProvider(final IAuthenticationProvider authenticationProvider) {
99-
client.setAuthenticationProvider(authenticationProvider);
133+
public Builder2 httpProvider(final IHttpProvider httpProvider) {
134+
checkNotNull(httpProvider, "httpProvider");
135+
this.httpProvider = httpProvider;
100136
return this;
101137
}
102138

103139
/**
104140
* Sets the executors
105141
*
106-
* @param executors the executors
142+
* @param executors
143+
* the executors
107144
* @return the instance of this builder
108145
*/
109-
public Builder executors(final IExecutors executors) {
110-
client.setExecutors(executors);
146+
public Builder2 executors(final IExecutors executors) {
147+
checkNotNull(executors, "executors");
148+
this.executors = executors;
111149
return this;
112150
}
113151

114152
/**
115153
* Sets the logger
116154
*
117-
* @param logger the logger
155+
* @param logger
156+
* the logger
118157
* @return the instance of this builder
119158
*/
120-
public Builder logger(final ILogger logger) {
121-
client.setLogger(logger);
159+
public Builder2 logger(final ILogger logger) {
160+
checkNotNull(logger, "logger");
161+
this.logger = logger;
122162
return this;
123163
}
124164

125165
/**
126-
* Set this builder based on the client configuration
166+
* Builds and returns the Graph service client.
127167
*
128-
* @param clientConfig the client configuration
129-
* @return the instance of this builder
168+
* @return the Graph service client object
169+
* @throws ClientException
170+
* if there was an exception creating the client
130171
*/
131-
public Builder fromConfig(final IClientConfig clientConfig) {
132-
return this.authenticationProvider(clientConfig.getAuthenticationProvider())
133-
.executors(clientConfig.getExecutors())
134-
.httpProvider(clientConfig.getHttpProvider())
135-
.logger(clientConfig.getLogger())
136-
.serializer(clientConfig.getSerializer());
137-
}
172+
public IGraphServiceClient buildClient() throws ClientException {
173+
DefaultClientConfig config = new DefaultClientConfig() {
138174

139-
/**
140-
* Builds and returns the GraphServiceClient
141-
*
142-
* @return the GraphServiceClient object
143-
* @throws ClientException if there was an exception creating the client
144-
*/
145-
public IGraphServiceClient buildClient() throws ClientException {
146-
client.validate();
147-
return client;
175+
@Override
176+
public IAuthenticationProvider getAuthenticationProvider() {
177+
return authenticationProvider;
178+
}
179+
180+
@Override
181+
public IHttpProvider getHttpProvider() {
182+
if (httpProvider != null) {
183+
return httpProvider;
184+
} else {
185+
return super.getHttpProvider();
186+
}
187+
}
188+
189+
@Override
190+
public IExecutors getExecutors() {
191+
if (executors != null) {
192+
return executors;
193+
} else {
194+
return super.getExecutors();
195+
}
196+
}
197+
198+
@Override
199+
public ILogger getLogger() {
200+
if (logger !=null) {
201+
return logger;
202+
} else {
203+
return super.getLogger();
204+
}
205+
}
206+
207+
@Override
208+
public ISerializer getSerializer() {
209+
if (serializer != null) {
210+
return serializer;
211+
} else {
212+
return super.getSerializer();
213+
}
214+
}
215+
};
216+
return GraphServiceClient.fromConfig(config);
217+
}
218+
}
219+
220+
private static void checkNotNull(Object o, String name) {
221+
if (o==null) {
222+
throw new NullPointerException(name + " cannot be null");
148223
}
149224
}
150225
}

src/main/java/com/microsoft/graph/serializer/DefaultSerializer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
package com.microsoft.graph.serializer;
2424

25+
import com.google.common.annotations.VisibleForTesting;
2526
import com.google.common.base.CaseFormat;
2627
import com.google.gson.Gson;
2728
import com.google.gson.JsonElement;
@@ -252,4 +253,9 @@ private Class<?> getDerivedClass(JsonObject jsonObject, Class<?> parentClass) {
252253
//If there is no defined OData type, return null
253254
return null;
254255
}
256+
257+
@VisibleForTesting
258+
public ILogger getLogger() {
259+
return logger;
260+
}
255261
}

src/test/java/com/microsoft/graph/core/DefaultClientConfigTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public ILogger getLogger() {
4949
return logger;
5050
}
5151

52+
@Override
53+
public IAuthenticationProvider getAuthenticationProvider() {
54+
return new MockAuthenticationProvider();
55+
}
56+
5257
};
5358
config.getExecutors();
5459
config.getAuthenticationProvider();

src/test/java/com/microsoft/graph/functional/TestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void authenticateRequest(final IHttpRequest request) {
5353
};
5454
IClientConfig mClientConfig = DefaultClientConfig.createWithAuthenticationProvider(mAuthenticationProvider);
5555

56-
graphClient = GraphServiceClient.builder().fromConfig(mClientConfig).buildClient();
56+
graphClient = GraphServiceClient.fromConfig(mClientConfig);
5757
}
5858
catch (Exception e)
5959
{

0 commit comments

Comments
 (0)