Created
July 21, 2019 12:42
-
-
Save SAGARSURI/105c02138849fb17478042232034531e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import 'dart:async'; | |
import 'dart:convert'; | |
import 'package:http/http.dart' show Client, Response; | |
import '../models/item_model.dart'; | |
import '../models/trailer_model.dart'; | |
import 'package:inject/inject.dart'; | |
import '../models/state.dart'; | |
class MovieApiProvider { | |
final Client client; | |
final _apiKey = 'api-key'; | |
final _baseUrl = "http://api.themoviedb.org/3/movie"; | |
@provide | |
MovieApiProvider(this.client); | |
Future<State> fetchMovieList() async { | |
Response response; | |
if(_apiKey != 'api-key') { | |
response = await client.get("$_baseUrl/popular?api_key=$_apiKey"); | |
}else{ | |
return State<String>.error('Please add your API key'); | |
} | |
if (response.statusCode == 200) { | |
// If the call to the server was successful, parse the JSON | |
return State<ItemModel>.success(ItemModel.fromJson(json.decode(response.body))); | |
} else { | |
// If that call was not successful, throw an error. | |
return State<String>.error(response.statusCode.toString()); | |
} | |
} | |
Future<State> fetchTrailer(int movieId) async { | |
final response = | |
await client.get("$_baseUrl/$movieId/videos?api_key=$_apiKey"); | |
if (response.statusCode == 200) { | |
return State<TrailerModel>.success(TrailerModel.fromJson(json.decode(response.body))); | |
} else { | |
return State<String>.error(response.statusCode.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment