aau-giraf / api_client

API client for Flutter to communicate with the web-api
GNU General Public License v3.0
6 stars 0 forks source link

As a developer I would like to have http responses returned from methods in the api client in order to be able to react differently depending on the response code. #89

Closed JeppeKLau closed 3 years ago

JeppeKLau commented 3 years ago

Currently the api client returns differing types of data. It can eg. be a boolean for logging in. The official Flutter approach to fetching data from the internet, seen here, is to use the built in http package.

I would like to see the official Flutter approach implemented as this enables the possibility of reacting to the different error codes given back from the web api. This removes the need to throw exceptions when making api calls, due to having the error codes available at the point where the api call is made. The implementation of this issue will also require removing the http class in the api client. This is to avoid conflicts when implementing functionality in the api client.

An example of the refactor is the login method:

Stream<bool> login(String username, String password) {
    return _http.post('/login', <String, String>{
      'username': username,
      'password': password,
    }).flatMap((Response res) =>
        Stream<bool>.fromFuture(_persist.set('token', res.json['data'])));
  }

That is the method as of writing this issue. Next is the new version:

Future<Response> login(String username, String password){
    return _http.post('/login', <String, String>{
      'username': username,
      'password': password,
    })

Another aspect of this refactor is altering the methods in the Http class to return Future<Response> instead of Stream<Response>, as these streams often arent disposed of properly and are only used for singular pieces of data. This also fits into the Flutter approach as the Future<T> class enables await of methods.