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

Request extends http.BaseRequest #370

Merged
merged 19 commits into from Oct 13, 2022
Merged

Conversation

techouse
Copy link
Collaborator

@techouse techouse commented Oct 9, 2022

I think that Request should inherit from BaseRequest.

The added benefit of this is that Request ceases to be a Chopper-specific class and now behaves like a BaseRequest.

The only downside is that Request can no longer be immutable and have a const constructor as BaseClass and all its children also aren't immutable and don't have const constructors.

class Request extends http.BaseRequest {
  /// I renamed this to [path] because it is essentially the same as [Uri.path]
  final String path;
  /// I renamed this to [origin] because it is essentially the same as [Uri.origin]
  final String origin;
  final dynamic body;
  final Map<String, dynamic> parameters;
  final bool multipart;
  final List<PartValue> parts;
  final bool useBrackets;

  Request(
    String method,
    this.path,
    this.origin, {
    this.body,
    this.parameters = const {},
    Map<String, String> headers = const {},
    this.multipart = false,
    this.parts = const [],
    this.useBrackets = false,
  }) : super(
          method,
          /// here I used [buildUrl] to construct a [Uri] and use it as the [url] parameter 
          /// of the parent [BaseRequest] constructor
          buildUri(origin, path, parameters, useBrackets: useBrackets),
        ) {
    /// Updating [BaseRequest.headers] is just nasty 🤪
    this.headers.addAll(headers);
  }

  /// Build the Chopper [Request] using a [Uri] instead of a [path] and [origin].
  /// Both the query parameters in the [Uri] and those provided explicitly in
  /// the [parameters] are merged together.
  Request.uri(
    String method,
    Uri url, {
    this.body,
    Map<String, dynamic>? parameters,
    Map<String, String> headers = const {},
    this.multipart = false,
    this.parts = const [],
    this.useBrackets = false,
  })  : origin = url.origin,
        path = url.path,
        parameters = {...url.queryParametersAll, ...?parameters},
        super(
          method,
          buildUri(
            url.origin,
            url.path,
            {...url.queryParametersAll, ...?parameters},
            useBrackets: useBrackets,
          ),
        ) {
    this.headers.addAll(headers);
  }

  /// These methods that were before sitting outside are now class methods
  /// Their API if therefore now vastly simpler.

  /// Convert this [Request] to a [http.Request]
  @visibleForTesting
  http.Request toHttpRequest() {
    // ... stuff
  }
  
  /// Convert this [Request] to a [http.MultipartRequest]
  @visibleForTesting
  Future<http.MultipartRequest> toMultipartRequest() async {
    // ... stuff
  }
  
  /// Convert this [Request] to a [http.StreamedRequest]
  @visibleForTesting
  http.StreamedRequest toStreamedRequest(Stream<List<int>> bodyStream) {
    // ... stuff
  }

  // ... stuff
}

I'll work on this PR over the coming days to add some more tests and fix some broken ones.

Lemme know if you even agree with this. 🙈

@techouse techouse added the enhancement New feature or request label Oct 9, 2022
@techouse techouse changed the title Request extends http.BaseRequest Request should inherit from BaseRequest Oct 9, 2022
@techouse techouse changed the title Request should inherit from BaseRequest Request extends http.BaseRequest Oct 9, 2022
@techouse
Copy link
Collaborator Author

techouse commented Oct 9, 2022

I noticed that for some reason it was expected for buildUri to do this:

buildUri('foo/', '/bar'); // --> 'foo//bar'

I believe that to be an error. This should be either

  • foo/bar (merging both so a single /)
  • /bar (the / in the 2nd part denotes a root which discards the 1st part)

I went with he 1st option: a single slash

buildUri('foo/', '/bar'); // --> 'foo/bar'

@techouse techouse marked this pull request as ready for review October 9, 2022 21:20
@@ -1,11 +1,10 @@
import 'dart:async';

import 'package:chopper/src/constants.dart';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, why absolute paths btw? :) Personal preference?

Copy link
Collaborator Author

@techouse techouse Oct 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the lint always_use_package_imports and the fact that I hate mixing the 2. Even in tests, it makes me cringe.

Check this Google Sheet https://docs.google.com/spreadsheets/d/1Nc1gFjmCOMubWZD7f2E4fLhWN7LYaOE__tsA7bf2NjA/edit#gid=0

@JEuler
Copy link
Collaborator

JEuler commented Oct 11, 2022 via email

@JEuler
Copy link
Collaborator

JEuler commented Oct 12, 2022

Thank you so much for hard work!

@JEuler
Copy link
Collaborator

JEuler commented Oct 12, 2022

I think the 'immutability' in this case is not 'important' 👍

@techouse
Copy link
Collaborator Author

Thank you so much for hard work!

My pleasure. 😇

@JEuler JEuler merged commit e3fd623 into lejard-h:develop Oct 13, 2022
@techouse techouse deleted the base-request branch October 14, 2022 09:01
JEuler added a commit that referenced this pull request Oct 15, 2022
* Fix Header Option Casting (#260)

Co-authored-by: Ivan Terekhin <i.terhin@gmail.com>

* Fix for #259 (#263)

* 4.0.1 fixes (#264)

* analyzer dependency upgraded (#296)

* fix(generator): fix PartValueFile value not nullable if arg is (#288) (#293)

* Chopper generator release 4.0.2 (#297)

* fix: fix this.body cast of null value when response body is null (#291) (#292)

* Interpolation fixes (#275)

* encodeQueryComponent now encodeComponent (#278)

* Prevent double call on token refreshment (#276)

* Fixes for #309 #308 (#310)

* Remove new keyword from interceptors.md (#312)

* Analyzer upgrade (#320)

Co-authored-by: István Juhos <stewemetal@gmail.com>

* Add unnecessary_brace_in_string_interps to lint ignores (#317)

* Extend pragma to quiet the linter (#318)

Co-authored-by: Ivan Terekhin <i.terhin@gmail.com>

* Fix converter getting called twice if using an authenticator with a JsonConverter on the request (#324)

* migrate example to nullsafety (#331)

* Resolve problem in main_json_serializable example (#328)

* Add @FiledMap @PartMap @PartFileMap (#335)

Co-authored-by: Meysam Karimi <mysmartapply.it4@gmail.com>

* Upgrade of analyzer (#340)

* Fix nullable QueryMap fails to compile (#344)

* Change return type of decodeJson to FutureOr in order to be able to support compute() (#345)

* Migrate from pedantic to lints ^2.0.0 with lints/recommended.yaml (#349)

* Version bumped for release (#352)

* Revert analyzer to ^4.1.0 and silence linters for Element.enclosingElement (#354)

* [chopper_generator] Update analyzer to ^4.4.0 and code_builde to ^4.3.0 and migrate deprecated code (#358)

* Add Makefiles to streamline development (#357)

* Add Bug Report Github issue template (#359)

* [chopper_generator] Add types to the generated variables (#360)

* Provide an example using an Isolate Worker Pool with Squadron (#361)

* mapToQuery changes (#364)

* Version bumped / changelog update (#367)

* Request extends http.BaseRequest (#370)

* Exclude null query vars by default and add new @method annotation includeNullQueryVars (#372)

* 5.1.0 (dev) (#373)

Co-authored-by: Ivan Terekhin <231950+JEuler@users.noreply.github.com>

Co-authored-by: Youssef Raafat <youssefraafatnasry@gmail.com>
Co-authored-by: luis901101 <luis901101@gmail.com>
Co-authored-by: melvspace <ratealt@gmail.com>
Co-authored-by: Michal Šrůtek <35694712+michalsrutek@users.noreply.github.com>
Co-authored-by: István Juhos <stewemetal@gmail.com>
Co-authored-by: Andre <andre.lipke@gmail.com>
Co-authored-by: John Wimer <john@wimer.org>
Co-authored-by: Max Röhrl <max.roehrl11@gmail.com>
Co-authored-by: ipcjs <gipcjs@gmail.com>
Co-authored-by: ibadin <exbatek@gmail.com>
Co-authored-by: Meysam Karimi <31154534+meysam1717@users.noreply.github.com>
Co-authored-by: Meysam Karimi <mysmartapply.it4@gmail.com>
Co-authored-by: Klemen Tusar <techouse@gmail.com>
Co-authored-by: Klemen Tusar <k.tusar@cmcmarkets.com>
Co-authored-by: Ivan Terekhin <231950+JEuler@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants