Ts a Mistake
This commit is contained in:
11 files changed
+22292
No files matched your search
+14
@@ -0,0 +1,14 @@
|
||||
pub fn add(left: u64, right: u64) -> u64 {
|
||||
left + right
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = add(2, 2);
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
use anyhow::Context;
|
||||
use std::path::Path;
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let codex_auth_file = std::env::home_dir()
|
||||
.context("failed to get home directory")?
|
||||
.join(".codex")
|
||||
.join("auth.json");
|
||||
let (access_token, account_id) =
|
||||
read_tokens(&codex_auth_file).context("failed to read tokens")?;
|
||||
fetch_account_info(&access_token, &account_id).context("failed to fetch account info")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_tokens(p: impl AsRef<Path>) -> anyhow::Result<(String, String)> {
|
||||
let auth = serde_json::from_str::<codexport::AuthDotJson>(
|
||||
&std::fs::read_to_string(p).context("failed to read auth file")?,
|
||||
)
|
||||
.context("failed to parse auth file as JSON")?;
|
||||
let access_token = auth
|
||||
.tokens
|
||||
.as_ref()
|
||||
.context("missing tokens field in auth.json")?
|
||||
.access_token
|
||||
.clone();
|
||||
let account_id = auth
|
||||
.tokens
|
||||
.as_ref()
|
||||
.context("missing tokens field in auth.json")?
|
||||
.account_id
|
||||
.clone()
|
||||
.context("missing account_id field in auth.json")?;
|
||||
Ok((access_token, account_id))
|
||||
}
|
||||
|
||||
fn fetch_account_info(access_token: &str, account_id: &str) -> anyhow::Result<()> {
|
||||
let client = reqwest::blocking::Client::new();
|
||||
let response = client
|
||||
.get("https://chatgpt.com/backend-api/wham/usage") // only for chatgpt accounts which is MY account. may tap into the real codex-cli for endpoint stability.
|
||||
.header("Authorization", format!("Bearer {}", access_token))
|
||||
// .header("ChatGPT-Account-Id", account_id) // so auth is enough to get the info, no need for account id
|
||||
.send()?;
|
||||
let json = response
|
||||
.json::<serde_json::Value>()
|
||||
.context("doesnt look like a json response")?;
|
||||
println!("info: {:#?}", json); // holy shit this works
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user