Skip to content

Commit

Permalink
Merge branch 'main' into issues/7-authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss authored Feb 15, 2024
2 parents 83b7872 + 9c459ed commit ff0e4fc
Show file tree
Hide file tree
Showing 15 changed files with 565 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ jobs:
run: dart pub get

- name: Verify formatting
continue-on-error: true
run: dart format --output=none --set-exit-if-changed .

- name: Analyze project source
continue-on-error: true
run: dart analyze --fatal-infos

- name: Run build_runner
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ From the comment at: https://github.com/microsoft/kiota/issues/2199#issuecomment
- [x] Parse node interface
- [x] Request adapter interface
WIP:
- [ ] Backing stores https://github.com/ricardoboss/dart_kiota_abstractions/pull/10
- [ ] Authentication https://github.com/ricardoboss/dart_kiota_abstractions/pull/12
Things other abstractions have, but this one doesn't:
- [ ] Backing stores
- [ ] Authentication
- [ ] Text serialization/deserialization
- [ ] JSON serialization/deserialization
- [ ] Form data serialization/deserialization
Expand Down
6 changes: 6 additions & 0 deletions lib/kiota_abstractions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ import 'dart:typed_data';

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

part 'src/authentication/allowed_hosts_validator.dart';
part 'src/authentication/access_token_provider.dart';
part 'src/authentication/anonymous_authentication_provider.dart';
part 'src/authentication/authentication_provider.dart';
part 'src/api_client_builder.dart';
part 'src/base_request_builder.dart';
part 'src/date_only.dart';
part 'src/error_mappings.dart';
part 'src/extensions/base_request_builder_extensions.dart';
part 'src/extensions/date_only_extensions.dart';
part 'src/extensions/map_extensions.dart';
part 'src/extensions/request_information_extensions.dart';
part 'src/extensions/time_only_extensions.dart';
part 'src/http_headers.dart';
part 'src/http_method.dart';
part 'src/multipart_body.dart';
Expand All @@ -47,4 +52,5 @@ part 'src/serialization/parse_node_proxy_factory.dart';
part 'src/serialization/serialization_writer.dart';
part 'src/serialization/serialization_writer_factory.dart';
part 'src/serialization/serialization_writer_factory_registry.dart';
part 'src/time_only.dart';
part 'src/serialization/serialization_writer_proxy_factory.dart';
5 changes: 4 additions & 1 deletion lib/src/base_request_builder.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
part of '../kiota_abstractions.dart';

/// Base class for all request builders.
abstract class BaseRequestBuilder {
abstract class BaseRequestBuilder<T extends BaseRequestBuilder<T>> {
BaseRequestBuilder(
this.requestAdapter,
this.urlTemplate,
Expand All @@ -16,4 +16,7 @@ abstract class BaseRequestBuilder {

/// Url template to use to build the URL for the current request builder.
String urlTemplate;

/// Clones the current request builder.
T clone();
}
67 changes: 67 additions & 0 deletions lib/src/date_only.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
part of '../kiota_abstractions.dart';

/// Interface for a date only object.
///
/// This interface provides an abstraction layer over date only objects.
/// It is used to represent date only values in a serialization format agnostic
/// way.
///
/// It can only be used to represent a date in the Gregorian calendar.
abstract class DateOnly {
/// Extracts the date part of a [DateTime] and creates an object implementing
/// [DateOnly].
factory DateOnly.fromDateTime(DateTime dateTime) {
return _DateOnlyImpl(
day: dateTime.day,
month: dateTime.month,
year: dateTime.year,
);
}

/// This factory uses the [DateTime.parse] method to create an object
/// implementing [DateOnly].
factory DateOnly.fromDateTimeString(String dateTimeString) {
final date = DateTime.parse(dateTimeString);

return DateOnly.fromDateTime(date);
}

/// Creates an object implementing [DateOnly] from the provided components.
factory DateOnly.fromComponents(
int year, [
int month = 1,
int day = 1,
]) {
return _DateOnlyImpl(
day: day,
month: month,
year: year,
);
}

/// Gets the year of the date.
int get year;

/// Gets the month of the date.
int get month;

/// Gets the day of the date.
int get day;
}

class _DateOnlyImpl implements DateOnly {
_DateOnlyImpl({
required this.day,
required this.month,
required this.year,
});

@override
final int day;

@override
final int month;

@override
final int year;
}
12 changes: 12 additions & 0 deletions lib/src/extensions/base_request_builder_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
part of '../../kiota_abstractions.dart';

extension BaseRequestBuilderExtensions<T extends BaseRequestBuilder<T>>
on BaseRequestBuilder<T> {
/// Clones the current request builder using [clone] and sets the given
/// [rawUrl] as the url to use.
///
/// This utilizes the [RequestInformation.rawUrlKey] to store the raw url
/// in the [pathParameters].
T withUrl(String rawUrl) => clone()
..pathParameters.addOrReplace(RequestInformation.rawUrlKey, rawUrl);
}
25 changes: 25 additions & 0 deletions lib/src/extensions/date_only_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
part of '../../kiota_abstractions.dart';

/// Extension methods for [DateOnly].
extension DateOnlyExtensions on DateOnly {
/// Converts the [DateOnly] to a [DateTime].
DateTime toDateTime() => DateTime(year, month, day);

/// Combines the [DateOnly] with the given [TimeOnly].
DateTime combine(TimeOnly time) {
return DateTime(
year,
month,
day,
time.hours,
time.minutes,
time.seconds,
time.milliseconds,
);
}

/// Converts the [DateOnly] to a string in the format `yyyy-MM-dd`.
String toRfc3339String() {
return '${year.toString().padLeft(4, '0')}-${month.toString().padLeft(2, '0')}-${day.toString().padLeft(2, '0')}';
}
}
46 changes: 46 additions & 0 deletions lib/src/extensions/time_only_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
part of '../../kiota_abstractions.dart';

/// Extension methods for [TimeOnly].
extension TimeOnlyExtensions on TimeOnly {
/// Converts the [TimeOnly] to a [DateTime].
DateTime toDateTime(
int year, [
int month = 1,
int day = 1,
]) =>
DateTime(
year,
month,
day,
hours,
minutes,
seconds,
milliseconds,
);

/// Combines the [TimeOnly] with the given [DateOnly].
DateTime combine(DateOnly date) {
return DateTime(
date.year,
date.month,
date.day,
hours,
minutes,
seconds,
milliseconds,
);
}

/// Converts the [TimeOnly] to a string in the format `HH:mm:ss` or
/// `HH:mm:ss.SSS` if milliseconds are present.
String toRfc3339String() {
final String fractionString;
if (milliseconds > 0) {
fractionString = '.${milliseconds.toString().padLeft(3, '0')}';
} else {
fractionString = '';
}

return '${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}$fractionString';
}
}
12 changes: 12 additions & 0 deletions lib/src/serialization/parse_node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,21 @@ abstract class ParseNode {
/// Gets the double value of the node.
double? getDoubleValue();

/// Gets the [UuidValue] value of the node.
UuidValue? getGuidValue();

/// Gets the [DateTime] value of the node.
DateTime? getDateTimeValue();

/// Gets the [DateOnly] value of the node.
DateOnly? getDateOnlyValue();

/// Gets the [TimeOnly] value of the node.
TimeOnly? getTimeOnlyValue();

/// Gets the [Duration] value of the node.
Duration? getDurationValue();

/// Gets the collection of primitive values of the node.
Iterable<T> getCollectionOfPrimitiveValues<T>();

Expand Down
75 changes: 75 additions & 0 deletions lib/src/time_only.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
part of '../kiota_abstractions.dart';

/// Interface for a time only object that represents a time of day.
///
/// This interface provides an abstraction layer over time only objects.
/// It is used to represent time only values in a serialization format agnostic
/// way.
abstract class TimeOnly {
/// Extracts the time part of a [DateTime] and creates an object implementing
/// [TimeOnly].
factory TimeOnly.fromDateTime(DateTime dateTime) {
return _TimeOnlyImpl(
hours: dateTime.hour,
minutes: dateTime.minute,
seconds: dateTime.second,
milliseconds: dateTime.millisecond,
);
}

/// This factory uses the [DateTime.parse] method to create an object
/// implementing [TimeOnly].
factory TimeOnly.fromDateTimeString(String dateTimeString) {
final dateTime = DateTime.parse('2024-01-01 $dateTimeString');

return TimeOnly.fromDateTime(dateTime);
}

/// Constructs an object implementing [TimeOnly] from the provided components.
factory TimeOnly.fromComponents(
int hours,
int minutes, [
int seconds = 0,
int milliseconds = 0,
]) {
return _TimeOnlyImpl(
hours: hours,
minutes: minutes,
seconds: seconds,
milliseconds: milliseconds,
);
}

/// Gets the hours of the time.
int get hours;

/// Gets the minutes of the time.
int get minutes;

/// Gets the seconds of the time.
int get seconds;

/// Gets the milliseconds of the time.
int get milliseconds;
}

class _TimeOnlyImpl implements TimeOnly {
_TimeOnlyImpl({
required this.hours,
required this.minutes,
required this.seconds,
required this.milliseconds,
});

@override
final int hours;

@override
final int minutes;

@override
final int seconds;

@override
final int milliseconds;
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ environment:

dependencies:
std_uritemplate: ^0.0.52
uuid: ^4.3.3

dev_dependencies:
strict: ^2.0.0
Expand Down
40 changes: 40 additions & 0 deletions test/date_only_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// ignore_for_file: avoid_redundant_argument_values

import 'package:kiota_abstractions/kiota_abstractions.dart';
import 'package:test/test.dart';

void main() {
group('DateOnly', () {
test('fromDateTimeString and toRfc3339String', () {
expect(
DateOnly.fromDateTimeString('2021-01-01').toRfc3339String(),
'2021-01-01',
);
});

test('round trip', () {
final fromString = DateOnly.fromDateTimeString('2021-01-01');
final toString = fromString.toRfc3339String();
expect(toString, '2021-01-01');

final roundTrip = DateOnly.fromDateTimeString(toString);
final roundTripString = roundTrip.toRfc3339String();
expect(roundTripString, '2021-01-01');
});

test('fromDateTime', () {
final dateTime = DateTime(2024, 2, 3, 12, 34, 56);
expect(
DateOnly.fromDateTime(dateTime).toRfc3339String(),
'2024-02-03',
);
});

test('fromComponents', () {
expect(
DateOnly.fromComponents(2021, 1, 1).toRfc3339String(),
'2021-01-01',
);
});
});
}
Loading

0 comments on commit ff0e4fc

Please sign in to comment.