Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert from Async to Sync #770

Merged
merged 11 commits into from
Nov 10, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public ApiException(@Nonnull final Throwable cause) {
* Gets the HTTP response status code
* @return The response status code from the failed response.
*/
@Nonnull
public int getResponseStatusCode() {
return responseStatusCode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class BaseRequestBuilder {
* @param urlTemplate Url template to use to build the URL for the current request builder
*/
protected BaseRequestBuilder(@Nonnull final RequestAdapter requestAdapter, @Nonnull final String urlTemplate) {
this(requestAdapter, urlTemplate, new HashMap<String, Object>());
this(requestAdapter, urlTemplate, new HashMap<>());
}
/**
* Instantiates a new BaseRequestBuilder and sets the default values.
Expand All @@ -33,7 +33,7 @@ protected BaseRequestBuilder(@Nonnull final RequestAdapter requestAdapter, @Nonn
protected BaseRequestBuilder(@Nonnull final RequestAdapter requestAdapter, @Nonnull final String urlTemplate, @Nonnull final HashMap<String, Object> pathParameters) {
this.requestAdapter = Objects.requireNonNull(requestAdapter);
this.urlTemplate = Objects.requireNonNull(urlTemplate);
this.pathParameters = new HashMap<String, Object>(Objects.requireNonNull(pathParameters));
this.pathParameters = new HashMap<>(Objects.requireNonNull(pathParameters));
}
/**
* Instantiates a new BaseRequestBuilder and sets the default values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
import jakarta.annotation.Nullable;

/**
* A map that is case insensitive on the keys
* A map that is case-insensitive on the keys
*/
public class CaseInsensitiveMap implements Map<String, Set<String>>{
public CaseInsensitiveMap() {
// default constructor
}
private final HashMap<String, HashSet<String>> internalMap = new HashMap<>();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.InputStream;

import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;

/**
* Compatibility methods for android
*/
Expand All @@ -30,4 +32,12 @@ public static byte[] readAllBytes(@Nonnull final InputStream inputStream) throws
return outputStream.toByteArray();
}
}
/** INTERNAL METHOD, DO NOT USE DIRECTLY
* Checks if the string is null or empty or blank
* @param str the string to check
* @return true if the string is null or empty or blank
*/
public static boolean isBlank(@Nullable final String str) {
return str == null || str.trim().isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.*;
ramsessanchez marked this conversation as resolved.
Show resolved Hide resolved
import java.util.function.Consumer;

import jakarta.annotation.Nonnull;
Expand All @@ -24,7 +20,10 @@
public class MultipartBody implements Parsable {
@Nonnull
private final String boundary = UUID.randomUUID().toString().replace("-", "");
/** @return the boundary string for the multipart body. */
/**
* Gets the boundary string for the multipart body.
* @return the boundary string for the multipart body.
*/
@Nonnull
public String getBoundary() {
return boundary;
Expand All @@ -43,14 +42,14 @@ public String getBoundary() {
*/
public <T> void addOrReplacePart(@Nonnull final String name, @Nonnull final String contentType, @Nonnull final T value) {
Objects.requireNonNull(value);
if (Strings.isNullOrEmpty(contentType) || contentType.isBlank())
if (Compatibility.isBlank(contentType))
throw new IllegalArgumentException("contentType cannot be blank or empty");
if (Strings.isNullOrEmpty(name) || name.isBlank())
if (Compatibility.isBlank(name))
throw new IllegalArgumentException("name cannot be blank or empty");

final String normalizedName = normalizePartName(name);
originalNames.put(normalizedName, name);
parts.put(normalizedName, Map.entry(contentType, value));
parts.put(normalizedName, new AbstractMap.SimpleEntry<>(contentType, value));
}
private final Map<String, Map.Entry<String, Object>> parts = new HashMap<>();
private final Map<String, String> originalNames = new HashMap<>();
Expand All @@ -66,7 +65,7 @@ private String normalizePartName(@Nonnull final String original)
@Nullable
public Object getPartValue(@Nonnull final String partName)
{
if (Strings.isNullOrEmpty(partName) || partName.isBlank())
if (Compatibility.isBlank(partName))
throw new IllegalArgumentException("partName cannot be blank or empty");
final String normalizedName = normalizePartName(partName);
final Map.Entry<String, Object> candidate = parts.get(normalizedName);
Expand All @@ -81,7 +80,7 @@ public Object getPartValue(@Nonnull final String partName)
*/
public boolean removePart(@Nonnull final String partName)
{
if (Strings.isNullOrEmpty(partName) || partName.isBlank())
if (Compatibility.isBlank(partName))
throw new IllegalArgumentException("partName cannot be blank or empty");
final String normalizedName = normalizePartName(partName);
final Object candidate = parts.remove(normalizedName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
Expand All @@ -18,11 +17,10 @@ public class NativeResponseHandler implements ResponseHandler {
private HashMap<String, ParsableFactory<? extends Parsable>> errorMappings;
@Override
@Nonnull
public <NativeResponseType, ModelType> CompletableFuture<ModelType> handleResponseAsync(@Nonnull NativeResponseType response, @Nullable HashMap<String, ParsableFactory<? extends Parsable>> errorMappings) {
public <NativeResponseType, ModelType> void handleResponseAsync(@Nonnull NativeResponseType response, @Nullable HashMap<String, ParsableFactory<? extends Parsable>> errorMappings) {
baywet marked this conversation as resolved.
Show resolved Hide resolved
this.value = response;
if(errorMappings != null)
this.errorMappings = new HashMap<>(errorMappings);
return CompletableFuture.completedFuture(null);
}
/**
* Set the error mappings for the response to use when deserializing failed responses bodies.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public static PeriodAndDuration ofPeriodAndDuration(@Nonnull PeriodAndDuration p
return new PeriodAndDuration(periodAndDuration.getPeriod(), periodAndDuration.getDuration());
}
/**
* Parses a string to produce a {@code PeriodAndDuration}.
* @param stringValue the {@code String} parse from.
* @return parsed instance of {@code PeriodAndDuration}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.microsoft.kiota;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.concurrent.CompletableFuture;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -35,7 +38,7 @@ public interface RequestAdapter {
*/
@Nullable
@SuppressWarnings("LambdaLast")
<ModelType extends Parsable> CompletableFuture<ModelType> sendAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final ParsableFactory<ModelType> factory, @Nullable final HashMap<String, ParsableFactory<? extends Parsable>> errorMappings);
<ModelType extends Parsable> ModelType sendAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final ParsableFactory<ModelType> factory, @Nullable final HashMap<String, ParsableFactory<? extends Parsable>> errorMappings) throws IOException, URISyntaxException;
ramsessanchez marked this conversation as resolved.
Show resolved Hide resolved
/**
* Executes the HTTP request specified by the given RequestInformation and returns the deserialized response model collection.
* @param requestInfo the request info to execute.
Expand All @@ -46,7 +49,7 @@ public interface RequestAdapter {
*/
@Nullable
@SuppressWarnings("LambdaLast")
<ModelType extends Parsable> CompletableFuture<List<ModelType>> sendCollectionAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final ParsableFactory<ModelType> factory, @Nullable final HashMap<String, ParsableFactory<? extends Parsable>> errorMappings);
<ModelType extends Parsable> List<ModelType> sendCollectionAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final ParsableFactory<ModelType> factory, @Nullable final HashMap<String, ParsableFactory<? extends Parsable>> errorMappings);
/**
* Executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive response model.
* @param requestInfo the request info to execute.
Expand All @@ -56,7 +59,7 @@ public interface RequestAdapter {
* @return a {@link CompletableFuture} with the deserialized primitive response model.
*/
@Nullable
<ModelType> CompletableFuture<ModelType> sendPrimitiveAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final HashMap<String, ParsableFactory<? extends Parsable>> errorMappings);
<ModelType> ModelType sendPrimitiveAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final HashMap<String, ParsableFactory<? extends Parsable>> errorMappings);
/**
* Executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive collection response model.
* @param requestInfo the request info to execute.
Expand All @@ -66,7 +69,7 @@ public interface RequestAdapter {
* @return a {@link CompletableFuture} with the deserialized primitive collection response model.
*/
@Nullable
<ModelType> CompletableFuture<List<ModelType>> sendPrimitiveCollectionAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final HashMap<String, ParsableFactory<? extends Parsable>> errorMappings);
<ModelType> List<ModelType> sendPrimitiveCollectionAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final HashMap<String, ParsableFactory<? extends Parsable>> errorMappings);

/**
Executes the HTTP request specified by the given RequestInformation and returns the deserialized enum value.
Expand All @@ -77,7 +80,7 @@ public interface RequestAdapter {
* @return a {@link CompletableFuture} with the deserialized primitive response model.
*/
@Nullable
<ModelType extends Enum<ModelType>> CompletableFuture<ModelType> sendEnumAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final HashMap<String, ParsableFactory<? extends Parsable>> errorMappings);
<ModelType extends Enum<ModelType>> ModelType sendEnumAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final HashMap<String, ParsableFactory<? extends Parsable>> errorMappings);

/**
Executes the HTTP request specified by the given RequestInformation and returns the deserialized enum collection value.
Expand All @@ -88,7 +91,7 @@ public interface RequestAdapter {
* @return a {@link CompletableFuture} with the deserialized primitive response model.
*/
@Nullable
<ModelType extends Enum<ModelType>> CompletableFuture<List<ModelType>> sendEnumCollectionAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final HashMap<String, ParsableFactory<? extends Parsable>> errorMappings);
<ModelType extends Enum<ModelType>> List<ModelType> sendEnumCollectionAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> targetClass, @Nullable final HashMap<String, ParsableFactory<? extends Parsable>> errorMappings);
/**
* Sets The base url for every request.
* @param baseUrl The base url for every request.
Expand All @@ -107,5 +110,5 @@ public interface RequestAdapter {
* @return the native HTTP request.
*/
@Nonnull
<T> CompletableFuture<T> convertToNativeRequestAsync(@Nonnull final RequestInformation requestInfo);
<T> T convertToNativeRequestAsync(@Nonnull final RequestInformation requestInfo) throws URISyntaxException, MalformedURLException;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.microsoft.kiota;

import java.util.HashMap;
import java.util.concurrent.CompletableFuture;

import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
Expand All @@ -13,12 +12,12 @@
public interface ResponseHandler {
/**
* Callback method that is invoked when a response is received.
* @param response The native response object.
* @param errorMappings the error mappings for the response to use when deserializing failed responses bodies. Where an error code like 401 applies specifically to that status code, a class code like 4XX applies to all status codes within the range if an the specific error code is not present.
*
* @param response The native response object.
* @param errorMappings the error mappings for the response to use when deserializing failed responses bodies. Where an error code like 401 applies specifically to that status code, a class code like 4XX applies to all status codes within the range if an the specific error code is not present.
* @param <NativeResponseType> The type of the native response object.
* @param <ModelType> The type of the response model object.
* @return A CompletableFuture that represents the asynchronous operation and contains the deserialized response.
* @param <ModelType> The type of the response model object.
*/
@Nonnull
<NativeResponseType, ModelType> CompletableFuture<ModelType> handleResponseAsync(@Nonnull final NativeResponseType response, @Nullable final HashMap<String, ParsableFactory<? extends Parsable>> errorMappings);
<NativeResponseType, ModelType> void handleResponseAsync(@Nonnull final NativeResponseType response, @Nullable final HashMap<String, ParsableFactory<? extends Parsable>> errorMappings);
ramsessanchez marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface AccessTokenProvider {
* @return A CompletableFuture that holds the access token.
*/
@Nonnull
CompletableFuture<String> getAuthorizationToken(@Nonnull final URI uri, @Nullable final Map<String, Object> additionalAuthenticationContext);
String getAuthorizationToken(@Nonnull final URI uri, @Nullable final Map<String, Object> additionalAuthenticationContext);
/**
* Returns the allowed hosts validator.
* @return The allowed hosts validator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.microsoft.kiota.RequestInformation;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
Expand All @@ -13,8 +12,7 @@ public class AnonymousAuthenticationProvider implements AuthenticationProvider {
/** Default constructor for the anonymous authentication provider. */
public AnonymousAuthenticationProvider() {}
/** {@inheritDoc} */
@Nonnull
public CompletableFuture<Void> authenticateRequest(@Nonnull final RequestInformation request, @Nullable final Map<String, Object> additionalAuthenticationContext) {
return CompletableFuture.completedFuture(null);
}
public void authenticateRequest(@Nonnull final RequestInformation request, @Nullable final Map<String, Object> additionalAuthenticationContext) {
//This authentication provider does not perform any authentication.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ public ApiKeyAuthenticationProvider(@Nonnull final String apiKey, @Nonnull final
private static final String parentSpanKey = "parent-span";
/** {@inheritDoc} */
@Override
@Nonnull
public CompletableFuture<Void> authenticateRequest(@Nonnull final RequestInformation request, @Nullable final Map<String, Object> additionalAuthenticationContext) {
public void authenticateRequest(@Nonnull final RequestInformation request, @Nullable final Map<String, Object> additionalAuthenticationContext) throws URISyntaxException {
Objects.requireNonNull(request);
final CompletableFuture<Void> resultFuture = new CompletableFuture<>();
Span span;
if(additionalAuthenticationContext != null && additionalAuthenticationContext.containsKey(parentSpanKey) && additionalAuthenticationContext.get(parentSpanKey) instanceof Span) {
final Span parentSpan = (Span) additionalAuthenticationContext.get(parentSpanKey);
Expand All @@ -64,14 +62,11 @@ public CompletableFuture<Void> authenticateRequest(@Nonnull final RequestInforma
final URI uri = request.getUri();
if(uri == null || !validator.isUrlHostValid(uri)) {
span.setAttribute("com.microsoft.kiota.authentication.is_url_valid", false);
return CompletableFuture.completedFuture(null);
return;
}
if(!uri.getScheme().equalsIgnoreCase("https")) {
span.setAttribute("com.microsoft.kiota.authentication.is_url_valid", false);
final Exception result = new IllegalArgumentException("Only https is supported");
span.recordException(result);
baywet marked this conversation as resolved.
Show resolved Hide resolved
resultFuture.completeExceptionally(result);
return resultFuture;
throw new IllegalArgumentException("Only https is supported");
}
span.setAttribute("com.microsoft.kiota.authentication.is_url_valid", true);

Expand All @@ -83,17 +78,13 @@ public CompletableFuture<Void> authenticateRequest(@Nonnull final RequestInforma
request.setUri(new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), uri.getQuery() == null ? paramName + "=" + apiKey : uri.getQuery() + "&" + paramName + "=" + apiKey, uri.getFragment()));
break;
default:
resultFuture.completeExceptionally(new IllegalArgumentException("Unsupported key location"));
}
if (!resultFuture.isDone()) {
resultFuture.complete(null);
throw new IllegalArgumentException("Unsupported key location");
}
return resultFuture;
} catch (URISyntaxException e) {
} catch (URISyntaxException | IllegalArgumentException e ) {
span.recordException(e);
resultFuture.completeExceptionally(e);
return resultFuture;
} finally {
throw e;
}
finally {
span.end();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.microsoft.kiota.RequestInformation;

import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

Expand All @@ -14,8 +15,6 @@ public interface AuthenticationProvider {
* Authenticates the application request.
* @param request the request to authenticate.
* @param additionalAuthenticationContext Additional authentication context to pass to the authentication library.
* @return a CompletableFuture to await for the authentication to be completed.
*/
@Nonnull
CompletableFuture<Void> authenticateRequest(@Nonnull final RequestInformation request, @Nullable final Map<String, Object> additionalAuthenticationContext);
void authenticateRequest(@Nonnull final RequestInformation request, @Nullable final Map<String, Object> additionalAuthenticationContext) throws URISyntaxException;
}
Loading
Loading