Permalink
Browse files

base api should be good to go, added some more tests and a basic auth…

…provider
  • Loading branch information...
erikzaadi committed Nov 19, 2011
1 parent 7ccdb56 commit cae915532ee4b8367e1ea6a38bf598b3badc68ae
Showing with 576 additions and 144 deletions.
  1. +3 −2 Core/Base/JsonConverter.cs
  2. +7 −2 Core/Core.csproj
  3. +52 −0 Core/GithubError.cs
  4. +13 −2 Core/GithubRequest.cs
  5. +14 −10 Core/{GithubRequestWithInput.cs → GithubRequestWithInputAndReturnType.cs}
  6. +4 −5 Core/GithubRequestWithReturnType.cs
  7. +4 −0 Core/GithubResponse.cs
  8. +20 −0 Core/Models/BasicUser.cs
  9. +16 −0 Core/Models/ChangeStatus.cs
  10. +59 −59 Core/Models/Commit.cs
  11. +17 −0 Core/Models/Fork.cs
  12. +47 −0 Core/Models/Gist.cs
  13. +19 −0 Core/Models/History.cs
  14. +9 −0 GithubSharp.sln
  15. +0 −1 Plugins/AuthProviders/NullAuthProvider/NullAuthProvider.csproj
  16. +27 −0 Plugins/AuthProviders/UserPasswordAuthProvider/AssemblyInfo.cs
  17. +12 −0 Plugins/AuthProviders/UserPasswordAuthProvider/MyClass.cs
  18. +76 −0 Plugins/AuthProviders/UserPasswordAuthProvider/UserPasswordAuthProvider.cs
  19. +45 −0 Plugins/AuthProviders/UserPasswordAuthProvider/UserPasswordAuthProvider.csproj
  20. +2 −4 Plugins/CacheProviders/ApplicationCacher/ApplicationCacher.csproj
  21. +0 −1 Plugins/CacheProviders/NullCacher/NullCacher.csproj
  22. +2 −4 Plugins/CacheProviders/WebCache/WebCache.csproj
  23. +0 −1 Plugins/LogProviders/NullLogger/NullLogger.csproj
  24. +3 −6 Plugins/LogProviders/SimpleLogProvider/SimpleLogProvider.csproj
  25. +16 −25 Samples/MvcSample/MvcApplication/MvcApplication.csproj
  26. +11 −18 Samples/MvcSample/MvcUI/MvcUI.csproj
  27. +6 −3 Tests/CoreTests/CoreTests.csproj
  28. +76 −1 Tests/CoreTests/GithubRequestTest.cs
  29. +16 −0 Tests/CoreTests/TestSettings.cs
@@ -23,9 +23,10 @@ internal static string ToJson<T> (this T Obj)
return ServiceStack.Text.JsonSerializer.SerializeToString<T>(Obj);
}
- internal static void ToJsonStream<T>(this T Obj, Stream toWrite)
+ internal static byte[] ToJsonBytes<T>(this T TObj)
{
- ServiceStack.Text.JsonSerializer.SerializeToStream(Obj, toWrite);
+ var jsonString = ToJson<T>(TObj);
+ return System.Text.Encoding.UTF8.GetBytes(jsonString);
}
/// <summary>
View
@@ -101,13 +101,18 @@
<Compile Include="Services\ILogProvider.cs" />
<Compile Include="GithubRequest.cs" />
<Compile Include="GithubResponse.cs" />
- <Compile Include="GithubErrorResponse.cs" />
<Compile Include="Services\IAuthProvider.cs" />
<Compile Include="GithubAuthenticationException.cs" />
<Compile Include="GithubFailedResponse.cs" />
<Compile Include="GithubRequestWithBinaryFileToSend.cs" />
<Compile Include="GithubRequestWithReturnType.cs" />
- <Compile Include="GithubRequestWithInput.cs" />
+ <Compile Include="Models\Gist.cs" />
+ <Compile Include="Models\Fork.cs" />
+ <Compile Include="Models\BasicUser.cs" />
+ <Compile Include="Models\History.cs" />
+ <Compile Include="Models\ChangeStatus.cs" />
+ <Compile Include="GithubRequestWithInputAndReturnType.cs" />
+ <Compile Include="GithubError.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
View
@@ -0,0 +1,52 @@
+using System;
+using System.Collections.Generic;
+
+namespace GithubSharp.Core
+{
+ public class GithubError : Exception
+ {
+ public GithubError(System.Net.HttpWebResponse response, string uri)
+ :base(string.Format("Github error when retrieving {0}", uri))
+ {
+ var responseString = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();
+ GithubErrorResult = GithubSharp.Core.Base.JsonConverter.FromJson<GithubErrorModel>(responseString);
+ StatusCode = (int)response.StatusCode;
+ StatusText = response.StatusDescription;
+ }
+
+ public GithubErrorModel GithubErrorResult { get;set;}
+ public int StatusCode { get;set;}
+ public string StatusText { get;set;}
+ }
+
+ public class GithubErrorModel
+ {
+ public string message {
+ get;
+ set;
+ }
+ public IEnumerable<GithubErrorDetails> errors {
+ get;
+ set;
+ }
+ }
+
+ public class GithubErrorDetails
+ {
+ public string Resource {
+ get;
+ set;
+ }
+
+ public string Field {
+ get;
+ set;
+ }
+
+ public string Code {
+ get;
+ set;
+ }
+ }
+}
+
View
@@ -13,6 +13,7 @@ public interface IGithubRequest
Core.Services.ICacheProvider CacheProvider {get;set;}
int? PagingRequestAmount {get;set;}
int? PagingCurrentPage {get;set;}
+ System.Net.HttpWebRequest PrepareWebRequest(System.Net.HttpWebRequest webRequest);
}
@@ -159,15 +160,18 @@ public virtual IGithubResponse GetResponse ()
webRequest = PrepareWebRequest(authResult.WebRequest);
+
try
{
- var response = webRequest.GetResponse();
+ var response = webRequest.GetResponse() as System.Net.HttpWebResponse;
var responseString = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();
var responseToReturn = new GithubResponse
{
RateLimitLimit = int.Parse(response.Headers["X-RateLimit-Limit"]),
RateLimitRemaining = int.Parse(response.Headers["X-RateLimit-Remaining"]),
- Response = responseString
+ Response = responseString,
+ StatusCode = (int)response.StatusCode,
+ StatusText = response.StatusDescription
};
if (!string.IsNullOrEmpty(response.Headers.Get("Link")))
{
@@ -181,6 +185,13 @@ public virtual IGithubResponse GetResponse ()
}
catch (Exception error)
{
+ if (error is System.Net.WebException)
+ {
+ var webError = error as System.Net.WebException;
+ var githubException = new GithubError(webError.Response as System.Net.HttpWebResponse, uri);
+ if (LogProvider.HandleAndReturnIfToThrowError(githubException))
+ throw githubException;
+ }
if (LogProvider.HandleAndReturnIfToThrowError(error))
throw;
return new GithubFailedResponse(uri);
@@ -3,9 +3,10 @@
namespace GithubSharp.Core
{
- public class GithubRequestWithInput<TInputType> : GithubRequest
+ public class GithubRequestWithInputAndReturnType<TInputType,TReturnType> : GithubRequestWithReturnType<TReturnType>
+ where TReturnType : class
{
- public GithubRequestWithInput (
+ public GithubRequestWithInputAndReturnType (
ILogProvider logProvider,
ICacheProvider cacheProvider,
IAuthProvider authProvider,
@@ -18,8 +19,7 @@ public class GithubRequestWithInput<TInputType> : GithubRequest
{
ModelToSend = input;
}
-
- public GithubRequestWithInput (
+ public GithubRequestWithInputAndReturnType (
ILogProvider logProvider,
ICacheProvider cacheProvider,
IAuthProvider authProvider,
@@ -37,16 +37,20 @@ public class GithubRequestWithInput<TInputType> : GithubRequest
public TInputType ModelToSend { get;set;}
+
public override System.Net.HttpWebRequest PrepareWebRequest (System.Net.HttpWebRequest webRequest)
{
- var toReturn = base.PrepareWebRequest (webRequest);
-
-
- GithubSharp.Core.Base.JsonConverter.ToJsonStream<TInputType>
- (ModelToSend, toReturn.GetRequestStream());
+ webRequest.ContentType = "application/json"; //TODO Needed?
+ webRequest.MediaType = "UTF-8";
+ var bytes = GithubSharp.Core.Base.JsonConverter.ToJsonBytes<TInputType>
+ (ModelToSend);
+ webRequest.ContentLength = bytes.Length;
+ var stream = webRequest.GetRequestStream();
+ stream.Write(bytes, 0, bytes.Length);
+ stream.Close();
- return toReturn;
+ return webRequest;
}
}
}
@@ -13,6 +13,7 @@ public class GithubRequestWithReturnType<TReturnType> : GithubRequest
: base(logProvider, cacheProvider, authProvider, path)
{
}
+
public GithubRequestWithReturnType (Core.Services.ILogProvider logProvider,
Core.Services.ICacheProvider cacheProvider,
Core.Services.IAuthProvider authProvider,
@@ -32,7 +33,6 @@ private new IGithubResponse GetResponse()
return null;
}
-
public IGithubResponseWithReturnType<TReturnType> GetResponseWithReturnType()
{
var baseResult = base.GetResponse();
@@ -49,10 +49,8 @@ public IGithubResponseWithReturnType<TReturnType> GetResponseWithReturnType()
try
{
- baseWithReturnType.Result = ServiceStack.Text.JsonSerializer
- .DeserializeFromString<TReturnType>(baseWithReturnType.Response);
-// baseWithReturnType.Result = JsonConverter
-// .FromJson<TReturnType>(baseWithReturnType.Response);
+ baseWithReturnType.Result = JsonConverter
+ .FromJson<TReturnType>(baseWithReturnType.Response);
}
catch (Exception error)
{
@@ -62,6 +60,7 @@ public IGithubResponseWithReturnType<TReturnType> GetResponseWithReturnType()
return baseWithReturnType;
}
+
}
}
View
@@ -11,6 +11,8 @@ public interface IGithubResponse
string LinkPrevious { get; set; }
string LinkFirst { get; set; }
string LinkLast { get; set; }
+ int? StatusCode {get;set;}
+ string StatusText {get;set;}
}
public interface IGithubResponseWithReturnType<TResultType> : IGithubResponse
@@ -28,6 +30,8 @@ public class GithubResponse : IGithubResponse
public string LinkPrevious { get; set; }
public string LinkFirst { get; set; }
public string LinkLast { get; set; }
+ public int? StatusCode {get;set;}
+ public string StatusText {get;set;}
}
public class GithubResponseWithReturnType<TResultType> : GithubResponse, IGithubResponseWithReturnType<TResultType>
View
@@ -0,0 +1,20 @@
+using System;
+
+namespace GithubSharp.Core.Models
+{
+ [Serializable]
+ public class BasicUser
+ {
+ public BasicUser ()
+ {
+ }
+
+ public string login { get; set; }
+ public int id { get; set; }
+ public string avatar_url { get; set; }
+ public string gravatar_id { get; set; }
+ public string url { get; set; }
+
+ }
+}
+
@@ -0,0 +1,16 @@
+using System;
+
+namespace GithubSharp.Core.Models
+{
+ [Serializable]
+ public class ChangeStatus
+ {
+ public ChangeStatus ()
+ {
+ }
+ public int deletions { get; set; }
+ public int additions { get; set; }
+ public int total { get; set; }
+ }
+}
+
View
@@ -1,59 +1,59 @@
-using System;
-using System.Collections.Generic;
-using System.Runtime.Serialization;
-
-namespace GithubSharp.Core.Models
-{
- public class Commit
- {
- public List<CommmitParent> parents { get; set; }
-
- public Person author { get; set; }
-
- public string url { get; set; }
-
- public string sha { get; set; }
-
- public string message { get; set; }
-
- public CommmitParent tree { get; set; }
-
- public Person committer { get; set; }
- }
-
- public class CommmitParent
- {
- public string sha { get; set; }
-
- public string url { get; set; }
- }
-
- public class Person
- {
- public string name { get; set; }
-
- public DateTime date { get; set; }
-
- public string email { get; set; }
- }
-
- [DataContract]
- public class SingleFileCommit : Commit
- {
- [DataMember(Name = "added")]
- public IEnumerable<SingleFileCommitFileReference> Added { get; set; }
-
- [DataMember(Name = "removed")]
- public IEnumerable<SingleFileCommitFileReference> Removed { get; set; }
-
- [DataMember(Name = "modified")]
- public IEnumerable<SingleFileCommitFileReference> Modified { get; set; }
- }
-
- [DataContract]
- public class SingleFileCommitFileReference
- {
- [DataMember(Name = "filename")]
- public string Filename { get; set; }
- }
-}
+using System;
+using System.Collections.Generic;
+using System.Runtime.Serialization;
+
+namespace GithubSharp.Core.Models
+{
+ public class Commit
+ {
+ public List<CommmitParent> parents { get; set; }
+
+ public Person author { get; set; }
+
+ public string url { get; set; }
+
+ public string sha { get; set; }
+
+ public string message { get; set; }
+
+ public CommmitParent tree { get; set; }
+
+ public Person committer { get; set; }
+ }
+
+ public class CommmitParent
+ {
+ public string sha { get; set; }
+
+ public string url { get; set; }
+ }
+
+ public class Person
+ {
+ public string name { get; set; }
+
+ public DateTime date { get; set; }
+
+ public string email { get; set; }
+ }
+
+ [DataContract]
+ public class SingleFileCommit : Commit
+ {
+ [DataMember(Name = "added")]
+ public IEnumerable<SingleFileCommitFileReference> Added { get; set; }
+
+ [DataMember(Name = "removed")]
+ public IEnumerable<SingleFileCommitFileReference> Removed { get; set; }
+
+ [DataMember(Name = "modified")]
+ public IEnumerable<SingleFileCommitFileReference> Modified { get; set; }
+ }
+
+ [DataContract]
+ public class SingleFileCommitFileReference
+ {
+ [DataMember(Name = "filename")]
+ public string Filename { get; set; }
+ }
+}
Oops, something went wrong.

0 comments on commit cae9155

Please sign in to comment.