Skip to content

Commit

Permalink
wip(search): Model search repositories response
Browse files Browse the repository at this point in the history
  • Loading branch information
proudmuslim-dev committed Jun 15, 2022
1 parent a5e928b commit 378c885
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 12 deletions.
41 changes: 33 additions & 8 deletions github-rest/src/builders/search.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
use crate::{
builders::{builder, builder_nested_setters, builder_string_setters, Builder},
methods::SearchRepositoryBody,
methods::SearchRepositoriesBody,
model::search::RepoSearchResultItem,
GithubRestError, Requester,
};
use async_trait::async_trait;
use github_api_octocat::end_points::EndPoints;
use serde_json::Value;
use std::ops::Range;

builder!(SearchRepositoriesBuilder {
query: String,
body: SearchRepositoryBody
});
builder!(
/// * tags search
/// * get `/search/repositories`
/// * docs <https://docs.github.com/rest/reference/search#search-repositories>
///
/// Search repositories
/// Find repositories via various criteria. This method returns up to 100 results [per page](https://docs.github.com/rest/overview/resources-in-the-rest-api#pagination).
///
/// When searching for repositories, you can get text match metadata for the **name** and **description** fields when you pass the `text-match` media type. For more details about how to receive highlighted search results, see [Text match metadata](https://docs.github.com/rest/reference/search#text-match-metadata).
///
/// For example, if you want to search for popular Tetris repositories
/// written in assembly code, your query might look like this:
///
/// `q=tetris+language:assembly&sort=stars&order=desc`
///
/// This query searches for repositories with the word `tetris` in the name,
/// the description, or the README. The results are limited to repositories
/// where the primary language is assembly. The results are sorted by stars
/// in descending order, so that the most popular repositories appear first
/// in the search results.
SearchRepositoriesBuilder {
query: String,
body: SearchRepositoriesBody
}
);

builder_string_setters!(SearchRepositoriesBuilder { query });
builder_nested_setters!(SearchRepositoriesBuilder {
Expand All @@ -25,8 +47,7 @@ builder_nested_setters!(SearchRepositoriesBuilder {

#[async_trait]
impl Builder for SearchRepositoriesBuilder {
// TODO: Model response
type Response = Value;
type Response = RepoSearchResultItem;

async fn execute<T>(mut self, client: &T) -> Result<Self::Response, GithubRestError>
where
Expand All @@ -35,7 +56,11 @@ impl Builder for SearchRepositoriesBuilder {
self.query.push_str(self.body.into_query().as_str());

client
.req::<_, String, Value>(EndPoints::GetSearchRepositories(), Some(&[("q", self.query)]), None)
.req::<_, String, RepoSearchResultItem>(
EndPoints::GetSearchRepositories(),
Some(&[("q", self.query)]),
None,
)
.await
}
}
Expand Down
2 changes: 1 addition & 1 deletion github-rest/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Requester for DefaultRequester {
}
let txt = res.text().await?;

Ok(txt)
Ok(dbg!(txt))
}

async fn req<T, V, A: DeserializeOwned>(
Expand Down
12 changes: 10 additions & 2 deletions github-rest/src/methods/search.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
use crate::{methods::prelude::*, model::search::RepoSearchResultItem};
use std::ops::Range;

#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct SearchRepositoriesResponse {
pub total_count: usize,
pub incomplete_results: bool,
pub items: Vec<RepoSearchResultItem>,
}

#[derive(Debug, Default, Clone)]
pub struct SearchRepositoryBody {
pub struct SearchRepositoriesBody {
/// Size of repository in kilobytes.
pub size: Option<Range<usize>>,
pub followers: Option<Range<usize>>,
pub forks: Option<Range<usize>>,
pub stars: Option<Range<usize>>,
}

impl SearchRepositoryBody {
impl SearchRepositoriesBody {
pub(crate) fn into_query(self) -> String {
let mut ret = "".to_owned();

Expand Down
81 changes: 80 additions & 1 deletion github-rest/src/model/search.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::model::prelude::*;
use crate::model::{prelude::*, repositories::nested::SimpleLicense};

/// Descending is the default.
///
Expand All @@ -25,3 +25,82 @@ pub enum Sort {
UpdatedDesc,
UpdatedAsc,
}

// TODO: Verify this because the docs suck apparently
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct RepoSearchResultItem {
pub archive_url: Option<String>,
pub assignees_url: Option<String>,
pub blobs_url: String,
pub branches_url: String,
pub collaborators_url: String,
pub comments_url: String,
pub commits_url: String,
pub compare_url: String,
pub contents_url: String,
pub contributors_url: String,
pub deployments_url: String,
pub description: Option<String>,
pub downloads_url: String,
pub events_url: String,
pub fork: bool,
pub forks_url: String,
pub full_name: String,
pub git_commits_url: String,
pub git_refs_url: String,
pub git_tags_url: String,
pub hooks_url: String,
pub html_url: String,
pub id: usize,
pub node_id: String,
pub issue_comment_url: String,
pub issue_events_url: String,
pub issues_url: String,
pub keys_url: String,
pub labels_url: String,
pub languages_url: String,
pub merges_url: String,
pub milestones_url: String,
pub name: String,
pub notifications_url: String,
pub owner: String,
pub private: bool,
pub pulls_url: String,
pub releases_url: String,
pub stargazers_url: String,
pub statuses_url: String,
pub subscribers_url: String,
pub subscription_url: String,
pub tags_url: String,
pub teams_url: String,
pub trees_url: String,
pub url: String,
pub clone_url: String,
pub default_branch: String,
pub forks: usize,
pub forks_count: usize,
pub git_url: String,
pub has_downloads: bool,
pub has_issues: bool,
pub has_projects: bool,
pub has_wiki: bool,
pub has_pages: bool,
pub homepage: Option<String>,
pub language: Option<String>,
pub archived: bool,
pub disabled: bool,
pub mirror_url: String,
pub open_issues: usize,
pub open_issues_count: usize,
pub license: Option<SimpleLicense>,
pub pushed_at: String,
pub size: usize,
pub ssh_url: String,
pub stargazers_count: usize,
pub svn_url: String,
pub watchers: usize,
pub watchers_count: usize,
pub created_at: String,
pub updated_at: String,
pub score: usize,
}

0 comments on commit 378c885

Please sign in to comment.