Commit 786b4269 authored by Ben Boeckel's avatar Ben Boeckel
Browse files

gitlab: remove leading underscores from method names

parent ea9eded9
......@@ -79,18 +79,18 @@ impl Gitlab {
///
/// Errors out if `token` is invalid.
pub fn new<T: ToString>(host: &str, token: T) -> Result<Self> {
Self::_new("https", host, token.to_string())
Self::new_impl("https", host, token.to_string())
}
/// Create a new non-SSL Gitlab API representation.
///
/// Errors out if `token` is invalid.
pub fn new_insecure<T: ToString>(host: &str, token: T) -> Result<Self> {
Self::_new("http", host, token.to_string())
Self::new_impl("http", host, token.to_string())
}
/// Internal method to create a new Gitlab client.
fn _new(protocol: &str, host: &str, token: String) -> Result<Self> {
fn new_impl(protocol: &str, host: &str, token: String) -> Result<Self> {
let base_url = Url::parse(&format!("{}://{}/api/v4/", protocol, host))
.chain_err(|| ErrorKind::UrlParse)?;
......@@ -108,55 +108,55 @@ impl Gitlab {
/// The user the API is acting as.
pub fn current_user(&self) -> Result<UserPublic> {
self._get("user")
self.get("user")
}
/// Get all user accounts
pub fn users<T: UserResult>(&self) -> Result<Vec<T>> {
self._get_paged("users")
self.get_paged("users")
}
/// Find a user by id.
pub fn user<T: UserResult>(&self, user: UserId) -> Result<T> {
self._get(&format!("users/{}", user))
self.get(&format!("users/{}", user))
}
/// Find a user by username.
pub fn user_by_name<T: UserResult>(&self, name: &str) -> Result<T> {
let mut users = self._get_paged_with_param("users", &[("username", name)])?;
let mut users = self.get_paged_with_param("users", &[("username", name)])?;
users.pop()
.ok_or_else(|| Error::from_kind(ErrorKind::Gitlab("no such user".to_string())))
}
/// Get all accessible projects.
pub fn projects(&self) -> Result<Vec<Project>> {
self._get_paged("projects")
self.get_paged("projects")
}
/// Get all owned projects.
pub fn owned_projects(&self) -> Result<Vec<Project>> {
self._get_paged_with_param("projects", &[("owned", "true")])
self.get_paged_with_param("projects", &[("owned", "true")])
}
/// Find a project by id.
pub fn project(&self, project: ProjectId) -> Result<Project> {
self._get(&format!("projects/{}", project))
self.get(&format!("projects/{}", project))
}
/// Find a project by name.
pub fn project_by_name(&self, name: &str) -> Result<Project> {
self._get(&format!("projects/{}",
percent_encode(name.as_bytes(), PATH_SEGMENT_ENCODE_SET)))
self.get(&format!("projects/{}",
percent_encode(name.as_bytes(), PATH_SEGMENT_ENCODE_SET)))
}
/// Get a project's hooks.
pub fn hooks(&self, project: ProjectId) -> Result<Vec<ProjectHook>> {
self._get_paged(&format!("projects/{}/hooks", project))
self.get_paged(&format!("projects/{}/hooks", project))
}
/// Get a project hook.
pub fn hook(&self, project: ProjectId, hook: HookId) -> Result<ProjectHook> {
self._get(&format!("projects/{}/hooks/{}", project, hook))
self.get(&format!("projects/{}/hooks/{}", project, hook))
}
/// Convert a boolean parameter into an HTTP request value.
......@@ -184,27 +184,27 @@ impl Gitlab {
let mut flags = Self::event_flags(events);
flags.push(("url", url));
self._post_with_param(&format!("projects/{}/hooks", project), &flags)
self.post_with_param(&format!("projects/{}/hooks", project), &flags)
}
/// Get the team members of a group.
pub fn group_members(&self, group: GroupId) -> Result<Vec<Member>> {
self._get_paged(&format!("groups/{}/members", group))
self.get_paged(&format!("groups/{}/members", group))
}
/// Get a team member of a group.
pub fn group_member(&self, group: GroupId, user: UserId) -> Result<Member> {
self._get(&format!("groups/{}/members/{}", group, user))
self.get(&format!("groups/{}/members/{}", group, user))
}
/// Get the team members of a project.
pub fn project_members(&self, project: ProjectId) -> Result<Vec<Member>> {
self._get_paged(&format!("projects/{}/members", project))
self.get_paged(&format!("projects/{}/members", project))
}
/// Get a team member of a project.
pub fn project_member(&self, project: ProjectId, user: UserId) -> Result<Member> {
self._get(&format!("projects/{}/members/{}", project, user))
self.get(&format!("projects/{}/members/{}", project, user))
}
/// Add a user to a project.
......@@ -213,41 +213,41 @@ impl Gitlab {
let user_str = format!("{}", user);
let access_str = format!("{}", access);
self._post_with_param(&format!("projects/{}/members", project),
&[("user", &user_str), ("access", &access_str)])
self.post_with_param(&format!("projects/{}/members", project),
&[("user", &user_str), ("access", &access_str)])
}
/// Get branches for a project.
pub fn branches(&self, project: ProjectId) -> Result<Vec<RepoBranch>> {
self._get_paged(&format!("projects/{}/branches", project))
self.get_paged(&format!("projects/{}/branches", project))
}
/// Get a branch.
pub fn branch(&self, project: ProjectId, branch: &str) -> Result<RepoBranch> {
self._get(&format!("projects/{}/repository/branches/{}",
project,
percent_encode(branch.as_bytes(), PATH_SEGMENT_ENCODE_SET)))
self.get(&format!("projects/{}/repository/branches/{}",
project,
percent_encode(branch.as_bytes(), PATH_SEGMENT_ENCODE_SET)))
}
/// Get a commit.
pub fn commit(&self, project: ProjectId, commit: &str) -> Result<RepoCommitDetail> {
self._get(&format!("projects/{}/repository/commits/{}", project, commit))
self.get(&format!("projects/{}/repository/commits/{}", project, commit))
}
/// Get comments on a commit.
pub fn commit_comments(&self, project: ProjectId, commit: &str) -> Result<Vec<CommitNote>> {
self._get_paged(&format!("projects/{}/repository/commits/{}/comments",
project,
commit))
self.get_paged(&format!("projects/{}/repository/commits/{}/comments",
project,
commit))
}
/// Get comments on a commit.
pub fn create_commit_comment(&self, project: ProjectId, commit: &str, body: &str)
-> Result<CommitNote> {
self._post_with_param(&format!("projects/{}/repository/commits/{}/comment",
project,
commit),
&[("note", body)])
self.post_with_param(&format!("projects/{}/repository/commits/{}/comment",
project,
commit),
&[("note", body)])
}
/// Get comments on a commit.
......@@ -257,43 +257,43 @@ impl Gitlab {
let line_str = format!("{}", line);
let line_type = LineType::New;
self._post_with_param(&format!("projects/{}/repository/commits/{}/comment",
project,
commit),
&[("note", body),
("path", path),
("line", &line_str),
("line_type", line_type.as_str())])
self.post_with_param(&format!("projects/{}/repository/commits/{}/comment",
project,
commit),
&[("note", body),
("path", path),
("line", &line_str),
("line_type", line_type.as_str())])
}
/// Get the latest statuses of a commit.
pub fn commit_latest_statuses(&self, project: ProjectId, commit: &str)
-> Result<Vec<CommitStatus>> {
self._get_paged(&format!("projects/{}/repository/commits/{}/statuses",
project,
commit))
self.get_paged(&format!("projects/{}/repository/commits/{}/statuses",
project,
commit))
}
/// Get the all statuses of a commit.
pub fn commit_all_statuses(&self, project: ProjectId, commit: &str)
-> Result<Vec<CommitStatus>> {
self._get_paged_with_param(&format!("projects/{}/repository/commits/{}/statuses",
project,
commit),
&[("all", "true")])
self.get_paged_with_param(&format!("projects/{}/repository/commits/{}/statuses",
project,
commit),
&[("all", "true")])
}
/// Get the latest builds of a commit.
pub fn commit_latest_builds(&self, project: ProjectId, commit: &str) -> Result<Vec<Job>> {
self._get_paged(&format!("projects/{}/repository/commits/{}/builds", project, commit))
self.get_paged(&format!("projects/{}/repository/commits/{}/builds", project, commit))
}
/// Get the all builds of a commit.
pub fn commit_all_builds(&self, project: ProjectId, commit: &str) -> Result<Vec<Job>> {
self._get_paged_with_param(&format!("projects/{}/repository/commits/{}/builds",
project,
commit),
&[("all", "true")])
self.get_paged_with_param(&format!("projects/{}/repository/commits/{}/builds",
project,
commit),
&[("all", "true")])
}
/// Create a status message for a commit.
......@@ -309,22 +309,22 @@ impl Gitlab {
info.target_url.map(|v| params.push(("target_url", v)));
info.description.map(|v| params.push(("description", v)));
self._post_with_param(path, &params)
self.post_with_param(path, &params)
}
/// Get the issues for a project.
pub fn issues(&self, project: ProjectId) -> Result<Vec<Issue>> {
self._get_paged(&format!("projects/{}/issues", project))
self.get_paged(&format!("projects/{}/issues", project))
}
/// Get issues.
pub fn issue(&self, project: ProjectId, issue: IssueInternalId) -> Result<Issue> {
self._get(&format!("projects/{}/issues/{}", project, issue))
self.get(&format!("projects/{}/issues/{}", project, issue))
}
/// Get the notes from a issue.
pub fn issue_notes(&self, project: ProjectId, issue: IssueInternalId) -> Result<Vec<Note>> {
self._get_paged(&format!("projects/{}/issues/{}/notes", project, issue))
self.get_paged(&format!("projects/{}/issues/{}/notes", project, issue))
}
/// Create a note on a issue.
......@@ -332,41 +332,41 @@ impl Gitlab {
-> Result<Note> {
let path = &format!("projects/{}/issues/{}/notes", project, issue);
self._post_with_param(path, &[("body", content)])
self.post_with_param(path, &[("body", content)])
}
/// Get the merge requests for a project.
pub fn merge_requests(&self, project: ProjectId) -> Result<Vec<MergeRequest>> {
self._get_paged(&format!("projects/{}/merge_requests", project))
self.get_paged(&format!("projects/{}/merge_requests", project))
}
/// Get the merge requests with a given state.
pub fn merge_requests_with_state(&self, project: ProjectId, state: MergeRequestStateFilter)
-> Result<Vec<MergeRequest>> {
self._get_paged_with_param(&format!("projects/{}/merge_requests", project),
&[("state", state.as_str())])
self.get_paged_with_param(&format!("projects/{}/merge_requests", project),
&[("state", state.as_str())])
}
/// Get merge requests.
pub fn merge_request(&self, project: ProjectId, merge_request: MergeRequestInternalId)
-> Result<MergeRequest> {
self._get(&format!("projects/{}/merge_requests/{}", project, merge_request))
self.get(&format!("projects/{}/merge_requests/{}", project, merge_request))
}
/// Get the issues that will be closed when a merge request is merged.
pub fn merge_request_closes_issues(&self, project: ProjectId, merge_request: MergeRequestInternalId)
-> Result<Vec<IssueReference>> {
self._get_paged(&format!("projects/{}/merge_requests/{}/closes_issues",
project,
merge_request))
self.get_paged(&format!("projects/{}/merge_requests/{}/closes_issues",
project,
merge_request))
}
/// Get the notes from a merge request.
pub fn merge_request_notes(&self, project: ProjectId, merge_request: MergeRequestInternalId)
-> Result<Vec<Note>> {
self._get_paged(&format!("projects/{}/merge_requests/{}/notes",
project,
merge_request))
self.get_paged(&format!("projects/{}/merge_requests/{}/notes",
project,
merge_request))
}
/// Award a merge request note with an award.
......@@ -377,25 +377,25 @@ impl Gitlab {
project,
merge_request,
note);
self._post_with_param(path, &[("name", award)])
self.post_with_param(path, &[("name", award)])
}
/// Get the awards for a merge request.
pub fn merge_request_awards(&self, project: ProjectId, merge_request: MergeRequestInternalId)
-> Result<Vec<AwardEmoji>> {
self._get_paged(&format!("projects/{}/merge_requests/{}/award_emoji",
project,
merge_request))
self.get_paged(&format!("projects/{}/merge_requests/{}/award_emoji",
project,
merge_request))
}
/// Get the awards for a merge request note.
pub fn merge_request_note_awards(&self, project: ProjectId, merge_request: MergeRequestInternalId,
note: NoteId)
-> Result<Vec<AwardEmoji>> {
self._get_paged(&format!("projects/{}/merge_requests/{}/notes/{}/award_emoji",
project,
merge_request,
note))
self.get_paged(&format!("projects/{}/merge_requests/{}/notes/{}/award_emoji",
project,
merge_request,
note))
}
/// Create a note on a merge request.
......@@ -405,7 +405,7 @@ impl Gitlab {
let path = &format!("projects/{}/merge_requests/{}/notes",
project,
merge_request);
self._post_with_param(path, &[("body", content)])
self.post_with_param(path, &[("body", content)])
}
/// Get issues closed by a merge request.
......@@ -415,7 +415,7 @@ impl Gitlab {
let path = &format!("projects/{}/merge_requests/{}/closes_issues",
project,
merge_request);
self._get_paged(path)
self.get_paged(path)
}
/// Set the labels on an issue.
......@@ -427,7 +427,7 @@ impl Gitlab {
let path = &format!("projects/{}/issues/{}",
project,
issue);
self._put_with_param(path, &[("labels", labels.into_iter().join(","))])
self.put_with_param(path, &[("labels", labels.into_iter().join(","))])
}
/// Set the labels on a merge request.
......@@ -439,29 +439,29 @@ impl Gitlab {
let path = &format!("projects/{}/merge_requests/{}",
project,
merge_request);
self._put_with_param(path, &[("labels", labels.into_iter().join(","))])
self.put_with_param(path, &[("labels", labels.into_iter().join(","))])
}
/// Create a URL to an API endpoint.
fn _mk_url(&self, url: &str) -> Result<Url> {
fn create_url(&self, url: &str) -> Result<Url> {
debug!(target: "gitlab", "api call {}", url);
Ok(self.base_url.join(url).chain_err(|| ErrorKind::UrlParse)?)
}
/// Create a URL to an API endpoint with query parameters.
fn _mk_url_with_param<I, K, V>(&self, url: &str, param: I) -> Result<Url>
fn create_url_with_param<I, K, V>(&self, url: &str, param: I) -> Result<Url>
where I: IntoIterator,
I::Item: Borrow<(K, V)>,
K: AsRef<str>,
V: AsRef<str>,
{
let mut full_url = self._mk_url(url)?;
let mut full_url = self.create_url(url)?;
full_url.query_pairs_mut().extend_pairs(param);
Ok(full_url)
}
/// Refactored code which talks to Gitlab and transforms error messages properly.
fn _comm<T>(&self, mut req: RequestBuilder) -> Result<T>
fn send<T>(&self, mut req: RequestBuilder) -> Result<T>
where T: DeserializeOwned,
{
req.header(GitlabPrivateToken(self.token.to_string()));
......@@ -479,74 +479,74 @@ impl Gitlab {
}
/// Create a `GET` request to an API endpoint.
fn _get<T>(&self, url: &str) -> Result<T>
fn get<T>(&self, url: &str) -> Result<T>
where T: DeserializeOwned,
{
let param: &[(&str, &str)] = &[];
self._get_with_param(url, param)
self.get_with_param(url, param)
}
/// Create a `GET` request to an API endpoint with query parameters.
fn _get_with_param<T, I, K, V>(&self, url: &str, param: I) -> Result<T>
fn get_with_param<T, I, K, V>(&self, url: &str, param: I) -> Result<T>
where T: DeserializeOwned,
I: IntoIterator,
I::Item: Borrow<(K, V)>,
K: AsRef<str>,
V: AsRef<str>,
{
let full_url = self._mk_url_with_param(url, param)?;
let full_url = self.create_url_with_param(url, param)?;
let req = self.client.get(full_url)?;
self._comm(req)
self.send(req)
}
/// Create a `POST` request to an API endpoint.
fn _post<T>(&self, url: &str) -> Result<T>
fn post<T>(&self, url: &str) -> Result<T>
where T: DeserializeOwned,
{
let param: &[(&str, &str)] = &[];
self._post_with_param(url, param)
self.post_with_param(url, param)
}
/// Create a `POST` request to an API endpoint with query parameters.
fn _post_with_param<T, U>(&self, url: &str, param: U) -> Result<T>
fn post_with_param<T, U>(&self, url: &str, param: U) -> Result<T>
where T: DeserializeOwned,
U: Serialize,
{
let full_url = self._mk_url(url)?;
let full_url = self.create_url(url)?;
let mut req = self.client.post(full_url)?;
req.form(&param)?;
self._comm(req)
self.send(req)
}
/// Create a `PUT` request to an API endpoint.
fn _put<T>(&self, url: &str) -> Result<T>
fn put<T>(&self, url: &str) -> Result<T>
where T: DeserializeOwned,
{
let param: &[(&str, &str)] = &[];
self._put_with_param(url, param)
self.put_with_param(url, param)
}
/// Create a `PUT` request to an API endpoint with query parameters.
fn _put_with_param<T, U>(&self, url: &str, param: U) -> Result<T>
fn put_with_param<T, U>(&self, url: &str, param: U) -> Result<T>
where T: DeserializeOwned,
U: Serialize,
{
let full_url = self._mk_url(url)?;
let full_url = self.create_url(url)?;
let mut req = self.client.request(Method::Put, full_url)?;
req.form(&param)?;
self._comm(req)
self.send(req)
}
/// Handle paginated queries. Returns all results.
fn _get_paged<T>(&self, url: &str) -> Result<Vec<T>>
fn get_paged<T>(&self, url: &str) -> Result<Vec<T>>
where T: DeserializeOwned,
{
let param: &[(&str, &str)] = &[];
self._get_paged_with_param(url, param)
self.get_paged_with_param(url, param)
}
/// Handle paginated queries with query parameters. Returns all results.
fn _get_paged_with_param<T, I, K, V>(&self, url: &str, param: I) -> Result<Vec<T>>
fn get_paged_with_param<T, I, K, V>(&self, url: &str, param: I) -> Result<Vec<T>>
where T: DeserializeOwned,
I: IntoIterator,
I::Item: Borrow<(K, V)>,
......@@ -557,7 +557,7 @@ impl Gitlab {
let per_page = 100;
let per_page_str = &format!("{}", per_page);
let full_url = self._mk_url_with_param(url, param)?;
let full_url = self.create_url_with_param(url, param)?;
let mut results: Vec<T> = vec![];
......@@ -568,7 +568,7 @@ impl Gitlab {
.extend_pairs(&[("page", page_str), ("per_page", per_page_str)]);
let req = self.client.get(page_url)?;
let page: Vec<T> = self._comm(req)?;
let page: Vec<T> = self.send(req)?;
let page_len = page.len();
results.extend(page);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment