-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c8cea2
commit 23071fb
Showing
27 changed files
with
784 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!-- | ||
Developer's Certificate of Origin 1.1 | ||
By making a contribution to this project, I certify that: | ||
(a) The contribution was created in whole or in part by me and I | ||
have the right to submit it under the open source license | ||
indicated in the file; or | ||
(b) The contribution is based upon previous work that, to the best | ||
of my knowledge, is covered under an appropriate open source | ||
license and I have the right under that license to submit that | ||
work with modifications, whether created in whole or in part | ||
by me, under the same open source license (unless I am | ||
permitted to submit under a different license), as indicated | ||
in the file; or | ||
(c) The contribution was provided directly to me by some other | ||
person who certified (a), (b) or (c) and I have not modified | ||
it. | ||
(d) I understand and agree that this project and the contribution | ||
are public and that a record of the contribution (including all | ||
personal information I submit with it, including my sign-off) is | ||
maintained indefinitely and may be redistributed consistent with | ||
this project or the open source license(s) involved. | ||
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Please see the documentation for all configuration options: | ||
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates | ||
|
||
version: 2 | ||
updates: | ||
- package-ecosystem: 'cargo' | ||
directory: '/' | ||
schedule: | ||
interval: 'weekly' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
name: Continuous Integration | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: [main] | ||
paths: | ||
- "**" | ||
- "!/*.md" | ||
- "!/**.md" | ||
|
||
concurrency: | ||
group: "${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}" | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
ci: | ||
name: CI | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup Rust Toolchain | ||
uses: actions-rs/toolchain@v1 | ||
with: | ||
profile: minimal | ||
toolchain: stable | ||
|
||
- name: Setup Rust Cache | ||
uses: Swatinem/rust-cache@v2 | ||
|
||
- name: Check formatting | ||
run: cargo fmt --check | ||
|
||
- name: Check clippy | ||
run: cargo clippy --workspace -- -D warnings | ||
|
||
- name: Unit Tests | ||
run: cargo test -p matrix | ||
|
||
- name: Install Just | ||
uses: extractions/setup-just@v1 | ||
|
||
- name: Prepare Data for Tests | ||
run: | | ||
cp -R ./crates/test/fixtures/synapse ./docker/synapse | ||
docker compose up -d | ||
- name: E2E Tests | ||
run: cargo test -p test -- --test-threads=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: Dependabot auto-approve | ||
on: pull_request_target | ||
|
||
permissions: | ||
pull-requests: write | ||
|
||
jobs: | ||
dependabot: | ||
runs-on: ubuntu-latest | ||
if: ${{ github.actor == 'dependabot[bot]' }} | ||
steps: | ||
- name: Dependabot metadata | ||
id: metadata | ||
uses: dependabot/[email protected] | ||
with: | ||
github-token: "${{ secrets.GITHUB_TOKEN }}" | ||
- name: Approve a PR | ||
run: gh pr review --approve "$PR_URL" | ||
env: | ||
PR_URL: ${{github.event.pull_request.html_url}} | ||
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: Dependabot auto-merge | ||
on: pull_request_target | ||
|
||
permissions: | ||
pull-requests: write | ||
contents: write | ||
|
||
jobs: | ||
dependabot: | ||
runs-on: ubuntu-latest | ||
if: ${{ github.actor == 'dependabot[bot]' }} | ||
steps: | ||
- name: Dependabot metadata | ||
id: metadata | ||
uses: dependabot/[email protected] | ||
with: | ||
github-token: "${{ secrets.GITHUB_TOKEN }}" | ||
- name: Enable auto-merge for Dependabot PRs | ||
if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}} | ||
run: gh pr merge --auto --squash "$PR_URL" | ||
env: | ||
PR_URL: ${{github.event.pull_request.html_url}} | ||
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[workspace] | ||
members = [ | ||
"crates/matrix", | ||
"crates/test" | ||
] | ||
resolver = "1" | ||
|
||
[workspace.dependencies] | ||
serde = "1.0.192" | ||
url = { version = "2.4.1", features = ["serde"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "matrix" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
anyhow = "1.0.75" | ||
async-trait = "0.1.74" | ||
hex = "0.4.3" | ||
hmac = "0.12.1" | ||
matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk.git", rev = "e43a25a" } | ||
reqwest = { version = "0.11.22", features = ["json"] } | ||
serde_path_to_error = "0.1.14" | ||
serde_qs = "0.12.0" | ||
sha1 = "0.10.6" | ||
|
||
# Workspace Dependencies | ||
serde = { workspace = true } | ||
url = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
use std::ops::Deref; | ||
use std::str::from_utf8; | ||
|
||
use anyhow::{bail, Result}; | ||
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION}; | ||
use reqwest::{Client as HttpClient, Response}; | ||
use serde::Serialize; | ||
use url::Url; | ||
|
||
pub struct Client { | ||
client: HttpClient, | ||
base_url: Url, | ||
token: Option<String>, | ||
} | ||
|
||
impl Client { | ||
pub fn new<S: AsRef<str>>(url: S) -> Result<Self> { | ||
let url = Url::parse(url.as_ref())?; | ||
|
||
Ok(Self { | ||
client: HttpClient::new(), | ||
base_url: url, | ||
token: None, | ||
}) | ||
} | ||
|
||
/// Sets the token to be used for authentication with the server. | ||
pub fn set_token(&mut self, token: impl Into<String>) -> Result<()> { | ||
let token = token.into(); | ||
|
||
if token.is_empty() { | ||
self.token = None; | ||
bail!("Token cannot be empty"); | ||
} | ||
|
||
self.token = Some(token); | ||
Ok(()) | ||
} | ||
|
||
pub async fn get(&self, path: impl AsRef<str>) -> Result<Response> { | ||
let url = self.build_url(path)?; | ||
let headers = self.build_headers()?; | ||
let response = self.client.get(url).headers(headers).send().await?; | ||
|
||
Ok(response) | ||
} | ||
|
||
pub async fn get_query( | ||
&self, | ||
path: impl AsRef<str>, | ||
params: impl Serialize, | ||
) -> Result<Response> { | ||
let url = self.build_url_with_params(path, params)?; | ||
let headers = self.build_headers()?; | ||
let response = self.client.get(url).headers(headers).send().await?; | ||
|
||
Ok(response) | ||
} | ||
|
||
pub async fn post_json<T>(&self, path: impl AsRef<str>, body: &T) -> Result<Response> | ||
where | ||
T: Serialize, | ||
{ | ||
let url = self.build_url(path)?; | ||
let headers = self.build_headers()?; | ||
let resp = self | ||
.client | ||
.post(url) | ||
.json(body) | ||
.headers(headers) | ||
.send() | ||
.await?; | ||
|
||
Ok(resp) | ||
} | ||
|
||
pub async fn put_json<T>(&self, path: impl AsRef<str>, body: &T) -> Result<Response> | ||
where | ||
T: Serialize, | ||
{ | ||
let url = self.build_url(path)?; | ||
let headers = self.build_headers()?; | ||
let resp = self | ||
.client | ||
.put(url) | ||
.json(body) | ||
.headers(headers) | ||
.send() | ||
.await?; | ||
|
||
Ok(resp) | ||
} | ||
|
||
fn build_headers(&self) -> Result<HeaderMap> { | ||
let mut headers = HeaderMap::new(); | ||
|
||
if let Some(token) = &self.token { | ||
headers.insert( | ||
AUTHORIZATION, | ||
HeaderValue::from_str(&format!("Bearer {}", token))?, | ||
); | ||
} | ||
|
||
Ok(headers) | ||
} | ||
|
||
#[inline] | ||
fn build_url(&self, path: impl AsRef<str>) -> Result<Url> { | ||
let mut next = self.base_url.clone(); | ||
|
||
next.set_path(path.as_ref()); | ||
|
||
Ok(next) | ||
} | ||
|
||
fn build_url_with_params(&self, path: impl AsRef<str>, params: impl Serialize) -> Result<Url> { | ||
let mut url = self.build_url(path)?; | ||
let mut buff = Vec::new(); | ||
let qs_ser = &mut serde_qs::Serializer::new(&mut buff); | ||
|
||
serde_path_to_error::serialize(¶ms, qs_ser)?; | ||
|
||
let params = from_utf8(buff.as_slice())?.to_string(); | ||
|
||
url.set_query(Some(¶ms)); | ||
|
||
Ok(url) | ||
} | ||
} | ||
|
||
impl Deref for Client { | ||
type Target = HttpClient; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.client | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod client; | ||
pub mod resources; | ||
|
||
pub use client::Client; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod token; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod shared_secret; |
Oops, something went wrong.