Skip to content

Commit

Permalink
feat(pubky): rename PubkyClient to Client
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Nov 14, 2024
1 parent af390e8 commit 5972b48
Show file tree
Hide file tree
Showing 26 changed files with 106 additions and 105 deletions.
4 changes: 2 additions & 2 deletions examples/authn/signup.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()?;
Expand Down
8 changes: 4 additions & 4 deletions examples/authz/3rd-party-app/src/pubky-auth-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || ""
}
Expand All @@ -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())
Expand Down
6 changes: 3 additions & 3 deletions examples/authz/authenticator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use clap::Parser;
use pubky::PubkyClient;
use pubky::Client;
use std::path::PathBuf;
use url::Url;

Expand Down Expand Up @@ -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.
Expand All @@ -78,7 +78,7 @@ async fn main() -> Result<()> {

client
} else {
PubkyClient::new()?
Client::new()?
};

println!("Sending AuthToken to the 3rd party app...");
Expand Down
4 changes: 2 additions & 2 deletions examples/request/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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" => {
Expand Down
6 changes: 3 additions & 3 deletions pubky/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ 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 () {
// Mainline Dht testnet and a temporary homeserver for unit testing.
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();
Expand Down
18 changes: 9 additions & 9 deletions pubky/pkg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
Expand All @@ -60,11 +60,11 @@ await client.delete(url);

## API

### PubkyClient
### Client

#### constructor
```js
let client = new PubkyClient()
let client = new Client()
```

#### signup
Expand Down Expand Up @@ -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();
```
4 changes: 2 additions & 2 deletions pubky/pkg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions pubky/pkg/test/auth.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions pubky/pkg/test/http.js
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
16 changes: 8 additions & 8 deletions pubky/pkg/test/public.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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
{
Expand All @@ -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();
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion pubky/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
22 changes: 10 additions & 12 deletions pubky/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::Duration;

use pkarr::mainline::Testnet;

use crate::PubkyClient;
use crate::Client;

mod api;
mod internals;
Expand Down Expand Up @@ -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))
Expand All @@ -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()
}
Expand All @@ -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()
}
}
4 changes: 2 additions & 2 deletions pubky/src/native/api/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 5972b48

Please sign in to comment.