If you have this error, you probably forgot to run the build package. To do that, simply run the following command in your shell.
pub run build_runner build
It will generate the code that actually do the HTTP request (YOUR_FILE.chopper.dart). If you wish to update the code automatically when you change your definition run the watch command.
pub run build_runner watch
How to increase timeout ?
Connection timeout is very limited for now due to http package (see: dart-lang/http#21)
But if you are on VM or Flutter you can set the connectionTimeout you want
You may need to change the base URL of your network calls during runtime, for example, if you have to use different servers or routes dynamically in your app in case of a "regular" or a "paid" user. You can store the current server base url in your SharedPreferences (encrypt/decrypt it if needed) and use it in an interceptor like this:
Chopper is built on top of http package and you can override the inner http client.
import'dart:io';import'package:http/io_client.dart'as http;final ioc =newHttpClient();ioc.findProxy = (url) =>'PROXY 192.168.0.102:9090';ioc.badCertificateCallback = (X509Certificate cert, String host, int port)=>true;final chopper =ChopperClient( client: http.IOClient(ioc),);
Authorized HTTP requests
Basically, the algorithm goes like this (credits to stewemetal):
Add the authentication token to the request (by "Authorization" header, for example) -> try the request -> if it fails use the refresh token to get a new auth token -> if that succeeds, save the auth token and retry the original request with it if the refresh token is not valid anymore, drop the session (and navigate to the login screen, for example)
Simple code example:
classAuthInterceptorimplementsInterceptor {@overrideFutureOr<Response<BodyType>> intercept<BodyType>(Chain<BodyType> chain) async {final request =applyHeader(chain.request, 'authorization',SharedPrefs.localStorage.getString(tokenHeader), override:false);final response =await chain.proceed(request);if (response?.statusCode ==401) {SharedPrefs.localStorage.remove(tokenHeader);// Navigate to some login page or just request new token }return response; }}...interceptors: [AuthInterceptor(),// ... other interceptors ]...
The actual implementation of the algorithm above may vary based on how the backend API - more precisely the login and session handling - of your app looks like. Breaking out of the authentication flow/inteceptor can be achieved in multiple ways. For example by throwing an exception or by using a service handles navigation. See interceptor for more info.
Authorized HTTP requests using the special Authenticator interceptor
Similar to OkHTTP's authenticator, the idea here is to provide a reactive authentication in the event that an auth challenge is raised. It returns a nullable Request that contains a possible update to the original Request to satisfy the authentication challenge.
import'dart:async'show FutureOr;import'dart:io'show HttpHeaders, HttpStatus;import'package:chopper/chopper.dart';/// This method returns a [Request] that includes credentials to satisfy an authentication challenge received in/// [response]. It returns `null` if the challenge cannot be satisfied.classMyAuthenticatorextendsAuthenticator {@overrideFutureOr<Request?> authenticate(Request request,Response response, [Request? originalRequest, ]) async {if (response.statusCode ==HttpStatus.unauthorized) {finalString? newToken =awaitrefreshToken();if (newToken !=null) {return request.copyWith(headers: { ...request.headers,HttpHeaders.authorizationHeader: newToken, }); } }returnnull; }Future<String?> refreshToken() async {/// Refresh the accessToken using refreshToken however needed. /// This could be done either via an HTTP client, or a ChopperService, or a /// repository could be a dependency. /// This approach is intentionally not opinionated about how this works.throwUnimplementedError(); }}/// When initializing your ChopperClientfinal client =ChopperClient(/// register your Authenticator here authenticator:MyAuthenticator(),);
Decoding JSON using Isolates
Sometimes you want to decode JSON outside the main thread in order to reduce janking. In this example we're going to go even further and implement a Worker Pool using Squadron which can dynamically spawn a maximum number of Workers as they become needed.
It goes without saying that running the code generation is a pre-requisite at this stage
flutterpubrunbuild_runnerbuild
Changing the default extension of the generated files
If you want to change the default extension of the generated files from .chopper.dart to something else, you can do so by adding the following to your build.yaml file:
targets:$default:builders:chopper_generator:options:# This assumes you want the files to end with `.chopper.g.dart`# instead of the default `.chopper.dart`.build_extensions: { ".dart": [ ".chopper.g.dart" ] }
Configure a WorkerPool and run the example
/// inspired by https://github.com/d-markey/squadron_sample/blob/main/lib/main.dartvoidinitSquadron(String id) {Squadron.setId(id);Squadron.setLogger(ConsoleSquadronLogger());Squadron.logLevel =SquadronLogLevel.all;Squadron.debugMode =true;}Future<void> main() async {/// initialize Squadron before using itinitSquadron('worker_pool_example');final jsonDecodeServiceWorkerPool =JsonDecodeServiceWorkerPool(// Set whatever you want here concurrencySettings:ConcurrencySettings.oneCpuThread, );/// start the Worker Poolawait jsonDecodeServiceWorkerPool.start();/// Instantiate the JsonConverter from abovefinal converter =JsonSerializableWorkerPoolConverter( {Resource:Resource.fromJsonFactory, },/// make sure to provide the WorkerPool to the JsonConverter jsonDecodeServiceWorkerPool, );/// Instantiate a ChopperClientfinal chopper =ChopperClient( client: client, baseUrl:Uri.parse('http://localhost:8000'),// bind your object factories here converter: converter, errorConverter: converter, services: [// the generated serviceMyService.create(), ],/* Interceptor */ interceptors: [authHeader], );/// Do stuff with myServicefinal myService = chopper.getService<MyService>();/// ...stuff... /// stop the Worker Pool once done jsonDecodeServiceWorkerPool.stop();}
This barely scratches the surface. If you want to know more about squadron and squadron_builder make sure to head over to their respective repositories.
Define your ChopperService as usual. Annotate the class with @lazySingleton (or other type if preferred) and use the @factoryMethod annotation to specify the factory method for the service. This would normally be the static create method.