api.dart 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:http/http.dart' as http;
  4. import 'package:rxdart/rxdart.dart';
  5. import 'package:empty/models/album.dart';
  6. import 'package:empty/models/bbs.dart';
  7. import 'package:empty/constants.dart';
  8. class AlbumBloc {
  9. final _albumSubject = BehaviorSubject<Album>();
  10. final _albumGuide = BehaviorSubject<Album>();
  11. final _bbsList = BehaviorSubject<Bbs>();
  12. Future<Bbs> getData(String type,String page, String size) async {
  13. var response = await http.get(
  14. kConfig['bbs'] + type + "?page=" + page + "&size=" + size,
  15. headers: {'Content-Type':'application/json','x-aliot-token':kEmptyToken},
  16. );
  17. if (response.statusCode == 200) {
  18. Bbs result = Bbs.fromJson( json.decode(response.body) );
  19. return result;
  20. } else {
  21. print('Request failed with status: ${response.statusCode}.');
  22. }
  23. }
  24. Future<Album> fetchData(String type, String section) async {
  25. var response = await http.post(
  26. kConfig['list'],
  27. body: jsonEncode({'type':type,'section':section,'rcmd':'','size':20,'page':1}),
  28. headers: {'Content-Type':'application/json','x-aliot-token':kEmptyToken},
  29. );
  30. //print(kUserInfo['emptyToken']);
  31. if (response.statusCode == 200) {
  32. // print('album : ' + response.body);
  33. Album result = Album.fromJson( json.decode(response.body) );
  34. return result;
  35. } else {
  36. print('Request failed with status: ${response.statusCode}.');
  37. }
  38. }
  39. void fetch(String type, String section) async {
  40. var albumResult = await fetchData(type,section);
  41. _albumSubject.add(albumResult);
  42. }
  43. void guide(String type, String section) async {
  44. var albumResult = await fetchData(type,section);
  45. _albumGuide.add(albumResult);
  46. }
  47. void bbs(String type,String page, String size) async {
  48. var albumResult = await getData(type,page,size);
  49. _bbsList.add(albumResult);
  50. }
  51. Stream<Album> get albumResult => _albumSubject.stream;
  52. Stream<Album> get albumGuide => _albumGuide.stream;
  53. Stream<Bbs> get bbsList => _bbsList.stream;
  54. }