Skip to content

Commit

Permalink
feat: adds /peers endpoint
Browse files Browse the repository at this point in the history
With this change a new API endpoint is added for accessing connected
peers. The API is similar to the /api/v0/swarm/peers endpoint however it
allows for multiple addresses and does not have to remain Kubo RPC
compatible.

The endpoint allows posting an address and therefore connecting to a new
peer.

Fixes: #608
  • Loading branch information
nathanielc committed Dec 6, 2024
1 parent baea692 commit e3e587d
Show file tree
Hide file tree
Showing 23 changed files with 1,583 additions and 37 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ sha3 = "0.10"
smallvec = "1.10"
# pragma optimize hangs forver on 0.8, possibly due to libsqlite-sys upgrade
sqlx = { version = "0.7", features = ["sqlite", "runtime-tokio", "chrono"] }
ssh-key = { version = "0.5.1", default-features = false }
ssh-key = { version = "0.5.1", default-features = false, features = [
"std",
"rand_core",
] }
ssi = { version = "0.7", features = ["ed25519"] }
swagger = { version = "6.1", features = [
"serdejson",
Expand Down
2 changes: 2 additions & 0 deletions api-server/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ docs/Interest.md
docs/InterestsGet.md
docs/InterestsGetInterestsInner.md
docs/NetworkInfo.md
docs/Peer.md
docs/Peers.md
docs/StreamState.md
docs/Version.md
docs/default_api.md
Expand Down
10 changes: 9 additions & 1 deletion api-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ To see how to make this your own, look here:
[README]((https://openapi-generator.tech))

- API version: 0.45.0
- Build date: 2024-11-25T18:44:34.261235426Z[Etc/UTC]
- Build date: 2024-12-06T08:58:05.827048302-07:00[America/Denver]



Expand Down Expand Up @@ -82,6 +82,9 @@ cargo run --example client InterestsSortKeySortValueOptions
cargo run --example client InterestsSortKeySortValuePost
cargo run --example client LivenessGet
cargo run --example client LivenessOptions
cargo run --example client PeersGet
cargo run --example client PeersOptions
cargo run --example client PeersPost
cargo run --example client StreamsStreamIdGet
cargo run --example client StreamsStreamIdOptions
cargo run --example client VersionGet
Expand Down Expand Up @@ -142,6 +145,9 @@ Method | HTTP request | Description
[****](docs/default_api.md#) | **POST** /interests/{sort_key}/{sort_value} | Register interest for a sort key
[****](docs/default_api.md#) | **GET** /liveness | Test the liveness of the Ceramic node
[****](docs/default_api.md#) | **OPTIONS** /liveness | cors
[****](docs/default_api.md#) | **GET** /peers | Get list of connected peers
[****](docs/default_api.md#) | **OPTIONS** /peers | cors
[****](docs/default_api.md#) | **POST** /peers | Connect to a peer
[****](docs/default_api.md#) | **GET** /streams/{stream_id} | Get stream state
[****](docs/default_api.md#) | **OPTIONS** /streams/{stream_id} | cors
[****](docs/default_api.md#) | **GET** /version | Get the version of the Ceramic node
Expand All @@ -162,6 +168,8 @@ Method | HTTP request | Description
- [InterestsGet](docs/InterestsGet.md)
- [InterestsGetInterestsInner](docs/InterestsGetInterestsInner.md)
- [NetworkInfo](docs/NetworkInfo.md)
- [Peer](docs/Peer.md)
- [Peers](docs/Peers.md)
- [StreamState](docs/StreamState.md)
- [Version](docs/Version.md)

Expand Down
105 changes: 105 additions & 0 deletions api-server/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,68 @@ paths:
"200":
description: cors
summary: cors
/peers:
get:
responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/Peers'
description: success
"500":
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
description: Internal server error
summary: Get list of connected peers
options:
parameters:
- description: "Multiaddress of peer to connect to, at least one address must\
\ contain the peer id."
explode: true
in: query
name: addresses
required: true
schema:
items:
type: string
type: array
style: form
responses:
"200":
description: cors
summary: cors
post:
parameters:
- description: "Multiaddress of peer to connect to, at least one address must\
\ contain the peer id."
explode: true
in: query
name: addresses
required: true
schema:
items:
type: string
type: array
style: form
responses:
"204":
description: success
"400":
content:
application/json:
schema:
$ref: '#/components/schemas/BadRequestResponse'
description: bad request
"500":
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
description: Internal server error
summary: Connect to a peer
components:
requestBodies:
EventData:
Expand Down Expand Up @@ -789,6 +851,49 @@ components:
- id
title: State of a Ceramic stream
type: object
Peers:
example:
peers:
- addresses:
- addresses
- addresses
id: id
- addresses:
- addresses
- addresses
id: id
properties:
peers:
items:
$ref: '#/components/schemas/Peer'
type: array
required:
- peers
title: List of Peers
type: object
Peer:
description: Information about a connected peer
example:
addresses:
- addresses
- addresses
id: id
properties:
id:
description: DID of peer
type: string
addresses:
description: "List of known multiaddress of peer, will always include the\
\ peer id"
items:
description: Multiaddress where peer may be dialed
type: string
type: array
required:
- addresses
- id
title: Information about a connected peer
type: object
_feed_resumeToken_get_200_response:
example:
resumeToken: resumeToken
Expand Down
11 changes: 11 additions & 0 deletions api-server/docs/Peer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Peer

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | **String** | DID of peer |
**addresses** | **Vec<String>** | List of known multiaddress of peer, will always include the peer id |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


10 changes: 10 additions & 0 deletions api-server/docs/Peers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Peers

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**peers** | [**Vec<models::Peer>**](Peer.md) | |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


75 changes: 75 additions & 0 deletions api-server/docs/default_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Method | HTTP request | Description
****](default_api.md#) | **POST** /interests/{sort_key}/{sort_value} | Register interest for a sort key
****](default_api.md#) | **GET** /liveness | Test the liveness of the Ceramic node
****](default_api.md#) | **OPTIONS** /liveness | cors
****](default_api.md#) | **GET** /peers | Get list of connected peers
****](default_api.md#) | **OPTIONS** /peers | cors
****](default_api.md#) | **POST** /peers | Connect to a peer
****](default_api.md#) | **GET** /streams/{stream_id} | Get stream state
****](default_api.md#) | **OPTIONS** /streams/{stream_id} | cors
****](default_api.md#) | **GET** /version | Get the version of the Ceramic node
Expand Down Expand Up @@ -601,6 +604,78 @@ No authorization required

[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)

# ****
> models::Peers ()
Get list of connected peers

### Required Parameters
This endpoint does not need any parameter.

### Return type

[**models::Peers**](Peers.md)

### Authorization

No authorization required

### HTTP request headers

- **Content-Type**: Not defined
- **Accept**: application/json

[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)

# ****
> (addresses)
cors

### Required Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**addresses** | [**String**](String.md)| Multiaddress of peer to connect to, at least one address must contain the peer id. |

### Return type

(empty response body)

### Authorization

No authorization required

### HTTP request headers

- **Content-Type**: Not defined
- **Accept**: Not defined

[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)

# ****
> (addresses)
Connect to a peer

### Required Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**addresses** | [**String**](String.md)| Multiaddress of peer to connect to, at least one address must contain the peer id. |

### Return type

(empty response body)

### Authorization

No authorization required

### HTTP request headers

- **Content-Type**: Not defined
- **Accept**: application/json

[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)

# ****
> models::StreamState (stream_id)
Get stream state
Expand Down
32 changes: 30 additions & 2 deletions api-server/examples/client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use ceramic_api_server::{
FeedEventsOptionsResponse, FeedResumeTokenGetResponse, FeedResumeTokenOptionsResponse,
InterestsOptionsResponse, InterestsPostResponse, InterestsSortKeySortValueOptionsResponse,
InterestsSortKeySortValuePostResponse, LivenessGetResponse, LivenessOptionsResponse,
StreamsStreamIdGetResponse, StreamsStreamIdOptionsResponse, VersionGetResponse,
VersionOptionsResponse, VersionPostResponse,
PeersGetResponse, PeersOptionsResponse, PeersPostResponse, StreamsStreamIdGetResponse,
StreamsStreamIdOptionsResponse, VersionGetResponse, VersionOptionsResponse,
VersionPostResponse,
};
use clap::{App, Arg};
#[allow(unused_imports)]
Expand Down Expand Up @@ -61,6 +62,9 @@ fn main() {
"InterestsSortKeySortValuePost",
"LivenessGet",
"LivenessOptions",
"PeersGet",
"PeersOptions",
"PeersPost",
"StreamsStreamIdGet",
"StreamsStreamIdOptions",
"VersionGet",
Expand Down Expand Up @@ -323,6 +327,30 @@ fn main() {
(client.context() as &dyn Has<XSpanIdString>).get().clone()
);
}
Some("PeersGet") => {
let result = rt.block_on(client.peers_get());
info!(
"{:?} (X-Span-ID: {:?})",
result,
(client.context() as &dyn Has<XSpanIdString>).get().clone()
);
}
Some("PeersOptions") => {
let result = rt.block_on(client.peers_options(&Vec::new()));
info!(
"{:?} (X-Span-ID: {:?})",
result,
(client.context() as &dyn Has<XSpanIdString>).get().clone()
);
}
Some("PeersPost") => {
let result = rt.block_on(client.peers_post(&Vec::new()));
info!(
"{:?} (X-Span-ID: {:?})",
result,
(client.context() as &dyn Has<XSpanIdString>).get().clone()
);
}
Some("StreamsStreamIdGet") => {
let result = rt.block_on(client.streams_stream_id_get("stream_id_example".to_string()));
info!(
Expand Down
Loading

0 comments on commit e3e587d

Please sign in to comment.