-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #301 from hahmed/search-base-class
Base class for searching GitHub
- Loading branch information
Showing
6 changed files
with
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
|
||
namespace Octokit | ||
{ | ||
/// <summary> | ||
/// Base class for searching issues/code/users/repos | ||
/// </summary> | ||
[SuppressMessage("Microsoft.Design", "CA1012:AbstractTypesShouldNotHaveConstructors")] | ||
public abstract class BaseSearchRequest | ||
{ | ||
public BaseSearchRequest(string term) | ||
{ | ||
Ensure.ArgumentNotNullOrEmptyString(term, "term"); | ||
Term = term; | ||
Page = 1; | ||
PerPage = 100; | ||
Order = SortDirection.Descending; | ||
} | ||
|
||
/// <summary> | ||
/// The search term | ||
/// </summary> | ||
public string Term { get; private set; } | ||
|
||
/// <summary> | ||
/// The sort field | ||
/// </summary> | ||
public abstract string Sort | ||
{ | ||
get; | ||
} | ||
|
||
private string SortOrder | ||
{ | ||
get | ||
{ | ||
return Order == SortDirection.Ascending ? "asc" : "desc"; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Optional Sort order if sort parameter is provided. One of asc or desc; the default is desc. | ||
/// </summary> | ||
public SortDirection Order { get; set; } | ||
|
||
/// <summary> | ||
/// Page of paginated results | ||
/// </summary> | ||
public int Page { get; set; } | ||
|
||
/// <summary> | ||
/// Number of items per page | ||
/// </summary> | ||
public int PerPage { get; set; } | ||
|
||
/// <summary> | ||
/// All qualifiers that are used for this search | ||
/// </summary> | ||
public abstract IReadOnlyCollection<string> MergedQualifiers(); | ||
|
||
/// <summary> | ||
/// Add qualifiers onto the search term | ||
/// </summary> | ||
private string TermAndQualifiers | ||
{ | ||
get | ||
{ | ||
var mergedParameters = String.Join("+", MergedQualifiers()); | ||
return Term + (mergedParameters.IsNotBlank() ? "+" + mergedParameters : ""); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Get the query parameters that will be appending onto the search | ||
/// </summary> | ||
public IDictionary<string, string> Parameters | ||
{ | ||
get | ||
{ | ||
var d = new Dictionary<string, string>(); | ||
d.Add("page", Page.ToString(CultureInfo.CurrentCulture)); | ||
d.Add("per_page", PerPage.ToString(CultureInfo.CurrentCulture)); | ||
d.Add("sort", Sort); | ||
d.Add("order", SortOrder); | ||
d.Add("q", TermAndQualifiers); | ||
return d; | ||
} | ||
} | ||
} | ||
} |
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
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
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