mastodonpp/docs/example09_nlohmann_json_8cp...

223 lines
16 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>mastodonpp: example09_nlohmann_json.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">mastodonpp
&#160;<span id="projectnumber">0.5.7</span>
</div>
<div id="projectbrief">C++ wrapper for the Mastodon and Pleroma APIs.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="headertitle">
<div class="title">example09_nlohmann_json.cpp</div> </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><span class="comment">/* This file is part of mastodonpp.</span></div>
<div class="line"><span class="comment"> * Copyright © 2020 tastytea &lt;tastytea@tastytea.de&gt;</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * Permission to use, copy, modify, and/or distribute this software for any</span></div>
<div class="line"><span class="comment"> * purpose with or without fee is hereby granted.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * THE SOFTWARE IS PROVIDED &quot;AS IS&quot; AND THE AUTHOR DISCLAIMS ALL WARRANTIES</span></div>
<div class="line"><span class="comment"> * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF</span></div>
<div class="line"><span class="comment"> * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY</span></div>
<div class="line"><span class="comment"> * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES</span></div>
<div class="line"><span class="comment"> * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION</span></div>
<div class="line"><span class="comment"> * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN</span></div>
<div class="line"><span class="comment"> * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// Get the last 4 public statuses of an instance and process them with</span></div>
<div class="line"><span class="comment">// nlohmann-json. &lt;https://github.com/nlohmann/json&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#if __has_include(&quot;mastodonpp.hpp&quot;</span>)</div>
<div class="line"><span class="preprocessor"># include &quot;mastodonpp.hpp&quot;</span> <span class="comment">// We&#39;re building mastodonpp.</span></div>
<div class="line"><span class="preprocessor">#else</span></div>
<div class="line"><span class="preprocessor"># include &lt;mastodonpp/mastodonpp.hpp&gt;</span> <span class="comment">// We&#39;re building outside mastodonpp.</span></div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// Don&#39;t compile if nlohmann-json can&#39;t be found.</span></div>
<div class="line"><span class="preprocessor">#if __has_include(&lt;nlohmann/json.hpp&gt;)</span></div>
<div class="line"><span class="preprocessor"># include &lt;nlohmann/json.hpp&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor"># include &lt;cstdlib&gt;</span></div>
<div class="line"><span class="preprocessor"># include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor"># include &lt;string&gt;</span></div>
<div class="line"><span class="preprocessor"># include &lt;string_view&gt;</span></div>
<div class="line"><span class="preprocessor"># include &lt;vector&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">namespace </span>masto = <a class="code" href="namespacemastodonpp.html">mastodonpp</a>;</div>
<div class="line"><span class="keyword">using</span> json = nlohmann::json;</div>
<div class="line"><span class="keyword">using</span> std::cerr;</div>
<div class="line"><span class="keyword">using</span> std::cout;</div>
<div class="line"><span class="keyword">using</span> std::exit;</div>
<div class="line"><span class="keyword">using</span> std::string_view;</div>
<div class="line"><span class="keyword">using</span> std::to_string;</div>
<div class="line"><span class="keyword">using</span> std::vector;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> handle_error(<span class="keyword">const</span> masto::answer_type &amp;answer);</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">const</span> vector&lt;string_view&gt; args(argv, argv + argc);</div>
<div class="line"> <span class="keywordflow">if</span> (args.size() &lt;= 1)</div>
<div class="line"> {</div>
<div class="line"> cerr &lt;&lt; <span class="stringliteral">&quot;Usage: &quot;</span> &lt;&lt; args[0] &lt;&lt; <span class="stringliteral">&quot; &lt;instance hostname&gt;\n&quot;</span>;</div>
<div class="line"> <span class="keywordflow">return</span> 1;</div>
<div class="line"> }</div>
<div class="line"> </div>
<div class="line"> <span class="keywordflow">try</span></div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// Initialize Instance and Connection.</span></div>
<div class="line"> masto::Instance instance{args[1], {}};</div>
<div class="line"> masto::Connection connection{instance};</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Get the last 4 public statuses of the instance.</span></div>
<div class="line"> <span class="keyword">auto</span> answer{connection.get(masto::API::v1::timelines_public,</div>
<div class="line"> {{<span class="stringliteral">&quot;limit&quot;</span>, <span class="stringliteral">&quot;4&quot;</span>}, {<span class="stringliteral">&quot;local&quot;</span>, <span class="stringliteral">&quot;true&quot;</span>}})};</div>
<div class="line"> <span class="keywordflow">if</span> (answer)</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// Parse JSON string.</span></div>
<div class="line"> <span class="keyword">auto</span> statuses{json::parse(answer.body)};</div>
<div class="line"> <span class="keywordflow">for</span> (<span class="keyword">const</span> <span class="keyword">auto</span> &amp;status : statuses)</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// Extract the info we want and print it.</span></div>
<div class="line"> <span class="keyword">const</span> <span class="keyword">auto</span> acct{status[<span class="stringliteral">&quot;account&quot;</span>][<span class="stringliteral">&quot;acct&quot;</span>].get&lt;string_view&gt;()};</div>
<div class="line"> <span class="keyword">const</span> <span class="keyword">auto</span> content{status[<span class="stringliteral">&quot;content&quot;</span>].get&lt;string_view&gt;()};</div>
<div class="line"> <span class="keyword">const</span> <span class="keyword">auto</span> <span class="keywordtype">id</span>{status[<span class="stringliteral">&quot;id&quot;</span>].get&lt;string_view&gt;()};</div>
<div class="line"> cout &lt;&lt; acct &lt;&lt; <span class="stringliteral">&quot; wrote status &quot;</span> &lt;&lt; <span class="keywordtype">id</span> &lt;&lt; <span class="stringliteral">&quot;: \n&quot;</span>;</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; &quot;</span> &lt;&lt; content.substr(0, 76) &lt;&lt; <span class="stringliteral">&quot; …\n&quot;</span>;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Print tags if there are any.</span></div>
<div class="line"> <span class="keyword">const</span> <span class="keyword">auto</span> tags{status[<span class="stringliteral">&quot;tags&quot;</span>]};</div>
<div class="line"> <span class="keywordflow">if</span> (!tags.empty())</div>
<div class="line"> {</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; Tags: &quot;</span>;</div>
<div class="line"> <span class="keywordflow">for</span> (<span class="keyword">const</span> <span class="keyword">auto</span> &amp;tag : tags)</div>
<div class="line"> {</div>
<div class="line"> cout &lt;&lt; <span class="charliteral">&#39;#&#39;</span> &lt;&lt; tag[<span class="stringliteral">&quot;name&quot;</span>].get&lt;string_view&gt;() &lt;&lt; <span class="stringliteral">&quot; &quot;</span>;</div>
<div class="line"> }</div>
<div class="line"> cout &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div>
<div class="line"> }</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Print the number of attachments.</span></div>
<div class="line"> <span class="keyword">const</span> <span class="keyword">auto</span> n_attachments{status[<span class="stringliteral">&quot;media_attachments&quot;</span>].size()};</div>
<div class="line"> <span class="keywordflow">if</span> (n_attachments &gt; 0)</div>
<div class="line"> {</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; &quot;</span> &lt;&lt; n_attachments &lt;&lt; <span class="stringliteral">&quot; attachment&quot;</span>;</div>
<div class="line"> <span class="keywordflow">if</span> (n_attachments &gt; 1)</div>
<div class="line"> {</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;s&quot;</span>;</div>
<div class="line"> }</div>
<div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;.\n&quot;</span>;</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">else</span></div>
<div class="line"> {</div>
<div class="line"> handle_error(answer);</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">catch</span> (<span class="keyword">const</span> masto::CURLException &amp;e)</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// Only libcurl errors that are not network errors will be thrown.</span></div>
<div class="line"> <span class="comment">// There went probably something wrong with the initialization.</span></div>
<div class="line"> cerr &lt;&lt; e.what() &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">catch</span> (<span class="keyword">const</span> nlohmann::detail::exception &amp;e)</div>
<div class="line"> {</div>
<div class="line"> cerr &lt;&lt; <span class="stringliteral">&quot;JSON exception: &quot;</span> &lt;&lt; e.what() &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div>
<div class="line"> }</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> handle_error(<span class="keyword">const</span> masto::answer_type &amp;answer)</div>
<div class="line">{</div>
<div class="line"> <span class="keywordflow">if</span> (answer.curl_error_code == 0)</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// If it is no libcurl error, it must be an HTTP error.</span></div>
<div class="line"> cerr &lt;&lt; <span class="stringliteral">&quot;HTTP status: &quot;</span> &lt;&lt; answer.http_status &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">else</span></div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// Network errors like “Couldn&#39;t resolve host.”.</span></div>
<div class="line"> cerr &lt;&lt; <span class="stringliteral">&quot;libcurl error &quot;</span> &lt;&lt; to_string(answer.curl_error_code) &lt;&lt; <span class="stringliteral">&quot;: &quot;</span></div>
<div class="line"> &lt;&lt; answer.error_message &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div>
<div class="line"> }</div>
<div class="line"> </div>
<div class="line"> exit(1);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#else</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor"># include &lt;iostream&gt;</span></div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line"> std::cout &lt;&lt; <span class="stringliteral">&quot;Example could not be compiled &quot;</span></div>
<div class="line"> <span class="stringliteral">&quot;because nlohmann-json was not found.\n&quot;</span>;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#endif </span><span class="comment">// __has_include(&lt;nlohmann/json.hpp&gt;)</span></div>
<div class="ttc" id="anamespacemastodonpp_html"><div class="ttname"><a href="namespacemastodonpp.html">mastodonpp</a></div><div class="ttdoc">C++ wrapper for the Mastodon API.</div><div class="ttdef"><b>Definition:</b> api.hpp:25</div></div>
</div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.1
</small></address>
</body>
</html>