forked from mobxjs/mobx.dart
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
78468ca
commit e5fcc0f
Showing
10 changed files
with
114 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#! /usr/bin/env dart | ||
|
||
import 'dart:async'; | ||
import 'dart:convert'; | ||
|
||
import 'package:http/http.dart' as http; | ||
|
||
enum Type { newest, top } | ||
|
||
extension on Type { | ||
Uri get uri { | ||
switch (this) { | ||
case Type.newest: | ||
return Uri.parse( | ||
'https://hacker-news.firebaseio.com/v0/newstories.json'); | ||
|
||
case Type.top: | ||
return Uri.parse( | ||
'https://hacker-news.firebaseio.com/v0/topstories.json'); | ||
} | ||
} | ||
} | ||
|
||
class FeedItem { | ||
FeedItem( | ||
{required this.score, | ||
required this.title, | ||
this.url, | ||
required this.author}); | ||
|
||
final int score; | ||
final String title; | ||
final String? url; | ||
final String author; | ||
|
||
@override | ||
String toString() => | ||
'${this.author}: ${this.title}(${this.score}⭐️), ${this.url ?? 'no-url'}'; | ||
} | ||
|
||
class HNApi { | ||
Future<List<FeedItem>> newest() async { | ||
final ids = await _getItems(Type.newest); | ||
final items = await Future.wait(ids.map(_getItem)); | ||
|
||
return items.toList(growable: false); | ||
} | ||
|
||
Future<List<FeedItem>> top() async { | ||
final ids = await _getItems(Type.top); | ||
final items = await Future.wait(ids.map(_getItem)); | ||
|
||
return items.toList(growable: false); | ||
} | ||
|
||
Future<FeedItem> _getItem(int id) async { | ||
final response = await http | ||
.get(Uri.parse('https://hacker-news.firebaseio.com/v0/item/$id.json')); | ||
final json = jsonDecode(response.body) as Map<String, dynamic>; | ||
|
||
return FeedItem( | ||
score: json['score'], | ||
title: json['title'], | ||
author: json['by'], | ||
url: json['url']); | ||
} | ||
|
||
Future<List<int>> _getItems(Type type, {int count = 25}) async { | ||
final response = await http.get(type.uri); | ||
|
||
final ids = (jsonDecode(response.body) as List) | ||
.take(count) | ||
.map((e) => e as int) | ||
.toList(growable: false); | ||
|
||
return ids; | ||
} | ||
} | ||
|
||
Future<void> main() async { | ||
final list = await HNApi().top(); | ||
print(list); | ||
} |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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