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

✨ Feature: Omit Response in service #545

Merged
merged 4 commits into from
Jan 5, 2024

Conversation

Guldem
Copy link
Contributor

@Guldem Guldem commented Jan 3, 2024

Added the possibility to omit Response when creating a service to reduce some boilerplate if the Response object is not really needed.

This give the developer the choice to make use of the Response and its meta data is needed. This might not always be case because a error converter deals with the error logic.

TODO:
Update documentation for this feature

Previously a ChopperService was created like this (still valid):

@ChopperApi(baseUrl: '/test')
abstract class MyChopperService extends ChopperService {
  static MyChopperService create([ChopperClient? client]) =>
      _$MyChopperService(client);

  @Get(path: 'get/{id}')
  Future<Response<String>> getSomething(
    @Path() String id, {
    @Header('test') required String dynamicHeader,
  });
}

Which creates:

final class _$MyChopperService extends MyChopperService {
  _$MyChopperService([ChopperClient? client]) {
    if (client == null) return;
    this.client = client;
  }

  @override
  final Type definitionType = MyChopperService;

  @override
  Future<Response<String>> getSomething(
    String id, {
    required String dynamicHeader,
  }) {
    final Uri $url = Uri.parse('/test/get/${id}');
    final Map<String, String> $headers = {
      'test': dynamicHeader,
    };
    final Request $request = Request(
      'GET',
      $url,
      client.baseUrl,
      headers: $headers,
    );
    return client.send<String, String>($request);
  }

Now its also possible to specify a service like this:

@ChopperApi(baseUrl: '/test')
abstract class MyChopperService extends ChopperService {
  static MyChopperService create([ChopperClient? client]) =>
      _$MyChopperService(client);

  @Get(path: 'get/{id}')
  Future<String> getSomething(
    @Path() String id, {
    @Header('test') required String dynamicHeader,
  });
}

Which creates this:

final class _$MyChopperService extends MyChopperService {
  _$MyChopperService([ChopperClient? client]) {
    if (client == null) return;
    this.client = client;
  }

  @override
  final Type definitionType = MyChopperService;

  @override
  Future<String> getSomething(
    String id, {
    required String dynamicHeader,
  }) async {
    final Uri $url = Uri.parse('/test/get/${id}');
    final Map<String, String> $headers = {
      'test': dynamicHeader,
    };
    final Request $request = Request(
      'GET',
      $url,
      client.baseUrl,
      headers: $headers,
    );
    final Response $response = await client.send<String, String>($request);
    return $response.bodyOrThrow;
  }

Copy link

codecov bot commented Jan 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (4ba5a83) 94.06% compared to head (d6170ea) 94.17%.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #545      +/-   ##
===========================================
+ Coverage    94.06%   94.17%   +0.11%     
===========================================
  Files           10       11       +1     
  Lines          472      481       +9     
===========================================
+ Hits           444      453       +9     
  Misses          28       28              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@techouse techouse added the enhancement New feature or request label Jan 3, 2024
@techouse techouse self-assigned this Jan 3, 2024
@techouse
Copy link
Collaborator

techouse commented Jan 3, 2024

Nice addition!

What's the plan for APIs that don't return anything, i.e. a DELETE request that only returns an HTTP status code 204?

@Delete(path: 'items/{id}')
Future<void> deleteItem(@Path() String id);

Like this ⬆️?

@Guldem
Copy link
Contributor Author

Guldem commented Jan 4, 2024

Nice addition!

What's the plan for APIs that don't return anything, i.e. a DELETE request that only returns an HTTP status code 204?

@Delete(path: 'items/{id}')
Future<void> deleteItem(@Path() String id);

Like this ⬆️?

When the service has Future<void> this will also be generated in the service implementation. I only wasn't able to find what the actual body becomes when a NoContent/204 occurs.

Added a extra tests for bodyOrThrow to test it. But this fail if the body would be set to null then it would throw the exception. But looking at this line it would expect atleast a empty String.

Not completly sure though.

@Guldem Guldem requested a review from techouse January 5, 2024 06:55
Copy link
Collaborator

@techouse techouse left a comment

Choose a reason for hiding this comment

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

LGTM

@techouse
Copy link
Collaborator

techouse commented Jan 5, 2024

Do you plan to update the docs in this PR?

@Guldem
Copy link
Contributor Author

Guldem commented Jan 5, 2024

Was thinking on doing separate separate PR for updating docs and include some more information on creating chopper services.

@techouse techouse merged commit 6420778 into lejard-h:develop Jan 5, 2024
6 checks passed
techouse added a commit that referenced this pull request Jan 8, 2024
# chopper

## 7.1.0

- #545
- #543
- #548

# chopper_generator

## 7.1.0

- #545
- #549
@techouse techouse mentioned this pull request Jan 8, 2024
techouse added a commit that referenced this pull request Jan 9, 2024
techouse added a commit that referenced this pull request Jan 9, 2024
## 7.0.11

- #545
- #543
- #548

# chopper_generator

## 7.0.8

- #545
- #549
@techouse techouse mentioned this pull request Jan 9, 2024
techouse added a commit that referenced this pull request Jan 9, 2024
## 7.1.0

- #545
- #543
- #548

# chopper_generator

## 7.1.0

- #545
- #549
@techouse techouse mentioned this pull request Jan 9, 2024
@Guldem Guldem deleted the feature/omit_response_object branch April 6, 2024 18:40
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.

2 participants