-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 🔖 release v7.3.0 # chopper ## 7.3.0 - Add support for `@Tag` annotation ([#586](#586)) # chopper_generator ## 7.3.0 - Add support for `@Tag` annotation ([#586](#586)) --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: wenchieh <[email protected]>
- Loading branch information
Showing
15 changed files
with
253 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/// @author luwenjie on 2024/3/20 11:38:11 | ||
/// | ||
/// | ||
/// | ||
import "package:chopper/chopper.dart"; | ||
|
||
import 'definition.dart'; | ||
|
||
part 'tag.chopper.dart'; | ||
|
||
Future<void> main() async { | ||
final chopper = ChopperClient( | ||
baseUrl: Uri.parse('http://localhost:8000'), | ||
services: [ | ||
// the generated service | ||
TagService.create(ChopperClient()), | ||
], | ||
interceptors: [ | ||
TagInterceptor(), | ||
], | ||
converter: JsonConverter(), | ||
); | ||
|
||
final myService = chopper.getService<MyService>(); | ||
|
||
final response = await myService.getMapResource('1'); | ||
print(response.body); | ||
|
||
final list = await myService.getListResources(); | ||
print(list.body); | ||
chopper.dispose(); | ||
} | ||
|
||
// add a uniform appId header for some path | ||
class BizTag { | ||
final int appId; | ||
|
||
BizTag({this.appId = 0}); | ||
} | ||
|
||
class IncludeBodyNullOrEmptyTag { | ||
bool includeNull = false; | ||
bool includeEmpty = false; | ||
|
||
IncludeBodyNullOrEmptyTag(this.includeNull, this.includeEmpty); | ||
} | ||
|
||
class TagConverter extends JsonConverter { | ||
FutureOr<Request> convertRequest(Request request) { | ||
final tag = request.tag; | ||
if (tag is IncludeBodyNullOrEmptyTag) { | ||
if (request.body is Map) { | ||
final Map body = request.body as Map; | ||
final Map bodyCopy = {}; | ||
for (final MapEntry entry in body.entries) { | ||
if (!tag.includeNull && entry.value == null) continue; | ||
if (!tag.includeEmpty && entry.value == "") continue; | ||
bodyCopy[entry.key] = entry.value; | ||
} | ||
request = request.copyWith(body: bodyCopy); | ||
} | ||
} | ||
} | ||
} | ||
|
||
class TagInterceptor implements RequestInterceptor { | ||
FutureOr<Request> onRequest(Request request) { | ||
final tag = request.tag; | ||
if (tag is BizTag) { | ||
request.headers["x-appId"] = tag.appId; | ||
} | ||
return request; | ||
} | ||
} | ||
|
||
@ChopperApi(baseUrl: '/tag') | ||
abstract class TagService extends ChopperService { | ||
static TagService create(ChopperClient client) => _$TagService(client); | ||
|
||
@get(path: '/bizRequest') | ||
Future<Response> requestWithTag({@Tag() BizTag tag = const BizTag()}); | ||
|
||
@get(path: '/include') | ||
Future<Response> includeBodyNullOrEmptyTag( | ||
{@Tag() | ||
IncludeBodyNullOrEmptyTag tag = const IncludeBodyNullOrEmptyTag()}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.