<!DOCTYPE HTML> <html lang="" > <head> <meta charset="UTF-8"> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>config ยท hello world! bluesky</title> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="description" content=""> <meta name="generator" content="@gitbook-ng/gitbook 3.3.6"> <meta name="author" content="syui"> <link rel="stylesheet" href="../gitbook/style.css"> <link rel="stylesheet" href="../gitbook/gitbook-plugin-codeblock-filename/block.css"> <link rel="stylesheet" href="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css"> <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css"> <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css"> <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css"> <meta name="HandheldFriendly" content="true"/> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png"> <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon"> <link rel="next" href="2.html" /> <link rel="prev" href="0.html" /> </head> <body> <div class="book"> <div class="book-summary"> <div id="book-search-input" role="search"> <input type="text" placeholder="Type to search" /> </div> <nav role="navigation"> <ul class="summary"> <li class="chapter " data-level="1.1" data-path="../"> <a href="../"> hello world! bluesky </a> </li> <li class="chapter " data-level="1.2" data-path="../c1/"> <a href="../c1/"> part 1 </a> <ul class="articles"> <li class="chapter " data-level="1.2.1" data-path="../c1/1.html"> <a href="../c1/1.html"> quick start </a> </li> <li class="chapter " data-level="1.2.2" data-path="../c1/2.html"> <a href="../c1/2.html"> example </a> </li> </ul> </li> <li class="chapter " data-level="1.3" data-path="../c2/"> <a href="../c2/"> part 2 </a> <ul class="articles"> <li class="chapter " data-level="1.3.1" data-path="../c2/1.html"> <a href="../c2/1.html"> bluesky </a> </li> <li class="chapter " data-level="1.3.2" data-path="../c2/2.html"> <a href="../c2/2.html"> terminal </a> </li> <li class="chapter " data-level="1.3.3" data-path="../c2/3.html"> <a href="../c2/3.html"> shell </a> </li> <li class="chapter " data-level="1.3.4" data-path="../c2/4.html"> <a href="../c2/4.html"> rust </a> </li> </ul> </li> <li class="chapter " data-level="1.4" data-path="../c3/"> <a href="../c3/"> part 3 </a> <ul class="articles"> <li class="chapter " data-level="1.4.1" data-path="../c3/1.html"> <a href="../c3/1.html"> hello world </a> </li> <li class="chapter " data-level="1.4.2" data-path="../c3/2.html"> <a href="../c3/2.html"> seahorse </a> </li> <li class="chapter " data-level="1.4.3" data-path="../c3/3.html"> <a href="../c3/3.html"> reqwest </a> </li> </ul> </li> <li class="chapter " data-level="1.5" data-path="./"> <a href="./"> part 4 </a> <ul class="articles"> <li class="chapter " data-level="1.5.1" data-path="0.html"> <a href="0.html"> ai </a> </li> <li class="chapter active" data-level="1.5.2" data-path="1.html"> <a href="1.html"> config </a> </li> <li class="chapter " data-level="1.5.3" data-path="2.html"> <a href="2.html"> mention </a> </li> <li class="chapter " data-level="1.5.4" data-path="3.html"> <a href="3.html"> base64 </a> </li> </ul> </li> <li class="chapter " data-level="1.6" data-path="../end/"> <a href="../end/"> end </a> </li> </ul> </nav> </div> <div class="book-body"> <div class="body-inner"> <div class="book-header" role="navigation"> <!-- Title --> <h1> <i class="fa fa-circle-o-notch fa-spin"></i> <a href=".." >config</a> </h1> </div> <div class="page-wrapper" tabindex="-1" role="main"> <div class="page-inner"> <div id="book-search-results"> <div class="search-noresults"> <section class="normal markdown-section"> <h3 id="config">config</h3> <p>Add the code for bluesky's authentication system.</p> <p>Specifically, write information in <code>~/.config/ai/config.toml</code> and create a command option to put authentication information in <code>~/.config/ai/token.toml</code>.</p> <div><p class="code-filename">~/.config/ai/config.toml</p></div> <pre><code class="lang-toml"><span class="hljs-attr">handle</span> = <span class="hljs-string">"yui.syui.ai"</span> <span class="hljs-attr">pass</span> = <span class="hljs-string">"xxx"</span> <span class="hljs-attr">host</span> = <span class="hljs-string">"bsky.social"</span> </code></pre> <div><p class="code-filename">Cargo.toml</p></div> <pre><code class="lang-toml"><span class="hljs-section">[package]</span> <span class="hljs-attr">name</span> = <span class="hljs-string">"ai"</span> <span class="hljs-attr">version</span> = <span class="hljs-string">"0.1.0"</span> <span class="hljs-attr">edition</span> = <span class="hljs-string">"2021"</span> <span class="hljs-section"> [dependencies]</span> <span class="hljs-attr">seahorse</span> = <span class="hljs-string">"*"</span> <span class="hljs-attr">reqwest</span> = { version = <span class="hljs-string">"*"</span>, features = [<span class="hljs-string">"blocking"</span>, <span class="hljs-string">"json"</span>] } <span class="hljs-attr">tokio</span> = { version = <span class="hljs-string">"1"</span>, features = [<span class="hljs-string">"full"</span>] } <span class="hljs-attr">serde_derive</span> = <span class="hljs-string">"1.0"</span> <span class="hljs-attr">serde_json</span> = <span class="hljs-string">"1.0"</span> <span class="hljs-attr">serde</span> = <span class="hljs-string">"*"</span> <span class="hljs-attr">config</span> = { git = <span class="hljs-string">"https://github.com/mehcode/config-rs"</span>, branch = <span class="hljs-string">"master"</span> } <span class="hljs-attr">shellexpand</span> = <span class="hljs-string">"*"</span> <span class="hljs-attr">toml</span> = <span class="hljs-string">"*"</span> </code></pre> <div><p class="code-filename">src/data.rs</p></div> <pre><code class="lang-rust"><span class="hljs-keyword">use</span> config::{Config, ConfigError, File}; <span class="hljs-keyword">use</span> serde_derive::{Deserialize, Serialize}; <span class="hljs-meta">#[derive(Debug, Deserialize)]</span> <span class="hljs-meta">#[allow(unused)]</span> <span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Data</span></span> { <span class="hljs-keyword">pub</span> host: <span class="hljs-built_in">String</span>, <span class="hljs-keyword">pub</span> pass: <span class="hljs-built_in">String</span>, <span class="hljs-keyword">pub</span> handle: <span class="hljs-built_in">String</span>, } <span class="hljs-meta">#[derive(Serialize, Deserialize)]</span> <span class="hljs-meta">#[allow(non_snake_case)]</span> <span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Token</span></span> { <span class="hljs-keyword">pub</span> did: <span class="hljs-built_in">String</span>, <span class="hljs-keyword">pub</span> accessJwt: <span class="hljs-built_in">String</span>, <span class="hljs-keyword">pub</span> refreshJwt: <span class="hljs-built_in">String</span>, <span class="hljs-keyword">pub</span> handle: <span class="hljs-built_in">String</span>, } <span class="hljs-meta">#[derive(Serialize, Deserialize)]</span> <span class="hljs-meta">#[allow(non_snake_case)]</span> <span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Tokens</span></span> { <span class="hljs-keyword">pub</span> did: <span class="hljs-built_in">String</span>, <span class="hljs-keyword">pub</span> access: <span class="hljs-built_in">String</span>, <span class="hljs-keyword">pub</span> refresh: <span class="hljs-built_in">String</span>, <span class="hljs-keyword">pub</span> handle: <span class="hljs-built_in">String</span>, } <span class="hljs-keyword">impl</span> Data { <span class="hljs-keyword">pub</span> <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">new</span></span>() -> <span class="hljs-built_in">Result</span><<span class="hljs-keyword">Self</span>, ConfigError> { <span class="hljs-keyword">let</span> d = shellexpand::tilde(<span class="hljs-string">"~"</span>) + <span class="hljs-string">"/.config/ai/config.toml"</span>; <span class="hljs-keyword">let</span> s = Config::builder() .add_source(File::with_name(&d)) .add_source(config::Environment::with_prefix(<span class="hljs-string">"APP"</span>)) .build()?; s.try_deserialize() } } </code></pre> <div><p class="code-filename">src/main.rs</p></div> <pre><code class="lang-rust"><span class="hljs-keyword">pub</span> <span class="hljs-keyword">mod</span> data; <span class="hljs-keyword">use</span> seahorse::{App, Context, Command}; <span class="hljs-keyword">use</span> std::env; <span class="hljs-keyword">use</span> std::fs; <span class="hljs-keyword">use</span> std::io::Write; <span class="hljs-keyword">use</span> std::collections::HashMap; <span class="hljs-keyword">use</span> data::Data <span class="hljs-keyword">as</span> Datas; <span class="hljs-keyword">use</span> crate::data::Token; <span class="hljs-keyword">use</span> crate::data::Tokens; <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>() { <span class="hljs-keyword">let</span> args: <span class="hljs-built_in">Vec</span><<span class="hljs-built_in">String</span>> = env::args().collect(); <span class="hljs-keyword">let</span> app = App::new(<span class="hljs-built_in">env!</span>(<span class="hljs-string">"CARGO_PKG_NAME"</span>)) <span class="hljs-comment">//.action(c_ascii_art)</span> .command( Command::new(<span class="hljs-string">"bluesky"</span>) .alias(<span class="hljs-string">"b"</span>) .action(c_list_records), ) .command( Command::new(<span class="hljs-string">"login"</span>) .alias(<span class="hljs-string">"l"</span>) .action(c_access_token), ) ; app.run(args); } <span class="hljs-meta">#[tokio::main]</span> async <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">list_records</span></span>() -> reqwest::<span class="hljs-built_in">Result</span><()> { <span class="hljs-keyword">let</span> client = reqwest::Client::new(); <span class="hljs-keyword">let</span> handle= <span class="hljs-string">"support.bsky.team"</span>; <span class="hljs-keyword">let</span> col = <span class="hljs-string">"app.bsky.feed.post"</span>; <span class="hljs-keyword">let</span> body = client.get(<span class="hljs-string">"https://bsky.social/xrpc/com.atproto.repo.listRecords"</span>) .query(&[(<span class="hljs-string">"repo"</span>, &handle),(<span class="hljs-string">"collection"</span>, &col),(<span class="hljs-string">"limit"</span>, &<span class="hljs-string">"1"</span>),(<span class="hljs-string">"revert"</span>, &<span class="hljs-string">"true"</span>)]) .send() .await? .text() .await?; <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, body); <span class="hljs-literal">Ok</span>(()) } <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">c_list_records</span></span>(_c: &Context) { list_records().unwrap(); } <span class="hljs-meta">#[tokio::main]</span> async <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">access_token</span></span>() -> reqwest::<span class="hljs-built_in">Result</span><()> { <span class="hljs-keyword">let</span> file = <span class="hljs-string">"/.config/ai/token.toml"</span>; <span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> f = shellexpand::tilde(<span class="hljs-string">"~"</span>).to_string(); f.push_str(&file); <span class="hljs-keyword">let</span> data = Datas::new().unwrap(); <span class="hljs-keyword">let</span> data = Datas { host: data.host, handle: data.handle, pass: data.pass, }; <span class="hljs-keyword">let</span> url = <span class="hljs-string">"https://"</span>.to_owned() + &data.host + &<span class="hljs-string">"/xrpc/com.atproto.server.createSession"</span>; <span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> map = HashMap::new(); map.insert(<span class="hljs-string">"identifier"</span>, &data.handle); map.insert(<span class="hljs-string">"password"</span>, &data.pass); <span class="hljs-keyword">let</span> client = reqwest::Client::new(); <span class="hljs-keyword">let</span> res = client .post(url) .json(&map) .send() .await? .text() .await?; <span class="hljs-keyword">let</span> json: Token = serde_json::from_str(&res).unwrap(); <span class="hljs-keyword">let</span> tokens = Tokens { did: json.did.to_string(), access: json.accessJwt.to_string(), refresh: json.refreshJwt.to_string(), handle: json.handle.to_string(), }; <span class="hljs-keyword">let</span> toml = toml::to_string(&tokens).unwrap(); <span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> f = fs::File::create(f.clone()).unwrap(); f.write_all(&toml.as_bytes()).unwrap(); <span class="hljs-literal">Ok</span>(()) } <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">c_access_token</span></span>(_c: &Context) { access_token().unwrap(); } </code></pre> </section> </div> <div class="search-results"> <div class="has-results"> <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1> <ul class="search-results-list"></ul> </div> <div class="no-results"> <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1> </div> </div> </div> </div> </div> </div> <a href="0.html" class="navigation navigation-prev " aria-label="Previous page: ai"> <i class="fa fa-angle-left"></i> </a> <a href="2.html" class="navigation navigation-next " aria-label="Next page: mention"> <i class="fa fa-angle-right"></i> </a> </div> <script> var gitbook = gitbook || []; gitbook.push(function() { gitbook.page.hasChanged({"page":{"title":"config","level":"1.5.2","depth":2,"next":{"title":"mention","level":"1.5.3","depth":2,"path":"c4/2.md","ref":"c4/2.md","articles":[]},"previous":{"title":"ai","level":"1.5.1","depth":2,"path":"c4/0.md","ref":"c4/0.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","codeblock-filename","mermaid-gb3","diff"],"root":"./","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"codeblock-filename":{},"mermaid-gb3":{},"diff":{"type":"markdown","method":"diffChars","options":{}},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"syui","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"hello world! bluesky","gitbook":"*","description":"This is a bluesky ai-card example, and first rust"},"file":{"path":"c4/1.md","mtime":"2023-07-31T07:03:50.324Z","type":"markdown"},"gitbook":{"version":"3.3.6","time":"2023-07-31T07:16:09.509Z"},"basePath":"..","book":{"language":""}}); }); </script> </div> <script src="../gitbook/gitbook.js"></script> <script src="../gitbook/theme.js"></script> <script src="../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script> <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script> <script src="../gitbook/gitbook-plugin-search/search.js"></script> <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script> <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script> <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script> <script src="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script> </body> </html>