feat(pubky): rename PubkyClient to Client

This commit is contained in:
nazeh
2024-11-14 15:30:01 +03:00
parent af390e8087
commit 5972b48316
26 changed files with 106 additions and 105 deletions

View File

@@ -1,6 +1,6 @@
use anyhow::Result;
use clap::Parser;
use pubky::PubkyClient;
use pubky::Client;
use std::path::PathBuf;
use pubky_common::crypto::PublicKey;
@@ -24,7 +24,7 @@ async fn main() -> Result<()> {
let homeserver = cli.homeserver;
let client = PubkyClient::new()?;
let client = Client::new()?;
println!("Enter your recovery_file's passphrase to signup:");
let passphrase = rpassword::read_password()?;

View File

@@ -56,8 +56,8 @@ export class PubkyAuthWidget extends LitElement {
this.testnet = false;
this.open = false;
/** @type {import("@synonymdev/pubky").PubkyClient} */
this.pubkyClient = new window.pubky.PubkyClient();
/** @type {import("@synonymdev/pubky").Client} */
this.pubkyClient = new window.pubky.Client();
this.caps = this.caps || ""
}
@@ -74,9 +74,9 @@ export class PubkyAuthWidget extends LitElement {
console.debug("Switching testnet");
if (this.testnet) {
this.pubkyClient = window.pubky.PubkyClient.testnet()
this.pubkyClient = window.pubky.Client.testnet()
} else {
this.pubkyClient = new window.pubky.PubkyClient();
this.pubkyClient = new window.pubky.Client();
}
console.debug("Pkarr Relays: " + this.pubkyClient.getPkarrRelays())

View File

@@ -1,6 +1,6 @@
use anyhow::Result;
use clap::Parser;
use pubky::PubkyClient;
use pubky::Client;
use std::path::PathBuf;
use url::Url;
@@ -66,7 +66,7 @@ async fn main() -> Result<()> {
println!("PublicKey: {}", keypair.public_key());
let client = if cli.testnet.unwrap_or_default() {
let client = PubkyClient::testnet()?;
let client = Client::testnet()?;
// For the purposes of this demo, we need to make sure
// the user has an account on the local homeserver.
@@ -78,7 +78,7 @@ async fn main() -> Result<()> {
client
} else {
PubkyClient::new()?
Client::new()?
};
println!("Sending AuthToken to the 3rd party app...");

View File

@@ -3,7 +3,7 @@ use clap::Parser;
use reqwest::Method;
use url::Url;
use pubky::PubkyClient;
use pubky::Client;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
@@ -18,7 +18,7 @@ struct Cli {
async fn main() -> Result<()> {
let cli = Cli::parse();
let client = PubkyClient::new()?;
let client = Client::new()?;
match cli.url.scheme() {
"https" => {

View File

@@ -8,7 +8,7 @@ Rust implementation implementation of [Pubky](https://github.com/pubky/pubky-cor
use pkarr::mainline::Testnet;
use pkarr::Keypair;
use pubky_homeserver::Homeserver;
use pubky::PubkyClient;
use pubky::Client;
#[tokio::main]
async fn main () {
@@ -16,10 +16,10 @@ async fn main () {
let testnet = Testnet::new(10);
let server = Homeserver::start_test(&testnet).await.unwrap();
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
// Uncomment the following line instead if you are not just testing.
// let client PubkyClient::new().unwrap();
// let client Client::new().unwrap();
// Generate a keypair
let keypair = Keypair::random();

View File

@@ -21,10 +21,10 @@ For Nodejs, you need Node v20 or later.
## Getting started
```js
import { PubkyClient, Keypair, PublicKey } from '../index.js'
import { Client, Keypair, PublicKey } from '../index.js'
// Initialize PubkyClient with Pkarr relay(s).
let client = new PubkyClient();
// Initialize Client with Pkarr relay(s).
let client = new Client();
// Generate a keypair
let keypair = Keypair.random();
@@ -49,7 +49,7 @@ await client.put(url, body);
// GET public data without signup or signin
{
const client = new PubkyClient();
const client = new Client();
let response = await client.get(url);
}
@@ -60,11 +60,11 @@ await client.delete(url);
## API
### PubkyClient
### Client
#### constructor
```js
let client = new PubkyClient()
let client = new Client()
```
#### signup
@@ -257,10 +257,10 @@ Run the local testnet server
npm run testnet
```
Use the logged addresses as inputs to `PubkyClient`
Use the logged addresses as inputs to `Client`
```js
import { PubkyClient } from '../index.js'
import { Client } from '../index.js'
const client = PubkyClient().testnet();
const client = Client().testnet();
```

View File

@@ -2,11 +2,11 @@
"name": "@synonymdev/pubky",
"type": "module",
"description": "Pubky client",
"version": "0.2.1",
"version": "0.3.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/pubky/pubky"
"url": "git+https://github.com/pubky/pubky-core.git"
},
"scripts": {
"testnet": "cargo run -p pubky_homeserver -- --testnet",

View File

@@ -1,11 +1,11 @@
import test from 'tape'
import { PubkyClient, Keypair, PublicKey } from '../index.cjs'
import { Client, Keypair, PublicKey } from '../index.cjs'
const Homeserver = PublicKey.from('8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo')
test('auth', async (t) => {
const client = PubkyClient.testnet();
const client = Client.testnet();
const keypair = Keypair.random()
const publicKey = keypair.publicKey()
@@ -36,7 +36,7 @@ test("3rd party signin", async (t) => {
// Third party app side
let capabilities = "/pub/pubky.app/:rw,/pub/foo.bar/file:r";
let client = PubkyClient.testnet();
let client = Client.testnet();
let [pubkyauth_url, pubkyauthResponse] = client
.authRequest("https://demo.httprelay.io/link", capabilities);
@@ -49,7 +49,7 @@ test("3rd party signin", async (t) => {
// Authenticator side
{
let client = PubkyClient.testnet();
let client = Client.testnet();
await client.signup(keypair, Homeserver);

View File

@@ -1,13 +1,13 @@
import test from 'tape'
import { PubkyClient, Keypair, PublicKey } from '../index.cjs'
import { Client, Keypair, PublicKey } from '../index.cjs'
const TLD = '8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo';
// TODO: test HTTPs too somehow.
test.skip("basic fetch", async (t) => {
let client = PubkyClient.testnet();
let client = Client.testnet();
// Normal TLD
{

View File

@@ -1,11 +1,11 @@
import test from 'tape'
import { PubkyClient, Keypair, PublicKey } from '../index.cjs'
import { Client, Keypair, PublicKey } from '../index.cjs'
const Homeserver = PublicKey.from('8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo');
test('public: put/get', async (t) => {
const client = PubkyClient.testnet();
const client = Client.testnet();
const keypair = Keypair.random();
@@ -20,7 +20,7 @@ test('public: put/get', async (t) => {
// PUT public data, by authorized client
await client.put(url, body);
const otherClient = PubkyClient.testnet();
const otherClient = Client.testnet();
// GET public data without signup or signin
{
@@ -42,7 +42,7 @@ test('public: put/get', async (t) => {
})
test("not found", async (t) => {
const client = PubkyClient.testnet();
const client = Client.testnet();
const keypair = Keypair.random();
@@ -59,7 +59,7 @@ test("not found", async (t) => {
})
test("unauthorized", async (t) => {
const client = PubkyClient.testnet();
const client = Client.testnet();
const keypair = Keypair.random()
const publicKey = keypair.publicKey()
@@ -86,7 +86,7 @@ test("unauthorized", async (t) => {
})
test("forbidden", async (t) => {
const client = PubkyClient.testnet();
const client = Client.testnet();
const keypair = Keypair.random()
const publicKey = keypair.publicKey()
@@ -111,7 +111,7 @@ test("forbidden", async (t) => {
})
test("list", async (t) => {
const client = PubkyClient.testnet();
const client = Client.testnet();
const keypair = Keypair.random()
const publicKey = keypair.publicKey()
@@ -242,7 +242,7 @@ test("list", async (t) => {
})
test('list shallow', async (t) => {
const client = PubkyClient.testnet();
const client = Client.testnet();
const keypair = Keypair.random()
const publicKey = keypair.publicKey()

View File

@@ -20,7 +20,7 @@ pub use crate::shared::list_builder::ListBuilder;
/// A client for Pubky homeserver API, as well as generic HTTP requests to Pubky urls.
#[derive(Debug, Clone)]
#[wasm_bindgen]
pub struct PubkyClient {
pub struct Client {
http: reqwest::Client,
pub(crate) pkarr: pkarr::Client,
}

View File

@@ -2,7 +2,7 @@ use std::time::Duration;
use pkarr::mainline::Testnet;
use crate::PubkyClient;
use crate::Client;
mod api;
mod internals;
@@ -42,13 +42,11 @@ impl Settings {
self
}
/// Build [PubkyClient]
pub fn build(self) -> Result<PubkyClient, std::io::Error> {
// TODO: convert to Result<PubkyClient>
/// Build [Client]
pub fn build(self) -> Result<Client, std::io::Error> {
let pkarr = pkarr::Client::new(self.pkarr_settings)?;
Ok(PubkyClient {
Ok(Client {
http: reqwest::Client::builder()
.cookie_store(true)
// .dns_resolver(Arc::new(dns_resolver))
@@ -60,13 +58,13 @@ impl Settings {
}
}
impl PubkyClient {
/// Create a new [PubkyClient] with default [Settings]
impl Client {
/// Create a new [Client] with default [Settings]
pub fn new() -> Result<Self, std::io::Error> {
Self::builder().build()
}
/// Returns a builder to edit settings before creating [PubkyClient].
/// Returns a builder to edit settings before creating [Client].
pub fn builder() -> Settings {
Settings::default()
}
@@ -83,8 +81,8 @@ impl PubkyClient {
}
#[cfg(test)]
/// Alias to `PubkyClient::builder().testnet(testnet).build().unwrap()`
pub(crate) fn test(testnet: &Testnet) -> PubkyClient {
PubkyClient::builder().testnet(testnet).build().unwrap()
/// Alias to `pubky::Client::builder().testnet(testnet).build().unwrap()`
pub(crate) fn test(testnet: &Testnet) -> Client {
Client::builder().testnet(testnet).build().unwrap()
}
}

View File

@@ -8,9 +8,9 @@ use pkarr::PublicKey;
use pubky_common::capabilities::Capabilities;
use crate::error::{Error, Result};
use crate::PubkyClient;
use crate::Client;
impl PubkyClient {
impl Client {
/// Signup to a homeserver and update Pkarr accordingly.
///
/// The homeserver is a Pkarr domain name, where the TLD is a Pkarr public key

View File

@@ -1,8 +1,8 @@
use reqwest::{IntoUrl, Method, RequestBuilder};
use crate::PubkyClient;
use crate::Client;
impl PubkyClient {
impl Client {
/// Start building a `Request` with the `Method` and `Url`.
///
/// Returns a `RequestBuilder`, which will allow setting headers and
@@ -25,7 +25,7 @@ mod tests {
use pkarr::mainline::Testnet;
use pubky_homeserver::Homeserver;
use crate::*;
use crate::Client;
// #[tokio::test]
async fn http_get_pubky() {
@@ -33,7 +33,7 @@ mod tests {
let homeserver = Homeserver::start_test(&testnet).await.unwrap();
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
let url = format!("http://{}/", homeserver.public_key());
@@ -50,7 +50,7 @@ mod tests {
async fn http_get_icann() {
let testnet = Testnet::new(10).unwrap();
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
let url = format!("http://example.com/");

View File

@@ -1,9 +1,9 @@
use bytes::Bytes;
use url::Url;
use crate::{error::Result, shared::list_builder::ListBuilder, PubkyClient};
use crate::{error::Result, shared::list_builder::ListBuilder, Client};
impl PubkyClient {
impl Client {
/// Upload a small payload to a given path.
pub async fn put<T: TryInto<Url>>(&self, url: T, content: &[u8]) -> Result<()> {
self.inner_put(url, content).await

View File

@@ -3,9 +3,9 @@ use pubky_common::{
recovery_file::{create_recovery_file, decrypt_recovery_file},
};
use crate::{error::Result, PubkyClient};
use crate::{error::Result, Client};
impl PubkyClient {
impl Client {
/// Create a recovery file of the `keypair`, containing the secret key encrypted
/// using the `passphrase`.
pub fn create_recovery_file(keypair: &Keypair, passphrase: &str) -> Result<Vec<u8>> {

View File

@@ -1,9 +1,9 @@
use reqwest::RequestBuilder;
use url::Url;
use crate::PubkyClient;
use crate::Client;
impl PubkyClient {
impl Client {
// === HTTP ===
/// A wrapper around [reqwest::Client::request], with the same signature between native and wasm.

View File

@@ -14,12 +14,12 @@ use pubky_common::{
use crate::{
error::{Error, Result},
PubkyClient,
Client,
};
use super::pkarr::Endpoint;
impl PubkyClient {
impl Client {
/// Signup to a homeserver and update Pkarr accordingly.
///
/// The homeserver is a Pkarr domain name, where the TLD is a Pkarr public key
@@ -225,7 +225,7 @@ mod tests {
let testnet = Testnet::new(10).unwrap();
let server = Homeserver::start_test(&testnet).await.unwrap();
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
let keypair = Keypair::random();
@@ -272,14 +272,14 @@ mod tests {
// Third party app side
let capabilities: Capabilities =
"/pub/pubky.app/:rw,/pub/foo.bar/file:r".try_into().unwrap();
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
let (pubkyauth_url, pubkyauth_response) = client
.auth_request("https://demo.httprelay.io/link", &capabilities)
.unwrap();
// Authenticator side
{
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
client.signup(&keypair, &server.public_key()).await.unwrap();

View File

@@ -1,7 +1,7 @@
use reqwest::Method;
use url::Url;
use crate::{error::Result, PubkyClient};
use crate::{error::Result, Client};
/// Helper struct to edit Pubky homeserver's list API options before sending them.
#[derive(Debug)]
@@ -10,13 +10,13 @@ pub struct ListBuilder<'a> {
reverse: bool,
limit: Option<u16>,
cursor: Option<&'a str>,
client: &'a PubkyClient,
client: &'a Client,
shallow: bool,
}
impl<'a> ListBuilder<'a> {
/// Create a new List request builder
pub(crate) fn new(client: &'a PubkyClient, url: Url) -> Self {
pub(crate) fn new(client: &'a Client, url: Url) -> Self {
Self {
client,
url,

View File

@@ -7,12 +7,12 @@ use pkarr::{
use crate::{
error::{Error, Result},
PubkyClient,
Client,
};
const MAX_ENDPOINT_RESOLUTION_RECURSION: u8 = 3;
impl PubkyClient {
impl Client {
/// Publish the SVCB record for `_pubky.<public_key>`.
pub(crate) async fn publish_pubky_homeserver(
&self,
@@ -265,7 +265,7 @@ mod tests {
target = format!("pubky.{}", keypair.public_key())
}
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
let endpoint = client.resolve_endpoint(&target).await.unwrap();
@@ -300,7 +300,7 @@ mod tests {
pkarr_client.publish(&signed_packet).await.unwrap();
{
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
let pubky = Keypair::random();

View File

@@ -6,12 +6,12 @@ use url::Url;
use crate::{
error::{Error, Result},
PubkyClient,
Client,
};
use super::{list_builder::ListBuilder, pkarr::Endpoint};
impl PubkyClient {
impl Client {
pub(crate) async fn inner_put<T: TryInto<Url>>(&self, url: T, content: &[u8]) -> Result<()> {
let url = self.pubky_to_http(url).await?;
@@ -109,7 +109,7 @@ mod tests {
let testnet = Testnet::new(10).unwrap();
let server = Homeserver::start_test(&testnet).await.unwrap();
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
let keypair = Keypair::random();
@@ -136,7 +136,7 @@ mod tests {
let testnet = Testnet::new(10).unwrap();
let server = Homeserver::start_test(&testnet).await.unwrap();
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
let keypair = Keypair::random();
@@ -147,7 +147,7 @@ mod tests {
let url = format!("pubky://{public_key}/pub/foo.txt");
let url = url.as_str();
let other_client = PubkyClient::test(&testnet);
let other_client = Client::test(&testnet);
{
let other = Keypair::random();
@@ -202,7 +202,7 @@ mod tests {
let testnet = Testnet::new(10).unwrap();
let server = Homeserver::start_test(&testnet).await.unwrap();
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
let keypair = Keypair::random();
@@ -406,7 +406,7 @@ mod tests {
let testnet = Testnet::new(10).unwrap();
let server = Homeserver::start_test(&testnet).await.unwrap();
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
let keypair = Keypair::random();
@@ -617,7 +617,7 @@ mod tests {
let testnet = Testnet::new(10).unwrap();
let server = Homeserver::start_test(&testnet).await.unwrap();
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
let keypair = Keypair::random();
@@ -646,7 +646,7 @@ mod tests {
let feed_url = format!("http://localhost:{}/events/", server.port());
let feed_url = feed_url.as_str();
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
let cursor;
@@ -714,7 +714,7 @@ mod tests {
let testnet = Testnet::new(10).unwrap();
let server = Homeserver::start_test(&testnet).await.unwrap();
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
let keypair = Keypair::random();
@@ -730,7 +730,7 @@ mod tests {
let feed_url = format!("http://localhost:{}/events/", server.port());
let feed_url = feed_url.as_str();
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
{
let response = client
@@ -762,7 +762,7 @@ mod tests {
async fn dont_delete_shared_blobs() {
let testnet = Testnet::new(10).unwrap();
let homeserver = Homeserver::start_test(&testnet).await.unwrap();
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
let homeserver_pubky = homeserver.public_key();
@@ -818,7 +818,7 @@ mod tests {
let testnet = Testnet::new(10).unwrap();
let server = Homeserver::start_test(&testnet).await.unwrap();
let client = PubkyClient::test(&testnet);
let client = Client::test(&testnet);
let keypair = Keypair::random();

View File

@@ -1,12 +1,12 @@
use wasm_bindgen::prelude::*;
use crate::PubkyClient;
use crate::Client;
mod api;
mod internals;
mod wrappers;
impl Default for PubkyClient {
impl Default for Client {
fn default() -> Self {
Self::new()
}
@@ -15,9 +15,9 @@ impl Default for PubkyClient {
static TESTNET_RELAYS: [&str; 1] = ["http://localhost:15411/pkarr"];
#[wasm_bindgen]
impl PubkyClient {
impl Client {
#[wasm_bindgen(constructor)]
/// Create PubkyClient with default Settings including default relays
/// Create Client with default Settings including default relays
pub fn new() -> Self {
Self {
http: reqwest::Client::builder().build().unwrap(),

View File

@@ -2,8 +2,8 @@ use url::Url;
use pubky_common::capabilities::Capabilities;
use crate::Client;
use crate::Error;
use crate::PubkyClient;
use crate::wasm::wrappers::keys::{Keypair, PublicKey};
use crate::wasm::wrappers::session::Session;
@@ -11,7 +11,7 @@ use crate::wasm::wrappers::session::Session;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
impl PubkyClient {
impl Client {
/// Signup to a homeserver and update Pkarr accordingly.
///
/// The homeserver is a Pkarr domain name, where the TLD is a Pkarr public key

View File

@@ -5,12 +5,12 @@ use wasm_bindgen::prelude::*;
use reqwest::Url;
use crate::PubkyClient;
use crate::Client;
use super::super::internals::resolve;
#[wasm_bindgen]
impl PubkyClient {
impl Client {
#[wasm_bindgen]
pub async fn fetch(
&self,
@@ -18,16 +18,19 @@ impl PubkyClient {
init: &web_sys::RequestInit,
) -> Result<js_sys::Promise, JsValue> {
let mut url: Url = url.try_into().map_err(|err| {
JsValue::from_str(&format!("PubkyClient::fetch(): Invalid `url`; {:?}", err))
JsValue::from_str(&format!("pubky::Client::fetch(): Invalid `url`; {:?}", err))
})?;
resolve(&self.pkarr, &mut url)
.await
.map_err(|err| JsValue::from_str(&format!("PubkyClient::fetch(): {:?}", err)))?;
.map_err(|err| JsValue::from_str(&format!("pubky::Client::fetch(): {:?}", err)))?;
let js_req =
web_sys::Request::new_with_str_and_init(url.as_str(), init).map_err(|err| {
JsValue::from_str(&format!("PubkyClient::fetch(): Invalid `init`; {:?}", err))
JsValue::from_str(&format!(
"pubky::Client::fetch(): Invalid `init`; {:?}",
err
))
})?;
Ok(js_fetch(&js_req))

View File

@@ -2,10 +2,10 @@ use wasm_bindgen::prelude::*;
use js_sys::{Array, Uint8Array};
use crate::PubkyClient;
use crate::Client;
#[wasm_bindgen]
impl PubkyClient {
impl Client {
#[wasm_bindgen]
/// Upload a small payload to a given path.
pub async fn put(&self, url: &str, content: &[u8]) -> Result<(), JsValue> {

View File

@@ -3,7 +3,7 @@ use url::Url;
use pkarr::{EndpointResolver, PublicKey};
use crate::{error::Result, PubkyClient};
use crate::{error::Result, Client};
// TODO: remove expect
pub async fn resolve(pkarr: &pkarr::Client, url: &mut Url) -> Result<()> {
@@ -25,7 +25,7 @@ pub async fn resolve(pkarr: &pkarr::Client, url: &mut Url) -> Result<()> {
Ok(())
}
impl PubkyClient {
impl Client {
/// A wrapper around [reqwest::Client::request], with the same signature between native and wasm.
pub(crate) async fn inner_request(&self, method: Method, url: Url) -> RequestBuilder {
self.http.request(method, url).fetch_credentials_include()