Skip to content

Commit 1ebf574

Browse files
committed
rework GraphServiceClient
1 parent 2edd553 commit 1ebf574

File tree

6 files changed

+349
-49
lines changed

6 files changed

+349
-49
lines changed

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/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: 110 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,124 +27,186 @@ 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);
51+
public CustomRequestBuilder<JsonObject> customRequest(final String url) {
52+
return new CustomRequestBuilder<JsonObject>(getServiceRoot() + url, (IGraphServiceClient) this, null,
53+
JsonObject.class);
5054
}
51-
55+
56+
/**
57+
* Returns a Graph service client using the given configuration.
58+
*
59+
* @param config
60+
* the client configuration
61+
* @return a Graph service client
62+
*/
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+
5274
public static Builder builder() {
5375
return new Builder();
5476
}
5577

5678
/**
5779
* The builder for this GraphServiceClient
5880
*/
59-
public static class Builder {
60-
81+
public static final class Builder {
82+
83+
private ISerializer serializer;
84+
private IHttpProvider httpProvider;
85+
private IAuthenticationProvider authenticationProvider;
86+
private IExecutors executors;
87+
private ILogger logger;
88+
6189
Builder() {
62-
// ensure instantiation only from static factory method
90+
// ensure instantiation only from static factory method
6391
}
6492

6593
/**
66-
* The client under construction
67-
*/
68-
private final GraphServiceClient client = new GraphServiceClient();
69-
70-
/**
71-
* Sets the serializer
94+
* Sets the serializer.
7295
*
73-
* @param serializer the serializer
96+
* @param serializer
97+
* the serializer
7498
* @return the instance of this builder
7599
*/
76100
public Builder serializer(final ISerializer serializer) {
77-
client.setSerializer(serializer);
101+
this.serializer = serializer;
78102
return this;
79103
}
80104

81105
/**
82106
* Sets the httpProvider
83107
*
84-
* @param httpProvider the httpProvider
108+
* @param httpProvider
109+
* the httpProvider
85110
* @return the instance of this builder
86111
*/
87112
public Builder httpProvider(final IHttpProvider httpProvider) {
88-
client.setHttpProvider(httpProvider);
113+
this.httpProvider = httpProvider;
89114
return this;
90115
}
91116

92117
/**
93118
* Sets the authentication provider
94119
*
95-
* @param authenticationProvider the authentication provider
120+
* @param authenticationProvider
121+
* the authentication provider
96122
* @return the instance of this builder
97123
*/
98124
public Builder authenticationProvider(final IAuthenticationProvider authenticationProvider) {
99-
client.setAuthenticationProvider(authenticationProvider);
125+
this.authenticationProvider = authenticationProvider;
100126
return this;
101127
}
102128

103129
/**
104130
* Sets the executors
105131
*
106-
* @param executors the executors
132+
* @param executors
133+
* the executors
107134
* @return the instance of this builder
108135
*/
109136
public Builder executors(final IExecutors executors) {
110-
client.setExecutors(executors);
137+
this.executors = executors;
111138
return this;
112139
}
113140

114141
/**
115142
* Sets the logger
116143
*
117-
* @param logger the logger
144+
* @param logger
145+
* the logger
118146
* @return the instance of this builder
119147
*/
120148
public Builder logger(final ILogger logger) {
121-
client.setLogger(logger);
149+
this.logger = logger;
122150
return this;
123151
}
124152

125153
/**
126-
* Set this builder based on the client configuration
154+
* Builds and returns the Graph service client.
127155
*
128-
* @param clientConfig the client configuration
129-
* @return the instance of this builder
156+
* @return the Graph service client object
157+
* @throws ClientException
158+
* if there was an exception creating the client
130159
*/
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-
}
160+
public IGraphServiceClient buildClient() throws ClientException {
161+
DefaultClientConfig config = new DefaultClientConfig() {
138162

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;
163+
@Override
164+
public IAuthenticationProvider getAuthenticationProvider() {
165+
if (authenticationProvider != null) {
166+
return authenticationProvider;
167+
} else {
168+
return super.getAuthenticationProvider();
169+
}
170+
}
171+
172+
@Override
173+
public IHttpProvider getHttpProvider() {
174+
if (httpProvider != null) {
175+
return httpProvider;
176+
} else {
177+
return super.getHttpProvider();
178+
}
179+
}
180+
181+
@Override
182+
public IExecutors getExecutors() {
183+
if (executors != null) {
184+
return executors;
185+
} else {
186+
return super.getExecutors();
187+
}
188+
}
189+
190+
@Override
191+
public ILogger getLogger() {
192+
if (logger !=null) {
193+
return logger;
194+
} else {
195+
return super.getLogger();
196+
}
197+
}
198+
199+
@Override
200+
public ISerializer getSerializer() {
201+
if (serializer != null) {
202+
return serializer;
203+
} else {
204+
return super.getSerializer();
205+
}
206+
}
207+
};
208+
return GraphServiceClient.fromConfig(config);
148209
}
149210
}
211+
150212
}

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/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)