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

Implement HTTP request adapter #20

Merged
merged 25 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a0d90e3
Started implementing http client
ricardoboss Mar 28, 2024
fddcd42
Implement http client request adapter
ricardoboss Mar 31, 2024
b9a1ac7
Added client factory
ricardoboss Mar 31, 2024
6fc4abb
Added run config
ricardoboss Apr 1, 2024
963e530
Make content nullable for requests without body
ricardoboss Apr 4, 2024
9fc8c0a
Use explicit value instead of case name for consistency
ricardoboss Apr 4, 2024
407aeaf
Use less else ifs for better readability
ricardoboss Apr 4, 2024
7c90e6e
Formatting
ricardoboss Apr 4, 2024
d576c87
Include body in request, if any
ricardoboss Apr 4, 2024
c675d3c
Add HTTP headers from info to request
ricardoboss Apr 4, 2024
24bf896
Set baseUrl in path parameters instead of query parameters
ricardoboss Apr 4, 2024
7d04366
Update test with empty request info having no content
ricardoboss Apr 4, 2024
fb3abd8
Test and fix sendPrimitive
ricardoboss Apr 4, 2024
dbf6bd6
Don't throw for 3xx response codes
ricardoboss Apr 4, 2024
76ef877
Make _getMessageFromInfo sync
ricardoboss Apr 4, 2024
c2fcb37
Added missing signature for enableBackingStore
ricardoboss Apr 4, 2024
1e818ee
Implemented retry handler
ricardoboss Apr 4, 2024
644e8c3
Added redirect handler (WIP)
ricardoboss Apr 4, 2024
c2ccfaa
Use official client wrappers and only provide a custom user agent client
ricardoboss Apr 7, 2024
a8f674f
Ignore generated mocks
ricardoboss Apr 7, 2024
5f80e77
Update README to include instructions how to run build runner
ricardoboss Apr 7, 2024
80feac9
Determine package version at build-time and use it as the user agent …
ricardoboss Apr 7, 2024
0834f69
First run build_runner, then analyze to prevent issues with generated…
ricardoboss Apr 7, 2024
01b4782
Ignore prefer_final_fields for fields regarding backing stores (rever…
ricardoboss Apr 7, 2024
6a2c487
Remove TODO as its done
ricardoboss Apr 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ jobs:
run: dart format --output=none --set-exit-if-changed .
working-directory: packages/${{ matrix.package }}

- name: Run build_runner
if: contains(fromJson('[ "kiota_abstractions", "kiota_http" ]'), matrix.package)
run: dart run build_runner build --delete-conflicting-outputs
working-directory: packages/${{ matrix.package }}

- name: Analyze project source
continue-on-error: true
run: dart analyze --fatal-infos
working-directory: packages/${{ matrix.package }}

- name: Run build_runner
if: contains(fromJson('[ "kiota_abstractions" ]'), matrix.package)
run: dart run build_runner build --delete-conflicting-outputs
working-directory: packages/${{ matrix.package }}

- name: Run tests
run: dart test
working-directory: packages/${{ matrix.package }}
7 changes: 7 additions & 0 deletions .run/All Tests.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="All Tests" type="CompoundRunConfigurationType">
<toRun name="Test kiota_abstractions" type="DartTestRunConfigurationType" />
<toRun name="Test kiota_http" type="DartTestRunConfigurationType" />
<method v="2" />
</configuration>
</component>
7 changes: 7 additions & 0 deletions .run/Test kiota_abstractions.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test kiota_abstractions" type="DartTestRunConfigurationType" factoryName="Dart Test">
<option name="filePath" value="$PROJECT_DIR$/packages/kiota_abstractions/test" />
<option name="scope" value="FOLDER" />
<method v="2" />
</configuration>
</component>
7 changes: 7 additions & 0 deletions .run/Test kiota_http.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test kiota_http" type="DartTestRunConfigurationType" factoryName="Dart Test">
<option name="filePath" value="$PROJECT_DIR$/packages/kiota_http/test" />
<option name="scope" value="FOLDER" />
<method v="2" />
</configuration>
</component>
17 changes: 17 additions & 0 deletions .run/watch kiota_abstractions.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="watch kiota_abstractions" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="dart run build_runner watch" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$/packages/kiota_abstractions/" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="true" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2" />
</configuration>
</component>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="build_runner watch" type="ShConfigurationType">
<configuration default="false" name="watch kiota_http" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="dart run build_runner watch" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$/packages/kiota_http/" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="" />
<option name="INTERPRETER_OPTIONS" value="" />
Expand Down
1 change: 1 addition & 0 deletions packages/kiota_abstractions/lib/kiota_abstractions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:std_uritemplate/std_uritemplate.dart';
import 'package:uuid/uuid.dart';

part 'src/api_client_builder.dart';
part 'src/api_exception.dart';
part 'src/authentication/access_token_provider.dart';
part 'src/authentication/allowed_hosts_validator.dart';
part 'src/authentication/anonymous_authentication_provider.dart';
Expand Down
21 changes: 20 additions & 1 deletion packages/kiota_abstractions/lib/src/api_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ApiException implements Exception {
this.statusCode,
this.message,
this.responseHeaders,
this.innerExceptions,
});

/// The HTTP status code of the response.
Expand All @@ -19,8 +20,26 @@ class ApiException implements Exception {
/// The headers of the response.
final Map<String, List<String>>? responseHeaders;

/// The inner exceptions that caused this exception.
final Iterable<Object?>? innerExceptions;

@override
String toString() {
return 'ApiException{statusCode: $statusCode, message: $message, headers: $responseHeaders}';
return 'ApiException{statusCode: $statusCode, message: $message, headers: $responseHeaders, innerExceptions: $innerExceptions}';
}

/// Creates a new instance of [ApiException] with the given parameters.
ApiException copyWith({
int? statusCode,
String? message,
Map<String, List<String>>? responseHeaders,
Iterable<Object?>? innerExceptions,
}) {
return ApiException(
statusCode: statusCode ?? this.statusCode,
message: message ?? this.message,
responseHeaders: responseHeaders ?? this.responseHeaders,
innerExceptions: innerExceptions ?? this.innerExceptions,
);
}
}
23 changes: 14 additions & 9 deletions packages/kiota_abstractions/lib/src/http_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,34 @@ part of '../kiota_abstractions.dart';
/// Represents the HTTP method used by a request.
enum HttpMethod {
/// The GET method requests a representation of the specified resource. Requests using GET should only retrieve data.
get,
get('GET'),

/// The POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.
post,
post('POST'),

/// The PATCH method is used to apply partial modifications to a resource.
patch,
patch('PATCH'),

/// The DELETE method deletes the specified resource.
delete,
delete('DELETE'),

/// The OPTIONS method is used to describe the communication options for the target resource.
options,
options('OPTIONS'),

/// The PUT method replaces all current representations of the target resource with the request payload.
put,
put('PUT'),

/// The HEAD method asks for a response identical to that of a GET request, but without the response body.
head,
head('HEAD'),

/// The CONNECT method establishes a tunnel to the server identified by the target resource.
connect,
connect('CONNECT'),

/// The TRACE method performs a message loop-back test along the path to the target resource.
trace,
trace('TRACE');

const HttpMethod(this.value);

/// The value or name of the method (for example "GET" or "POST").
final String value;
}
4 changes: 4 additions & 0 deletions packages/kiota_abstractions/lib/src/request_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ abstract class RequestAdapter {
/// the implementing adapter.
/// [T] is the type of the native request.
Future<T?> convertToNativeRequest<T>(RequestInformation requestInfo);

/// Enables the backing store proxies for the [SerializationWriter]s and
/// [ParseNode]s in use using the given [backingStoreFactory].
void enableBackingStore(BackingStoreFactory backingStoreFactory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class RequestInformation {
HttpHeaders get headers => _headers;

/// The request body.
Uint8List content = Uint8List(0);
Uint8List? content;

final Map<String, RequestOption> _requestOptions = {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ void main() {
expect(blank.pathParameters, <String, dynamic>{});
expect(blank.queryParameters, <String, dynamic>{});
expect(blank.headers, HttpHeaders());
expect(
blank.content,
isA<Uint8List>()
.having((content) => content.length, 'length', equals(0)),
);
expect(blank.content, isNull);
expect(blank.requestOptions, <RequestOption>[]);

expect(() => blank.uri, throwsArgumentError);
Expand Down
9 changes: 9 additions & 0 deletions packages/kiota_http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ Install the package in the generated project:
```bash
dart pub add kiota_http
```

## Development

To build the package, you first need to run `build_runner`.
It generates the necessary files for the package to work and for running the tests:

```bash
dart run build_runner build -d
```
1 change: 1 addition & 0 deletions packages/kiota_http/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:strict/analysis_options.yaml
6 changes: 6 additions & 0 deletions packages/kiota_http/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
targets:
$default:
builders:
build_version:
options:
output: lib/gen/version.dart
15 changes: 15 additions & 0 deletions packages/kiota_http/lib/kiota_http.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// This library implements a request adapter for generated
/// [Kiota](https://github.com/microsoft/kiota) clients.
library kiota_http;

import 'dart:typed_data';

import 'package:http/http.dart' as http;
import 'package:http/retry.dart' as retry;
import 'package:kiota_abstractions/kiota_abstractions.dart';
import 'package:kiota_http/gen/version.dart';
import 'package:uuid/uuid.dart';

part 'src/http_client_request_adapter.dart';
part 'src/kiota_client_factory.dart';
part 'src/middleware/user_agent_client.dart';
Loading
Loading