Skip to content

Commit cae9155

Browse files
committed
base api should be good to go, added some more tests and a basic authprovider
1 parent 7ccdb56 commit cae9155

29 files changed

+576
-144
lines changed

Core/Base/JsonConverter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ internal static string ToJson<T> (this T Obj)
2323
return ServiceStack.Text.JsonSerializer.SerializeToString<T>(Obj);
2424
}
2525

26-
internal static void ToJsonStream<T>(this T Obj, Stream toWrite)
26+
internal static byte[] ToJsonBytes<T>(this T TObj)
2727
{
28-
ServiceStack.Text.JsonSerializer.SerializeToStream(Obj, toWrite);
28+
var jsonString = ToJson<T>(TObj);
29+
return System.Text.Encoding.UTF8.GetBytes(jsonString);
2930
}
3031

3132
/// <summary>

Core/Core.csproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,18 @@
101101
<Compile Include="Services\ILogProvider.cs" />
102102
<Compile Include="GithubRequest.cs" />
103103
<Compile Include="GithubResponse.cs" />
104-
<Compile Include="GithubErrorResponse.cs" />
105104
<Compile Include="Services\IAuthProvider.cs" />
106105
<Compile Include="GithubAuthenticationException.cs" />
107106
<Compile Include="GithubFailedResponse.cs" />
108107
<Compile Include="GithubRequestWithBinaryFileToSend.cs" />
109108
<Compile Include="GithubRequestWithReturnType.cs" />
110-
<Compile Include="GithubRequestWithInput.cs" />
109+
<Compile Include="Models\Gist.cs" />
110+
<Compile Include="Models\Fork.cs" />
111+
<Compile Include="Models\BasicUser.cs" />
112+
<Compile Include="Models\History.cs" />
113+
<Compile Include="Models\ChangeStatus.cs" />
114+
<Compile Include="GithubRequestWithInputAndReturnType.cs" />
115+
<Compile Include="GithubError.cs" />
111116
</ItemGroup>
112117
<ItemGroup>
113118
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">

Core/GithubError.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace GithubSharp.Core
5+
{
6+
public class GithubError : Exception
7+
{
8+
public GithubError(System.Net.HttpWebResponse response, string uri)
9+
:base(string.Format("Github error when retrieving {0}", uri))
10+
{
11+
var responseString = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();
12+
GithubErrorResult = GithubSharp.Core.Base.JsonConverter.FromJson<GithubErrorModel>(responseString);
13+
StatusCode = (int)response.StatusCode;
14+
StatusText = response.StatusDescription;
15+
}
16+
17+
public GithubErrorModel GithubErrorResult { get;set;}
18+
public int StatusCode { get;set;}
19+
public string StatusText { get;set;}
20+
}
21+
22+
public class GithubErrorModel
23+
{
24+
public string message {
25+
get;
26+
set;
27+
}
28+
public IEnumerable<GithubErrorDetails> errors {
29+
get;
30+
set;
31+
}
32+
}
33+
34+
public class GithubErrorDetails
35+
{
36+
public string Resource {
37+
get;
38+
set;
39+
}
40+
41+
public string Field {
42+
get;
43+
set;
44+
}
45+
46+
public string Code {
47+
get;
48+
set;
49+
}
50+
}
51+
}
52+

Core/GithubRequest.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public interface IGithubRequest
1313
Core.Services.ICacheProvider CacheProvider {get;set;}
1414
int? PagingRequestAmount {get;set;}
1515
int? PagingCurrentPage {get;set;}
16+
System.Net.HttpWebRequest PrepareWebRequest(System.Net.HttpWebRequest webRequest);
1617
}
1718

1819

@@ -159,15 +160,18 @@ public virtual IGithubResponse GetResponse ()
159160

160161
webRequest = PrepareWebRequest(authResult.WebRequest);
161162

163+
162164
try
163165
{
164-
var response = webRequest.GetResponse();
166+
var response = webRequest.GetResponse() as System.Net.HttpWebResponse;
165167
var responseString = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();
166168
var responseToReturn = new GithubResponse
167169
{
168170
RateLimitLimit = int.Parse(response.Headers["X-RateLimit-Limit"]),
169171
RateLimitRemaining = int.Parse(response.Headers["X-RateLimit-Remaining"]),
170-
Response = responseString
172+
Response = responseString,
173+
StatusCode = (int)response.StatusCode,
174+
StatusText = response.StatusDescription
171175
};
172176
if (!string.IsNullOrEmpty(response.Headers.Get("Link")))
173177
{
@@ -181,6 +185,13 @@ public virtual IGithubResponse GetResponse ()
181185
}
182186
catch (Exception error)
183187
{
188+
if (error is System.Net.WebException)
189+
{
190+
var webError = error as System.Net.WebException;
191+
var githubException = new GithubError(webError.Response as System.Net.HttpWebResponse, uri);
192+
if (LogProvider.HandleAndReturnIfToThrowError(githubException))
193+
throw githubException;
194+
}
184195
if (LogProvider.HandleAndReturnIfToThrowError(error))
185196
throw;
186197
return new GithubFailedResponse(uri);

Core/GithubRequestWithInput.cs renamed to Core/GithubRequestWithInputAndReturnType.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
namespace GithubSharp.Core
55
{
6-
public class GithubRequestWithInput<TInputType> : GithubRequest
6+
public class GithubRequestWithInputAndReturnType<TInputType,TReturnType> : GithubRequestWithReturnType<TReturnType>
7+
where TReturnType : class
78
{
8-
public GithubRequestWithInput (
9+
public GithubRequestWithInputAndReturnType (
910
ILogProvider logProvider,
1011
ICacheProvider cacheProvider,
1112
IAuthProvider authProvider,
@@ -18,8 +19,7 @@ public GithubRequestWithInput (
1819
{
1920
ModelToSend = input;
2021
}
21-
22-
public GithubRequestWithInput (
22+
public GithubRequestWithInputAndReturnType (
2323
ILogProvider logProvider,
2424
ICacheProvider cacheProvider,
2525
IAuthProvider authProvider,
@@ -37,16 +37,20 @@ public GithubRequestWithInput (
3737

3838
public TInputType ModelToSend { get;set;}
3939

40+
4041
public override System.Net.HttpWebRequest PrepareWebRequest (System.Net.HttpWebRequest webRequest)
4142
{
42-
var toReturn = base.PrepareWebRequest (webRequest);
43-
44-
45-
GithubSharp.Core.Base.JsonConverter.ToJsonStream<TInputType>
46-
(ModelToSend, toReturn.GetRequestStream());
43+
webRequest.ContentType = "application/json"; //TODO Needed?
44+
webRequest.MediaType = "UTF-8";
45+
var bytes = GithubSharp.Core.Base.JsonConverter.ToJsonBytes<TInputType>
46+
(ModelToSend);
4747

48+
webRequest.ContentLength = bytes.Length;
49+
var stream = webRequest.GetRequestStream();
50+
stream.Write(bytes, 0, bytes.Length);
51+
stream.Close();
4852

49-
return toReturn;
53+
return webRequest;
5054
}
5155
}
5256
}

Core/GithubRequestWithReturnType.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public GithubRequestWithReturnType (Core.Services.ILogProvider logProvider,
1313
: base(logProvider, cacheProvider, authProvider, path)
1414
{
1515
}
16+
1617
public GithubRequestWithReturnType (Core.Services.ILogProvider logProvider,
1718
Core.Services.ICacheProvider cacheProvider,
1819
Core.Services.IAuthProvider authProvider,
@@ -32,7 +33,6 @@ public override bool IsCached (string uri)
3233
return null;
3334
}
3435

35-
3636
public IGithubResponseWithReturnType<TReturnType> GetResponseWithReturnType()
3737
{
3838
var baseResult = base.GetResponse();
@@ -49,10 +49,8 @@ public IGithubResponseWithReturnType<TReturnType> GetResponseWithReturnType()
4949

5050
try
5151
{
52-
baseWithReturnType.Result = ServiceStack.Text.JsonSerializer
53-
.DeserializeFromString<TReturnType>(baseWithReturnType.Response);
54-
// baseWithReturnType.Result = JsonConverter
55-
// .FromJson<TReturnType>(baseWithReturnType.Response);
52+
baseWithReturnType.Result = JsonConverter
53+
.FromJson<TReturnType>(baseWithReturnType.Response);
5654
}
5755
catch (Exception error)
5856
{
@@ -62,6 +60,7 @@ public IGithubResponseWithReturnType<TReturnType> GetResponseWithReturnType()
6260

6361
return baseWithReturnType;
6462
}
63+
6564
}
6665
}
6766

Core/GithubResponse.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public interface IGithubResponse
1111
string LinkPrevious { get; set; }
1212
string LinkFirst { get; set; }
1313
string LinkLast { get; set; }
14+
int? StatusCode {get;set;}
15+
string StatusText {get;set;}
1416
}
1517

1618
public interface IGithubResponseWithReturnType<TResultType> : IGithubResponse
@@ -28,6 +30,8 @@ public class GithubResponse : IGithubResponse
2830
public string LinkPrevious { get; set; }
2931
public string LinkFirst { get; set; }
3032
public string LinkLast { get; set; }
33+
public int? StatusCode {get;set;}
34+
public string StatusText {get;set;}
3135
}
3236

3337
public class GithubResponseWithReturnType<TResultType> : GithubResponse, IGithubResponseWithReturnType<TResultType>

Core/Models/BasicUser.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace GithubSharp.Core.Models
4+
{
5+
[Serializable]
6+
public class BasicUser
7+
{
8+
public BasicUser ()
9+
{
10+
}
11+
12+
public string login { get; set; }
13+
public int id { get; set; }
14+
public string avatar_url { get; set; }
15+
public string gravatar_id { get; set; }
16+
public string url { get; set; }
17+
18+
}
19+
}
20+

Core/Models/ChangeStatus.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace GithubSharp.Core.Models
4+
{
5+
[Serializable]
6+
public class ChangeStatus
7+
{
8+
public ChangeStatus ()
9+
{
10+
}
11+
public int deletions { get; set; }
12+
public int additions { get; set; }
13+
public int total { get; set; }
14+
}
15+
}
16+

Core/Models/Commit.cs

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Runtime.Serialization;
4-
5-
namespace GithubSharp.Core.Models
6-
{
7-
public class Commit
8-
{
9-
public List<CommmitParent> parents { get; set; }
10-
11-
public Person author { get; set; }
12-
13-
public string url { get; set; }
14-
15-
public string sha { get; set; }
16-
17-
public string message { get; set; }
18-
19-
public CommmitParent tree { get; set; }
20-
21-
public Person committer { get; set; }
22-
}
23-
24-
public class CommmitParent
25-
{
26-
public string sha { get; set; }
27-
28-
public string url { get; set; }
29-
}
30-
31-
public class Person
32-
{
33-
public string name { get; set; }
34-
35-
public DateTime date { get; set; }
36-
37-
public string email { get; set; }
38-
}
39-
40-
[DataContract]
41-
public class SingleFileCommit : Commit
42-
{
43-
[DataMember(Name = "added")]
44-
public IEnumerable<SingleFileCommitFileReference> Added { get; set; }
45-
46-
[DataMember(Name = "removed")]
47-
public IEnumerable<SingleFileCommitFileReference> Removed { get; set; }
48-
49-
[DataMember(Name = "modified")]
50-
public IEnumerable<SingleFileCommitFileReference> Modified { get; set; }
51-
}
52-
53-
[DataContract]
54-
public class SingleFileCommitFileReference
55-
{
56-
[DataMember(Name = "filename")]
57-
public string Filename { get; set; }
58-
}
59-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.Serialization;
4+
5+
namespace GithubSharp.Core.Models
6+
{
7+
public class Commit
8+
{
9+
public List<CommmitParent> parents { get; set; }
10+
11+
public Person author { get; set; }
12+
13+
public string url { get; set; }
14+
15+
public string sha { get; set; }
16+
17+
public string message { get; set; }
18+
19+
public CommmitParent tree { get; set; }
20+
21+
public Person committer { get; set; }
22+
}
23+
24+
public class CommmitParent
25+
{
26+
public string sha { get; set; }
27+
28+
public string url { get; set; }
29+
}
30+
31+
public class Person
32+
{
33+
public string name { get; set; }
34+
35+
public DateTime date { get; set; }
36+
37+
public string email { get; set; }
38+
}
39+
40+
[DataContract]
41+
public class SingleFileCommit : Commit
42+
{
43+
[DataMember(Name = "added")]
44+
public IEnumerable<SingleFileCommitFileReference> Added { get; set; }
45+
46+
[DataMember(Name = "removed")]
47+
public IEnumerable<SingleFileCommitFileReference> Removed { get; set; }
48+
49+
[DataMember(Name = "modified")]
50+
public IEnumerable<SingleFileCommitFileReference> Modified { get; set; }
51+
}
52+
53+
[DataContract]
54+
public class SingleFileCommitFileReference
55+
{
56+
[DataMember(Name = "filename")]
57+
public string Filename { get; set; }
58+
}
59+
}

0 commit comments

Comments
 (0)