MCPcopy Create free account
hub / github.com/AshampooSystems/boden / fetchPosts

Method fetchPosts

examples/reddit/src/reddit.cpp:34–63  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32{
33 public:
34 void fetchPosts(const std::function<void()> &doneHandler)
35 {
36 net::http::request(
37 {bdn::net::http::Method::GET, "https://www.reddit.com/hot.json?limit=100", [this, doneHandler](auto r) {
38 try {
39 json j = json::parse(r->data);
40
41 for (auto child : j["data"]["children"]) {
42 auto post = std::make_shared<RedditPost>();
43 post->title = child["data"]["title"];
44 post->url = child["data"]["url"];
45
46 std::string thumbnail = child["data"]["thumbnail"];
47
48 if (cpp20::starts_with(thumbnail, "https://")) {
49 post->thumbnailUrl = thumbnail;
50 } else {
51 post->thumbnailUrl =
52 "https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-180x180.png";
53 }
54 posts.push_back(post);
55 }
56
57 doneHandler();
58 }
59 catch (const json::parse_error &parseError) {
60 bdn::logstream() << "Error parsing json: " << parseError.what();
61 }
62 }});
63 }
64
65 std::vector<std::shared_ptr<RedditPost>> posts;
66};

Callers 1

updatePostsMethod · 0.80

Calls 3

requestFunction · 0.85
starts_withFunction · 0.85
logstreamClass · 0.85

Tested by

no test coverage detected