|
| 1 | +using System; |
| 2 | +using GithubSharp.Core.Base; |
| 3 | + |
| 4 | +namespace GithubSharp.Core |
| 5 | +{ |
| 6 | + public interface IGithubRequest |
| 7 | + { |
| 8 | + string Method { get;} |
| 9 | + string Path {get;set;} |
| 10 | + IGithubResponse GetResponse(); |
| 11 | + Core.Services.ILogProvider LogProvider {get;set;} |
| 12 | + Core.Services.IAuthProvider AuthProvider {get;set;} |
| 13 | + Core.Services.ICacheProvider CacheProvider {get;set;} |
| 14 | + int? PagingRequestAmount {get;set;} |
| 15 | + int? PagingCurrentPage {get;set;} |
| 16 | + } |
| 17 | + |
| 18 | + |
| 19 | + public class GithubRequest : IGithubRequest |
| 20 | + { |
| 21 | + public Core.Services.ILogProvider LogProvider {get;set;} |
| 22 | + public Core.Services.IAuthProvider AuthProvider {get;set;} |
| 23 | + public Core.Services.ICacheProvider CacheProvider {get;set;} |
| 24 | + public virtual string Method { get;set; } |
| 25 | + public virtual string Path { get;set; } |
| 26 | + public virtual int? PagingRequestAmount { get;set; } |
| 27 | + public virtual int? PagingCurrentPage { get;set; } |
| 28 | + |
| 29 | + public GithubRequest( |
| 30 | + Core.Services.ILogProvider logProvider, |
| 31 | + Core.Services.ICacheProvider cacheProvider, |
| 32 | + Core.Services.IAuthProvider authProvider, |
| 33 | + string path) |
| 34 | + :this(logProvider, cacheProvider, authProvider, path, "GET") |
| 35 | + { |
| 36 | + } |
| 37 | + |
| 38 | + public GithubRequest( |
| 39 | + Core.Services.ILogProvider logProvider, |
| 40 | + Core.Services.ICacheProvider cacheProvider, |
| 41 | + Core.Services.IAuthProvider authProvider, |
| 42 | + string path, |
| 43 | + string method) |
| 44 | + { |
| 45 | + LogProvider = logProvider; |
| 46 | + CacheProvider = cacheProvider; |
| 47 | + AuthProvider = authProvider; |
| 48 | + Path = path; |
| 49 | + Method = method; |
| 50 | + } |
| 51 | + |
| 52 | + public virtual bool IsCached(string uri) |
| 53 | + { |
| 54 | + return CacheProvider.IsCached<IGithubResponse>(uri); |
| 55 | + } |
| 56 | + |
| 57 | + public virtual IGithubResponse GetFromCache(string uri) |
| 58 | + { |
| 59 | + var cached = CacheProvider.Get<IGithubResponse>(uri); |
| 60 | + return cached; |
| 61 | + } |
| 62 | + |
| 63 | + public virtual void Cache(IGithubResponse response, string uri) |
| 64 | + { |
| 65 | + CacheProvider.Set(response, uri); |
| 66 | + } |
| 67 | + |
| 68 | + public virtual System.Net.HttpWebRequest PrepareWebRequest(System.Net.HttpWebRequest webRequest) |
| 69 | + { |
| 70 | + webRequest.Accept = "application/vnd.github+json"; |
| 71 | + |
| 72 | + return webRequest; |
| 73 | + } |
| 74 | + |
| 75 | + //Hack for mono |
| 76 | + public static bool Validator (object sender, |
| 77 | + System.Security.Cryptography.X509Certificates.X509Certificate certificate, |
| 78 | + System.Security.Cryptography.X509Certificates.X509Chain chain, |
| 79 | + System.Net.Security.SslPolicyErrors sslPolicyErrors) |
| 80 | + { |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + public string AddQueryStringToUri(string uri, |
| 85 | + string name, |
| 86 | + string val) |
| 87 | + { |
| 88 | + return string.Format("{0}{1}{2}={3}", |
| 89 | + uri, |
| 90 | + uri.Contains("?") ? "&" : "?", |
| 91 | + name, |
| 92 | + val); |
| 93 | + } |
| 94 | + |
| 95 | + public virtual string AddPagingToUri(string uri) |
| 96 | + { |
| 97 | + if (PagingCurrentPage.HasValue) |
| 98 | + uri = AddQueryStringToUri(uri, "page", PagingCurrentPage.Value.ToString()); |
| 99 | + if (PagingRequestAmount.HasValue) |
| 100 | + uri = AddQueryStringToUri(uri, "per_page", PagingRequestAmount.Value.ToString()); |
| 101 | + return uri; |
| 102 | + } |
| 103 | + |
| 104 | + public virtual string PrepareUri(string uri) |
| 105 | + { |
| 106 | + return uri; |
| 107 | + } |
| 108 | + |
| 109 | + public virtual GithubResponse ParsePagingLinks(GithubResponse response, string linkHeader) |
| 110 | + { |
| 111 | + foreach (var header in linkHeader.Split(new char [] { ',' })){ |
| 112 | + var splitted = header.Split(new char[] { ';' }); |
| 113 | + var link = splitted[0].Trim(); |
| 114 | + var what = splitted[1].Trim(); |
| 115 | + what = what.Substring(5); |
| 116 | + what = what.Substring(0, what.Length-1); |
| 117 | + link = link.Substring(1); |
| 118 | + link = link.Substring(0, link.Length-1); |
| 119 | + switch(what) |
| 120 | + { |
| 121 | + case "next" : response.LinkNext = link;break; |
| 122 | + case "prev" : response.LinkPrevious = link;break; |
| 123 | + case "first" : response.LinkFirst = link;break; |
| 124 | + default : response.LinkLast = link;break; |
| 125 | + } |
| 126 | + } |
| 127 | + return response; |
| 128 | + } |
| 129 | + |
| 130 | + public virtual IGithubResponse GetResponse () |
| 131 | + { |
| 132 | + var uri = string.Format("{0}/{1}", |
| 133 | + GithubSharp.Core.GithubURLs.GithubApiBaseUrl, |
| 134 | + Path); |
| 135 | + |
| 136 | + uri = AddPagingToUri(uri); |
| 137 | + |
| 138 | + uri = PrepareUri(uri); |
| 139 | + |
| 140 | + if (IsCached(uri)) |
| 141 | + { |
| 142 | + LogProvider.LogMessage("Returning cached result for {0}", uri); |
| 143 | + return GetFromCache(uri) as GithubResponse; |
| 144 | + } |
| 145 | + |
| 146 | + //Hack for mono |
| 147 | + System.Net.ServicePointManager.ServerCertificateValidationCallback = Validator; |
| 148 | + |
| 149 | + var webRequest = System.Net.HttpWebRequest.Create(new Uri(uri)) as System.Net.HttpWebRequest; |
| 150 | + |
| 151 | + webRequest.Method = Method; |
| 152 | + |
| 153 | + var authResult = AuthProvider.PreRequestAuth(this, webRequest); |
| 154 | + |
| 155 | + if (!authResult.Success) |
| 156 | + { |
| 157 | + var authError = new GithubAuthenticationException(uri); |
| 158 | + if (LogProvider.HandleAndReturnIfToThrowError(authError)) |
| 159 | + { |
| 160 | + throw authError; |
| 161 | + } |
| 162 | + return new GithubFailedResponse(uri); |
| 163 | + } |
| 164 | + |
| 165 | + webRequest = PrepareWebRequest(authResult.WebRequest); |
| 166 | + |
| 167 | + try |
| 168 | + { |
| 169 | + var response = webRequest.GetResponse(); |
| 170 | + var responseString = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd(); |
| 171 | + var responseToReturn = new GithubResponse |
| 172 | + { |
| 173 | + RateLimitLimit = int.Parse(response.Headers["X-RateLimit-Limit"]), |
| 174 | + RateLimitRemaining = int.Parse(response.Headers["X-RateLimit-Remaining"]), |
| 175 | + Response = responseString |
| 176 | + }; |
| 177 | + if (!string.IsNullOrEmpty(response.Headers.Get("Link"))) |
| 178 | + { |
| 179 | + responseToReturn = ParsePagingLinks( |
| 180 | + responseToReturn, |
| 181 | + response.Headers.Get("Link")); |
| 182 | + } |
| 183 | + Cache(responseToReturn, uri); |
| 184 | + return responseToReturn; |
| 185 | + |
| 186 | + } |
| 187 | + catch (Exception error) |
| 188 | + { |
| 189 | + if (LogProvider.HandleAndReturnIfToThrowError(error)) |
| 190 | + throw; |
| 191 | + return new GithubFailedResponse(uri); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + |
| 196 | + } |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | +} |
| 201 | + |
0 commit comments