Skip to content

Commit

Permalink
update new indexer endpoint (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyBuisset authored Nov 28, 2023
1 parent 76c376b commit 8639878
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
15 changes: 8 additions & 7 deletions api/src/infrastructure/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use crate::domain::services::indexer;
#[async_trait]
impl indexer::Service for http::Client {
async fn index_repo(&self, repo_id: GithubRepoId) -> Result<()> {
self.post::<serde_json::Value>(format!("repo/{repo_id}")).await?;
self.post::<serde_json::Value>(format!("repo/{repo_id}"), None).await?;
Ok(())
}

async fn index_user(&self, user_id: GithubUserId) -> Result<()> {
self.post::<serde_json::Value>(format!("user/{user_id}")).await?;
self.post::<serde_json::Value>(format!("user/{user_id}"), None).await?;
Ok(())
}

Expand All @@ -22,7 +22,7 @@ impl indexer::Service for http::Client {
repo_id: GithubRepoId,
issue_number: GithubIssueNumber,
) -> Result<()> {
self.post::<serde_json::Value>(format!("repo/{repo_id}/issue/{issue_number}"))
self.post::<serde_json::Value>(format!("repo/{repo_id}/issue/{issue_number}"), None)
.await?;
Ok(())
}
Expand All @@ -32,7 +32,7 @@ impl indexer::Service for http::Client {
repo_id: GithubRepoId,
pr_number: GithubPullRequestNumber,
) -> Result<()> {
self.post::<serde_json::Value>(format!("repo/{repo_id}/pull_request/{pr_number}"))
self.post::<serde_json::Value>(format!("repo/{repo_id}/pull_request/{pr_number}"), None)
.await?;
Ok(())
}
Expand All @@ -49,9 +49,10 @@ impl indexer::Service for http::Client {
}

let response: Response = self
.post(format!(
"repo/{repo_owner}/{repo_name}/pull_request/{pr_number}"
))
.post(
format!("repo/{repo_owner}/{repo_name}/pull_request/{pr_number}"),
None,
)
.await?;

Ok(response.id)
Expand Down
9 changes: 8 additions & 1 deletion api/src/infrastructure/new_indexer.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use anyhow::Result;
use domain::*;
use infrastructure::http;
use serde_json::json;

use crate::domain::services::new_indexer;

#[async_trait]
impl new_indexer::Service for http::Client {
async fn index_repo(&self, repo_id: GithubRepoId) -> Result<()> {
self.put(format!("api/v1/indexes/repos/{repo_id}")).await?;
self.post(
"api/v1/events/on-repo-link-changed".to_string(),
Some(json!({
"linkedRepoIds": [repo_id]
})),
)
.await?;
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"request": {
"urlPattern": "/new_indexer/api/v1/indexes/repos/[0-9]*",
"method": "PUT",
"urlPattern": "/new_indexer/api/v1/events/on-repo-link-changed",
"method": "POST",
"headers": {
"Authorization": {
"contains": "Bearer "
Expand Down
13 changes: 11 additions & 2 deletions common/infrastructure/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,17 @@ impl Client {
Ok(url)
}

pub async fn post<R: DeserializeOwned>(&self, path: String) -> Result<R> {
let response = self.client.post(self.url(path)?).send().await?.error_for_status()?;
pub async fn post<R: DeserializeOwned>(
&self,
path: String,
body: Option<serde_json::Value>,
) -> Result<R> {
let mut request = self.client.post(self.url(path)?);
if let Some(body) = body {
request = request.body(body.to_string());
}

let response = request.send().await?.error_for_status()?;
let json = response.json().await?;
Ok(json)
}
Expand Down

0 comments on commit 8639878

Please sign in to comment.