|
| 1 | +// ignore_for_file: long-parameter-list |
| 2 | + |
| 3 | +import 'dart:convert' show jsonDecode; |
| 4 | +import 'package:http/http.dart' as http show get, ClientException, Response; |
| 5 | + |
| 6 | +import 'order.dart'; |
| 7 | +import 'sort.dart'; |
| 8 | +import '../env/env.dart'; |
| 9 | +import '../extensions/unix_date_time_extension.dart'; |
| 10 | +import '../models/item_list.dart'; |
| 11 | +import '../models/question.dart'; |
| 12 | + |
| 13 | +class StackExchangeService { |
| 14 | + /// Read more https://api.stackexchange.com/docs/advanced-search |
| 15 | + static Future<ItemList<Question>> search({ |
| 16 | + required String siteId, |
| 17 | + required String query, |
| 18 | + bool? accepted, |
| 19 | + int? answers, |
| 20 | + String? body, |
| 21 | + bool? closed, |
| 22 | + bool? migrated, |
| 23 | + bool? notice, |
| 24 | + Set<String>? notTagged, |
| 25 | + Set<String>? tagged, |
| 26 | + String? title, |
| 27 | + int? ownerId, |
| 28 | + Uri? url, |
| 29 | + int? views, |
| 30 | + bool? wiki, |
| 31 | + DateTime? fromDate, |
| 32 | + DateTime? toDate, |
| 33 | + int page = 1, |
| 34 | + int pageSize = 100, |
| 35 | + Sort sort = Sort.relevance, |
| 36 | + Order order = Order.desc, |
| 37 | + }) => |
| 38 | + _search( |
| 39 | + { |
| 40 | + 'site': siteId, |
| 41 | + 'q': query, |
| 42 | + 'accepted': accepted, |
| 43 | + 'answers': answers, |
| 44 | + 'body': body, |
| 45 | + 'closed': closed, |
| 46 | + 'migrated': migrated, |
| 47 | + 'notice': notice, |
| 48 | + 'nottagged': notTagged?.join(';'), |
| 49 | + 'tagged': tagged?.join(';'), |
| 50 | + 'title': title, |
| 51 | + 'user': ownerId, |
| 52 | + 'url': url, |
| 53 | + 'views': views, |
| 54 | + 'wiki': wiki, |
| 55 | + 'fromdate': fromDate?.secondsSinceEpoch, |
| 56 | + 'todate': toDate?.secondsSinceEpoch, |
| 57 | + 'page': page, |
| 58 | + 'pagesize': pageSize, |
| 59 | + 'sort': sort, |
| 60 | + 'order': order, |
| 61 | + }..removeWhere((String key, dynamic value) => value == null), |
| 62 | + ); |
| 63 | + |
| 64 | + static Future<ItemList<Question>> _search(Map<String, dynamic> params) async { |
| 65 | + final Uri url = Uri.https( |
| 66 | + 'api.stackexchange.com', |
| 67 | + '${Env.stackExchangeApiVersion}/search/advanced', |
| 68 | + { |
| 69 | + ...params, |
| 70 | + 'key': Env.stackExchangeApiKey, |
| 71 | + 'client_id': Env.stackExchangeClientId, |
| 72 | + }.map( |
| 73 | + (String key, dynamic value) => MapEntry( |
| 74 | + key, |
| 75 | + value?.toString(), |
| 76 | + ), |
| 77 | + ), |
| 78 | + ); |
| 79 | + |
| 80 | + final http.Response response = await http.get(url); |
| 81 | + |
| 82 | + if (response.statusCode >= 200 && response.statusCode < 300) { |
| 83 | + if (response.body.isNotEmpty) { |
| 84 | + final Map<String, dynamic> json = jsonDecode(response.body); |
| 85 | + |
| 86 | + return json.containsKey('items') && json['items'] is Iterable |
| 87 | + ? ItemList<Question>( |
| 88 | + <Question>[ |
| 89 | + for (final item in json['items'] as List) |
| 90 | + Question.fromJson(item as Map<String, dynamic>), |
| 91 | + ], |
| 92 | + hasMore: json['has_more'] as bool? ?? false, |
| 93 | + quotaMax: json['quota_max'] as int? ?? 0, |
| 94 | + quotaRemaining: json['quota_remaining'] as int? ?? 0, |
| 95 | + ) |
| 96 | + : ItemList<Question>.empty(); |
| 97 | + } |
| 98 | + |
| 99 | + return ItemList<Question>.empty(); |
| 100 | + } else { |
| 101 | + if (response.body.isNotEmpty) { |
| 102 | + final Map<String, dynamic> json = jsonDecode(response.body); |
| 103 | + throw http.ClientException( |
| 104 | + json['error_message'] ?? 'Failed to load data', |
| 105 | + url, |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + throw http.ClientException('Failed to load data', url); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments