Skip to content

Commit

Permalink
Add NativeResponseHandler and Wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Feb 12, 2024
1 parent 5438171 commit 333288a
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 17 deletions.
9 changes: 7 additions & 2 deletions lib/kiota_abstractions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@
library kiota_abstractions;

import 'dart:async';
import 'dart:collection';
import 'dart:math';
import 'dart:typed_data';

import 'package:kiota_abstractions/src/case_insensitive_map.dart';
import 'package:std_uritemplate/std_uritemplate.dart';

part 'src/api_client_builder.dart';
part 'src/base_request_builder.dart';
part 'src/error_mappings.dart';
part 'src/extensions/map_extensions.dart';
part 'src/extensions/request_information_extensions.dart';
part 'src/http_headers.dart';
part 'src/http_method.dart';
part 'src/multipart_body.dart';
part 'src/native_response_handler.dart';
part 'src/native_response_wrapper.dart';
part 'src/path_parameters.dart';
part 'src/query_parameters.dart';
part 'src/request_adapter.dart';
part 'src/request_configuration.dart';
part 'src/http_headers.dart';
part 'src/request_information.dart';
part 'src/request_option.dart';
part 'src/response_handler.dart';
Expand Down
7 changes: 7 additions & 0 deletions lib/src/error_mappings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
part of '../kiota_abstractions.dart';

/// The [ErrorMappings] are used for the response 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.
typedef ErrorMappings = Map<String, ParsableFactory<Parsable>>;
25 changes: 25 additions & 0 deletions lib/src/native_response_handler.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
part of '../kiota_abstractions.dart';

/// Default response handler to access the native response object.
class NativeResponseHandler implements ResponseHandler {
/// The native response object as returned by the core service.
Object? value;

/// The error mappings for the response to use when deserializing failed
/// response bodies.
///
/// See [ErrorMappings] for more information.
ErrorMappings? errorMappings;

@override
Future<ModelType?>
handleResponse<NativeResponseType, ModelType extends Parsable>(
NativeResponseType response, [
ErrorMappings? errorMapping,
]) {
errorMappings = errorMapping;
value = response;

return Future<ModelType?>.value();
}
}
59 changes: 59 additions & 0 deletions lib/src/native_response_wrapper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
part of '../kiota_abstractions.dart';

/// Type definition for request callback.
typedef RequestCallback<T> = Future<T> Function(
QueryParameters Function()?,
HttpHeaders Function()?,
Iterable<RequestOption>?,
ResponseHandler,
);

/// Type definition for request callback with body.
typedef RequestWithBodyCallback<TBody, TResponse> = Future<TResponse> Function(
TBody,
QueryParameters Function()?,
HttpHeaders Function()?,
Iterable<RequestOption>?,
ResponseHandler,
);

/// This class can be used to wrap a request using the fluent API and get the
/// native response object in return.
class NativeResponseWrapper {
NativeResponseWrapper._();

/// Makes a request with the given parameters and returns the native response
/// object.
static Future<NativeResponseType?>
callAndGetNativeType<ModelType, NativeResponseType>(
RequestCallback<ModelType> callback, [
QueryParameters Function()? query,
HttpHeaders Function()? headers,
Iterable<RequestOption>? options,
]) async {
final handler = NativeResponseHandler();

// ignore result
await callback(query, headers, options, handler);

return handler.value as NativeResponseType?;
}

/// Makes a request with the given parameters and request body and then
/// returns the native response object.
static Future<NativeResponseType?>
callAndGetNativeTypeWithBody<TBody, TResponse, NativeResponseType>(
RequestWithBodyCallback<TBody, TResponse> callback,
TBody body, [
QueryParameters Function()? query,
HttpHeaders Function()? headers,
Iterable<RequestOption>? options,
]) async {
final handler = NativeResponseHandler();

// ignore result
await callback(body, query, headers, options, handler);

return handler.value as NativeResponseType?;
}
}
4 changes: 4 additions & 0 deletions lib/src/path_parameters.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
part of '../kiota_abstractions.dart';

/// Type definition for path parameters to be substituted in the URL template.
typedef PathParameters = Map<String, dynamic>;
4 changes: 4 additions & 0 deletions lib/src/query_parameters.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
part of '../kiota_abstractions.dart';

/// Type definition for query parameters.
typedef QueryParameters = Map<String, dynamic>;
14 changes: 4 additions & 10 deletions lib/src/request_information.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ class RequestInformation {
this.httpMethod,
this.urlTemplate,
Map<String, dynamic>? pathParameters,
}) : queryParameters = LinkedHashMap(
equals: (a, b) => a.toUpperCase() == b.toUpperCase(),
hashCode: (a) => a.toUpperCase().hashCode,
),
pathParameters = LinkedHashMap(
equals: (a, b) => a.toUpperCase() == b.toUpperCase(),
hashCode: (a) => a.toUpperCase().hashCode,
) {
}) : queryParameters = CaseInsensitiveMap(),
pathParameters = CaseInsensitiveMap() {
if (pathParameters != null) {
this.pathParameters.addAll(pathParameters);
}
Expand All @@ -26,13 +20,13 @@ class RequestInformation {
String? urlTemplate;

/// The path parameters to use for the URL template when generating the URI.
Map<String, dynamic> pathParameters;
PathParameters pathParameters;

/// The HTTP [HttpMethod] of the request.
HttpMethod? httpMethod;

/// The query parameters to use for the URL when generating the URI.
Map<String, dynamic> queryParameters;
QueryParameters queryParameters;

final HttpHeaders _headers = HttpHeaders();

Expand Down
7 changes: 2 additions & 5 deletions lib/src/response_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ part of '../kiota_abstractions.dart';
/// Defines the contract for a response handler.
abstract class ResponseHandler {
/// Callback method that is invoked when a response is received.
///
/// The [response] is the native response object.
/// The [errorMapping]s are used for the response 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.
Future<ModelType?>
handleResponse<NativeResponseType, ModelType extends Parsable>(
NativeResponseType response, [
Map<String, ParsableFactory<Parsable>>? errorMapping,
ErrorMappings? errorMapping,
]);
}

0 comments on commit 333288a

Please sign in to comment.