Skip to content

Commit

Permalink
refactor: rename Post to Article to avoid name clash with Chopper's P…
Browse files Browse the repository at this point in the history
…ost class
  • Loading branch information
techouse committed Jun 9, 2024
1 parent 52ca289 commit 15c317f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 85 deletions.
36 changes: 18 additions & 18 deletions examples/alice_chopper/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import 'package:alice/alice.dart';
import 'package:alice_chopper/alice_chopper_adapter.dart';
import 'package:example/interceptors/json_content_type_inerceptor.dart';
import 'package:example/interceptors/json_headers_interceptor.dart';
import 'package:example/models/example_post.dart';
import 'package:example/models/article.dart';
import 'package:example/services/converters/json_serializable_converter.dart';
import 'package:example/services/example_posts_service.dart';
import 'package:example/services/articles_service.dart';
import 'package:chopper/chopper.dart';
import 'package:flutter/material.dart';

Expand All @@ -24,13 +24,13 @@ class _MyAppState extends State<MyApp> {
)..addAdapter(_aliceChopperAdapter);

final JsonSerializableConverter converter = JsonSerializableConverter({
ExamplePost: ExamplePost.fromJson,
Article: Article.fromJson,
});

late final ChopperClient _chopper = ChopperClient(
baseUrl: Uri.https('jsonplaceholder.typicode.com'),
services: [
ExamplePostsService.create(),
ArticlesService.create(),
],
interceptors: [
JsonHeadersInterceptor(),
Expand All @@ -41,26 +41,26 @@ class _MyAppState extends State<MyApp> {
);

Future<void> _runChopperHttpRequests() async {
final ExamplePostsService postService =
_chopper.getService<ExamplePostsService>();
final ArticlesService articlesService =
_chopper.getService<ArticlesService>();

final ExamplePost post = ExamplePost(
final Article article = Article(
title: 'foo',
body: 'bar',
userId: 1,
);

postService.getPosts();
postService.getPosts(userId: 2);
postService.createPost(post);
postService.getPost(1);
postService.putPost(1, post.copyWith(id: 1));
postService.patchPost(1, post.copyWith(id: 1));
postService.deletePost(1);
postService.putPost(123456, post.copyWith(id: 123456));
postService.patchPost(123456, post.copyWith(id: 123456));
postService.getPost(123456);
postService.deletePost(123456);
articlesService.getAll();
articlesService.getAll(userId: 2);
articlesService.post(article);
articlesService.get(1);
articlesService.put(1, article.copyWith(id: 1));
articlesService.patch(1, article.copyWith(id: 1));
articlesService.delete(1);
articlesService.put(123456, article.copyWith(id: 123456));
articlesService.patch(123456, article.copyWith(id: 123456));
articlesService.get(123456);
articlesService.delete(123456);
}

void _runHttpInspector() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import 'package:json_annotation/json_annotation.dart';

part 'example_post.g.dart';
part 'article.g.dart';

@JsonSerializable()
class ExamplePost {
class Article {
final int? id;
final String title;
final String body;
final int userId;

const ExamplePost({
const Article({
this.id,
required this.title,
required this.body,
required this.userId,
});

ExamplePost copyWith({
Article copyWith({
int? id,
String? title,
String? body,
int? userId,
}) =>
ExamplePost(
Article(
id: id ?? this.id,
title: title ?? this.title,
body: body ?? this.body,
userId: userId ?? this.userId,
);

factory ExamplePost.fromJson(Map<String, dynamic> json) =>
_$ExamplePostFromJson(json);
factory Article.fromJson(Map<String, dynamic> json) =>
_$ArticleFromJson(json);

Map<String, dynamic> toJson() => _$ExamplePostToJson(this);
Map<String, dynamic> toJson() => _$ArticleToJson(this);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions examples/alice_chopper/lib/services/articles_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:chopper/chopper.dart';
import 'package:example/models/article.dart';

// this is necessary for the generated code to find your class
part 'articles_service.chopper.dart';

@ChopperApi(baseUrl: '/posts')
abstract class ArticlesService extends ChopperService {
// helper methods that help you instantiate your service
static ArticlesService create([ChopperClient? client]) =>
_$ArticlesService(client);

@Get(path: '/', timeout: Duration(seconds: 10))
Future<Response<List<Article>>> getAll({
@Query() int? userId,
});

@Get(path: '/{id}', timeout: Duration(seconds: 10))
Future<Response<Article?>> get(@Path() int id);

@Post(path: '/', timeout: Duration(seconds: 10))
Future<Response<Article?>> post(@Body() Article body);

@Put(path: '/{id}', timeout: Duration(seconds: 10))
Future<Response<Article?>> put(@Path() int id, @Body() Article body);

@Patch(path: '/{id}', timeout: Duration(seconds: 10))
Future<Response<Article?>> patch(@Path() int id, @Body() Article body);

@Delete(path: '/{id}', timeout: Duration(seconds: 10))
Future<Response<void>> delete(@Path() int id);
}
38 changes: 0 additions & 38 deletions examples/alice_chopper/lib/services/example_posts_service.dart

This file was deleted.

0 comments on commit 15c317f

Please sign in to comment.