Dart:Scalable Application Development
上QQ阅读APP看书,第一时间看更新

Passing parameters to the API

One method of providing input to a REST API is via the calling URL, and this will be used to get a specified number of entries from the database using the following URL:

http://127.0.0.1:8080/api/quake/v1/recent/100

The implementation involves extending the pattern of the path with a curly bracket syntax (these correspond to the String parameters in the function):

  @ApiMethod(path: 'recent/{count}')
  Future<List<String>> recent(String count) async {
    DaoQuakeAPI quakeAPI = new DaoQuakeAPI();
    return await quakeAPI.fetchRecent(int.parse(count));
  }

The int.parse method is used to convert the count string into a database query parameter.