From 869c70d237f79fd8078abb08f3e65caa28ad42c9 Mon Sep 17 00:00:00 2001 From: MysticBoy Date: Tue, 19 Mar 2019 14:44:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A1=AB=E5=86=99=E9=92=88=E5=AF=B9gitee?= =?UTF-8?q?=E7=9A=84sdk=E7=9A=84=20nuge=E5=8C=85=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Gitee.Api/Dto/CreateRepo.cs | 31 ++-- src/Gitee.Api/Dto/GiteeApiException.cs | 16 +- src/Gitee.Api/Dto/Repo.cs | 208 +++++++++++++++++-------- src/Gitee.Api/Dto/TokenDto.cs | 19 ++- src/Gitee.Api/Gitee.Api.csproj | 9 ++ src/Gitee.Api/SDK.cs | 49 +++--- src/Gitee.Api/Session.cs | 4 +- 7 files changed, 218 insertions(+), 118 deletions(-) diff --git a/src/Gitee.Api/Dto/CreateRepo.cs b/src/Gitee.Api/Dto/CreateRepo.cs index 89e4706..c235dd0 100644 --- a/src/Gitee.Api/Dto/CreateRepo.cs +++ b/src/Gitee.Api/Dto/CreateRepo.cs @@ -9,42 +9,51 @@ namespace Gitee.Api.Dto public class CreateRepo { /// - /// + /// /// public string access_token { get; set; } + /// - /// + /// /// public string name { get; set; } + /// - /// + /// /// public string description { get; set; } + /// - /// + /// /// public string homepage { get; set; } + /// - /// + /// /// public bool has_issues { get; set; } + /// - /// + /// /// public bool has_wiki { get; set; } + /// - /// + /// /// public bool auto_init { get; set; } + /// - /// + /// /// public string gitignore_template { get; set; } + /// - /// + /// /// public string license_template { get; set; } + [Newtonsoft.Json.JsonProperty("private")] public bool IsPrivate { get; set; } -} -} + } +} \ No newline at end of file diff --git a/src/Gitee.Api/Dto/GiteeApiException.cs b/src/Gitee.Api/Dto/GiteeApiException.cs index e960344..ea33f29 100644 --- a/src/Gitee.Api/Dto/GiteeApiException.cs +++ b/src/Gitee.Api/Dto/GiteeApiException.cs @@ -6,35 +6,39 @@ using System.Threading.Tasks; namespace Gitee.Api.Dto { - public class GiteeApiException : Exception { - public GiteeApiException() { } + public GiteeApiException() + { + } + public GiteeApiException(ApiError error) : base(error.error_description) { ApiError = error; - } + public GiteeApiException(ApiError error, Exception inner) : base(error.error_description, inner) { ApiError = error; } + public GiteeApiException(string message, Exception inner) : base(message, inner) { - } + public ApiError ApiError { get; private set; } } public class ApiError { /// - /// + /// /// public string error { get; set; } + /// /// 授权方式无效,或者登录回调地址无效、过期或已被撤销 /// public string error_description { get; set; } } -} +} \ No newline at end of file diff --git a/src/Gitee.Api/Dto/Repo.cs b/src/Gitee.Api/Dto/Repo.cs index 5b45757..96b7f11 100644 --- a/src/Gitee.Api/Dto/Repo.cs +++ b/src/Gitee.Api/Dto/Repo.cs @@ -6,301 +6,371 @@ using System.Threading.Tasks; namespace Gitee.Api.Dto { - public class Owner { /// - /// + /// /// public int id { get; set; } + /// - /// + /// /// public string login { get; set; } + [Newtonsoft.Json.JsonProperty("name")] public string Name { get; set; } + [Newtonsoft.Json.JsonProperty("avatar_url")] public string AvatarUrl { get; set; } + [Newtonsoft.Json.JsonProperty("url")] public string Url { get; set; } + /// - /// + /// /// public string html_url { get; set; } + /// - /// + /// /// public string followers_url { get; set; } + /// - /// + /// /// public string following_url { get; set; } + /// - /// + /// /// public string gists_url { get; set; } + /// - /// + /// /// public string starred_url { get; set; } + /// - /// + /// /// public string subscriptions_url { get; set; } + /// - /// + /// /// public string organizations_url { get; set; } + /// - /// + /// /// public string repos_url { get; set; } + /// - /// + /// /// public string events_url { get; set; } + /// - /// + /// /// public string received_events_url { get; set; } + /// - /// + /// /// public string type { get; set; } + /// - /// + /// /// public string site_admin { get; set; } } + public class RepoPermission { /// - /// + /// /// public string pull { get; set; } + /// - /// + /// /// public string push { get; set; } + /// - /// + /// /// public string admin { get; set; } } - public class Repo { /// - /// + /// /// public int id { get; set; } + /// - /// + /// /// public string full_name { get; set; } + /// - /// + /// /// public string human_name { get; set; } + [Newtonsoft.Json.JsonProperty("url")] public string Url { get; set; } + [Newtonsoft.Json.JsonProperty("namespace")] public Namespace Namespace { get; set; } + /// - /// + /// /// public string path { get; set; } + [Newtonsoft.Json.JsonProperty("name")] public string Name { get; set; } + [Newtonsoft.Json.JsonProperty("owner")] public Owner Owner { get; set; } + [Newtonsoft.Json.JsonProperty("description")] public string Description { get; set; } + [Newtonsoft.Json.JsonProperty("private")] public bool IsPrivate { get; set; } + [Newtonsoft.Json.JsonProperty("public")] public bool IsPublic { get; set; } + [Newtonsoft.Json.JsonProperty("internal")] public bool IsInternal { get; set; } public bool fork { get; set; } + /// - /// + /// /// public string html_url { get; set; } + /// - /// + /// /// public string forks_url { get; set; } + /// - /// + /// /// public string keys_url { get; set; } + /// - /// + /// /// public string collaborators_url { get; set; } + /// - /// + /// /// public string hooks_url { get; set; } + /// - /// + /// /// public string branches_url { get; set; } + /// - /// + /// /// public string tags_url { get; set; } + /// - /// + /// /// public string blobs_url { get; set; } + /// - /// + /// /// public string stargazers_url { get; set; } + /// - /// + /// /// public string contributors_url { get; set; } + /// - /// + /// /// public string commits_url { get; set; } + /// - /// + /// /// public string comments_url { get; set; } + /// - /// + /// /// public string issue_comment_url { get; set; } + /// - /// + /// /// public string issues_url { get; set; } + /// - /// + /// /// public string pulls_url { get; set; } + /// - /// + /// /// public string milestones_url { get; set; } + /// - /// + /// /// public string notifications_url { get; set; } + /// - /// + /// /// public string labels_url { get; set; } + /// - /// + /// /// public string releases_url { get; set; } + /// - /// + /// /// public string recommend { get; set; } + /// - /// + /// /// public string homepage { get; set; } + /// - /// + /// /// public string language { get; set; } + /// - /// + /// /// public int forks_count { get; set; } + /// - /// + /// /// public int stargazers_count { get; set; } + /// - /// + /// /// public int watchers_count { get; set; } + /// - /// + /// /// public string default_branch { get; set; } + /// - /// + /// /// public int open_issues_count { get; set; } + /// - /// + /// /// public bool has_issues { get; set; } + /// - /// + /// /// public bool has_wiki { get; set; } + /// - /// + /// /// public bool pull_requests_enabled { get; set; } + /// - /// + /// /// public bool has_page { get; set; } + /// - /// + /// /// public string license { get; set; } + /// - /// + /// /// public string outsourced { get; set; } + /// - /// + /// /// public string project_creator { get; set; } + /// - /// + /// /// public List members { get; set; } + /// - /// + /// /// public string pushed_at { get; set; } + /// - /// + /// /// public string created_at { get; set; } + /// - /// + /// /// public string updated_at { get; set; } + /// - /// + /// /// // public string parent { get; set; } /// - /// + /// /// public string paas { get; set; } + /// - /// + /// /// public string stared { get; set; } + /// - /// + /// /// public string watched { get; set; } + /// - /// + /// /// public RepoPermission permission { get; set; } + /// - /// + /// /// public string relation { get; set; } } - -} +} \ No newline at end of file diff --git a/src/Gitee.Api/Dto/TokenDto.cs b/src/Gitee.Api/Dto/TokenDto.cs index cfcc1db..5c57752 100644 --- a/src/Gitee.Api/Dto/TokenDto.cs +++ b/src/Gitee.Api/Dto/TokenDto.cs @@ -9,30 +9,35 @@ namespace Gitee.Api.Dto public class TokenDto { /// - /// + /// /// public string access_token { get; set; } + /// - /// + /// /// public string token_type { get; set; } + /// - /// + /// /// public int expires_in { get; set; } public DateTime Expires => DateTime.Now.AddSeconds(expires_in); + /// - /// + /// /// public string refresh_token { get; set; } + /// - /// + /// /// public string scope { get; set; } + /// - /// + /// /// public int created_at { get; set; } } -} +} \ No newline at end of file diff --git a/src/Gitee.Api/Gitee.Api.csproj b/src/Gitee.Api/Gitee.Api.csproj index 58ae8d4..e8e570f 100644 --- a/src/Gitee.Api/Gitee.Api.csproj +++ b/src/Gitee.Api/Gitee.Api.csproj @@ -2,6 +2,15 @@ net461;netstandard2.0 + https://gitee.com/GitGroup/Gitee.VisualStudio/tree/master/src/Gitee.Api + https://gitee.com/logo_icon.png + https://gitee.com/GitGroup/Gitee.VisualStudio + https://gitee.com/GitGroup/Gitee.VisualStudio/raw/master/LICENSE + gitee.com 的.Net SDK + gitee,git + maikebing + Gitee.com + true diff --git a/src/Gitee.Api/SDK.cs b/src/Gitee.Api/SDK.cs index e534562..0c51201 100644 --- a/src/Gitee.Api/SDK.cs +++ b/src/Gitee.Api/SDK.cs @@ -13,8 +13,8 @@ namespace Gitee.Api { public static class SDK { - public static string client_id { get; set; } - public static string client_secret { get; set; } + public static string client_id { get; set; } + public static string client_secret { get; set; } public static Task GetUserAsync(this Session session) => session.Client.GetV5UserAsync(session.Token.access_token); @@ -24,12 +24,13 @@ namespace Gitee.Api /// /// /// - public static Task> GetReposAsync(this Session session,int page,int per_page=20) + public static Task> GetReposAsync(this Session session, int page, int per_page = 20) { - return Request>(session, "v5/user/repos", new { sort = "full_name", page , per_page }); + return Request>(session, "v5/user/repos", new { sort = "full_name", page, per_page }); } + /// - /// + /// /// /// /// @@ -37,21 +38,22 @@ namespace Gitee.Api /// /// /// - public static async Task CreateRepoAsync(this Session session, string name, string description,bool isPrivate) + public static async Task CreateRepoAsync(this Session session, string name, string description, bool isPrivate) { - return await Request(session, "v5/user/repos", new CreateRepo() { access_token= session.Token.access_token, description=description, has_issues=true, has_wiki=true, IsPrivate=isPrivate, name=name } ,Method.POST,true); + return await Request(session, "v5/user/repos", new CreateRepo() { access_token = session.Token.access_token, description = description, has_issues = true, has_wiki = true, IsPrivate = isPrivate, name = name }, Method.POST, true); } - /// - /// 此请求方法主要用来弥补自动生成的部分api无法正常使用,使用RestSharp重新实现, 在未来,可能完全取消自动生成部分 - /// - /// - /// 会话必要信息 - /// 请求资源连接字符串 - /// 参数类, 根据来决定处理为json还是query条件 - /// Http方法,默认GET - /// Gitee大部分api是作为Query传送的,只有少数是 json,因此默认为false - /// - public static async Task Request(this Session session, string resource, object parameters, Method method = Method.GET,bool paramAsJson=false) where T : class + + /// + /// 此请求方法主要用来弥补自动生成的部分api无法正常使用,使用RestSharp重新实现, 在未来,可能完全取消自动生成部分 + /// + /// + /// 会话必要信息 + /// 请求资源连接字符串 + /// 参数类, 根据来决定处理为json还是query条件 + /// Http方法,默认GET + /// Gitee大部分api是作为Query传送的,只有少数是 json,因此默认为false + /// + public static async Task Request(this Session session, string resource, object parameters, Method method = Method.GET, bool paramAsJson = false) where T : class { T result = default(T); try @@ -73,7 +75,7 @@ namespace Gitee.Api } } var response = await new RestClient(session.BaseURL).ExecuteTaskAsync(request); - if ((response.StatusCode == HttpStatusCode.OK || response.StatusCode== HttpStatusCode.Created || response.StatusCode == HttpStatusCode.Accepted) && !string.IsNullOrEmpty(response.Content)) + if ((response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.Accepted) && !string.IsNullOrEmpty(response.Content)) { var jtk = Newtonsoft.Json.Linq.JToken.Parse(response.Content); result = jtk.ToObject(); @@ -103,14 +105,14 @@ namespace Gitee.Api public static async Task LoginAsync(string username, string password) { var _session = new Session(); - _session.Token = await _session.Request("../oauth/token", new { grant_type = "password", username, password, client_id, client_secret, scope = "projects user_info issues notes" }, Method.POST); + _session.Token = await _session.Request("../oauth/token", new { grant_type = "password", username, password, client_id, client_secret, scope = "projects user_info issues notes" }, Method.POST); _session.Client = new Client(new HttpClient()); _session.Client.BaseUrl = _session.BaseURL; return _session; - } + /// - /// + /// /// /// /// @@ -118,11 +120,12 @@ namespace Gitee.Api public static async Task RefreshToken(string refresh_token) { var _session = new Session(); - _session.Token = await _session.Request("../oauth/token", new { grant_type = "refresh_token", refresh_token}, Method.POST); + _session.Token = await _session.Request("../oauth/token", new { grant_type = "refresh_token", refresh_token }, Method.POST); _session.Client = new Client(new HttpClient()); _session.Client.BaseUrl = _session.BaseURL; return _session; } + /// /// 根据缓存的 token信息继续使用, 如果没过期的话 /// diff --git a/src/Gitee.Api/Session.cs b/src/Gitee.Api/Session.cs index 2671b55..b7943e5 100644 --- a/src/Gitee.Api/Session.cs +++ b/src/Gitee.Api/Session.cs @@ -12,5 +12,5 @@ namespace Gitee.Api public string BaseURL { get; private set; } = "https://gitee.com/api"; public Client Client { get; set; } = null; public TokenDto Token { get; set; } = null; - } -} + } +} \ No newline at end of file -- Gitee