-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrew Omondi
committed
Jul 23, 2024
1 parent
58d43f9
commit d4db102
Showing
7 changed files
with
185 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
components/bundle/src/main/java/com/microsoft/kiota/bundle/DefaultRequestAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.microsoft.kiota.bundle; | ||
|
||
import com.microsoft.kiota.ApiClientBuilder; | ||
import com.microsoft.kiota.authentication.AuthenticationProvider; | ||
import com.microsoft.kiota.http.ObservabilityOptions; | ||
import com.microsoft.kiota.http.OkHttpRequestAdapter; | ||
import com.microsoft.kiota.serialization.*; | ||
|
||
import jakarta.annotation.Nonnull; | ||
import jakarta.annotation.Nullable; | ||
|
||
import okhttp3.Call; | ||
|
||
/** RequestAdapter implementation for Kiota Bundle */ | ||
public class DefaultRequestAdapter extends OkHttpRequestAdapter { | ||
|
||
/** | ||
* Instantiates a DefaultRequestAdapter with the provided authentication provider. | ||
* @param authenticationProvider the authentication provider to use for authenticating requests. | ||
*/ | ||
public DefaultRequestAdapter(@Nonnull final AuthenticationProvider authenticationProvider) { | ||
this(authenticationProvider, null); | ||
} | ||
|
||
/** | ||
* Instantiates a new DefaultRequestAdapter with the provided authentication provider, and the parse node factory. | ||
* @param authenticationProvider the authentication provider to use for authenticating requests. | ||
* @param parseNodeFactory the parse node factory to use for parsing responses. | ||
*/ | ||
@SuppressWarnings("LambdaLast") | ||
public DefaultRequestAdapter( | ||
@Nonnull final AuthenticationProvider authenticationProvider, | ||
@Nullable final ParseNodeFactory parseNodeFactory) { | ||
this(authenticationProvider, parseNodeFactory, null); | ||
} | ||
|
||
/** | ||
* Instantiates a new DefaultRequestAdapter with the provided authentication provider, parse node factory, and the serialization writer factory. | ||
* @param authenticationProvider the authentication provider to use for authenticating requests. | ||
* @param parseNodeFactory the parse node factory to use for parsing responses. | ||
* @param serializationWriterFactory the serialization writer factory to use for serializing requests. | ||
*/ | ||
@SuppressWarnings("LambdaLast") | ||
public DefaultRequestAdapter( | ||
@Nonnull final AuthenticationProvider authenticationProvider, | ||
@Nullable final ParseNodeFactory parseNodeFactory, | ||
@Nullable final SerializationWriterFactory serializationWriterFactory) { | ||
this(authenticationProvider, parseNodeFactory, serializationWriterFactory, null); | ||
} | ||
|
||
/** | ||
* Instantiates a new DefaultRequestAdapter with the provided authentication provider, parse node factory, serialization writer factory, and the http client. | ||
* @param authenticationProvider the authentication provider to use for authenticating requests. | ||
* @param parseNodeFactory the parse node factory to use for parsing responses. | ||
* @param serializationWriterFactory the serialization writer factory to use for serializing requests. | ||
* @param client the http client to use for sending requests. | ||
*/ | ||
@SuppressWarnings("LambdaLast") | ||
public DefaultRequestAdapter( | ||
@Nonnull final AuthenticationProvider authenticationProvider, | ||
@Nullable final ParseNodeFactory parseNodeFactory, | ||
@Nullable final SerializationWriterFactory serializationWriterFactory, | ||
@Nullable final Call.Factory client) { | ||
this(authenticationProvider, parseNodeFactory, serializationWriterFactory, client, null); | ||
} | ||
|
||
/** | ||
* Instantiates a new DefaultRequestAdapter with the provided authentication provider, parse node factory, serialization writer factory, http client and observability options. | ||
* @param authenticationProvider the authentication provider to use for authenticating requests. | ||
* @param parseNodeFactory the parse node factory to use for parsing responses. | ||
* @param serializationWriterFactory the serialization writer factory to use for serializing requests. | ||
* @param client the http client to use for sending requests. | ||
* @param observabilityOptions the observability options to use for sending requests. | ||
*/ | ||
@SuppressWarnings("LambdaLast") | ||
public DefaultRequestAdapter( | ||
@Nonnull final AuthenticationProvider authenticationProvider, | ||
@Nullable final ParseNodeFactory parseNodeFactory, | ||
@Nullable final SerializationWriterFactory serializationWriterFactory, | ||
@Nullable final Call.Factory client, | ||
@Nullable final ObservabilityOptions observabilityOptions) { | ||
super( | ||
authenticationProvider, | ||
parseNodeFactory, | ||
serializationWriterFactory, | ||
client, | ||
observabilityOptions); | ||
setupDefaults(); | ||
} | ||
|
||
private void setupDefaults() { | ||
ApiClientBuilder.registerDefaultSerializer(JsonSerializationWriterFactory::new); | ||
ApiClientBuilder.registerDefaultSerializer(TextSerializationWriterFactory::new); | ||
ApiClientBuilder.registerDefaultSerializer(FormSerializationWriterFactory::new); | ||
ApiClientBuilder.registerDefaultSerializer(MultipartSerializationWriterFactory::new); | ||
ApiClientBuilder.registerDefaultDeserializer(JsonParseNodeFactory::new); | ||
ApiClientBuilder.registerDefaultDeserializer(FormParseNodeFactory::new); | ||
ApiClientBuilder.registerDefaultDeserializer(TextParseNodeFactory::new); | ||
} | ||
} |
57 changes: 0 additions & 57 deletions
57
components/bundle/src/main/java/com/microsoft/kiota/bundle/KiotaRequestAdapter.java
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
components/bundle/src/test/java/com/microsoft/kiota/bundle/BundleTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.microsoft.kiota.bundle; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import com.microsoft.kiota.authentication.AuthenticationProvider; | ||
import com.microsoft.kiota.serialization.ParseNodeFactoryRegistry; | ||
import com.microsoft.kiota.serialization.SerializationWriterFactoryRegistry; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class BundleTests { | ||
@Test | ||
void throwsErrorNullAuthenticationProvider() throws Exception { | ||
var exception = | ||
assertThrows(NullPointerException.class, () -> new DefaultRequestAdapter(null)); | ||
assertEquals("parameter authenticationProvider cannot be null", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
void serializersAreRegisteredAsExpected() throws Exception { | ||
final var authenticationProviderMock = mock(AuthenticationProvider.class); | ||
var defaultRequestAdapter = new DefaultRequestAdapter(authenticationProviderMock); | ||
assertEquals("", defaultRequestAdapter.getBaseUrl()); | ||
|
||
// validate | ||
var serializerCount = | ||
SerializationWriterFactoryRegistry.defaultInstance.contentTypeAssociatedFactories | ||
.size(); | ||
var deserializerCount = | ||
ParseNodeFactoryRegistry.defaultInstance.contentTypeAssociatedFactories.size(); | ||
|
||
assertEquals(4, serializerCount); // four serializers present | ||
assertEquals(3, deserializerCount); // three deserializers present | ||
|
||
var serializerKeys = | ||
SerializationWriterFactoryRegistry.defaultInstance.contentTypeAssociatedFactories | ||
.keySet(); | ||
var deserializerKeys = | ||
ParseNodeFactoryRegistry.defaultInstance.contentTypeAssociatedFactories.keySet(); | ||
|
||
assertTrue(serializerKeys.contains("application/json")); | ||
assertTrue( | ||
deserializerKeys.contains( | ||
"application/json")); // Serializer and deserializer present for | ||
// application/json | ||
|
||
assertTrue(serializerKeys.contains("text/plain")); | ||
assertTrue( | ||
deserializerKeys.contains( | ||
"text/plain")); // Serializer and deserializer present for text/plain | ||
|
||
assertTrue(serializerKeys.contains("application/x-www-form-urlencoded")); | ||
assertTrue( | ||
deserializerKeys.contains( | ||
"application/x-www-form-urlencoded")); // Serializer and deserializer | ||
// present for | ||
// application/x-www-form-urlencoded | ||
|
||
assertTrue( | ||
serializerKeys.contains( | ||
"multipart/form-data")); // Serializer present for multipart/form-data | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters