Skip to content

Commit 74cd12e

Browse files
committed
Refactored into enums to prevent typos (DOT NIET STYLE)
1 parent c680ba8 commit 74cd12e

File tree

12 files changed

+279
-159
lines changed

12 files changed

+279
-159
lines changed

Core/API/Gist.cs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using GithubSharp.Core.Services;
22
using System.Collections.Generic;
3+
using GithubSharp.Core.Base;
34

45
namespace GithubSharp.Core.API
56
{
@@ -20,29 +21,29 @@ public Gist(
2021
string.Format(
2122
"users/{0}/gists",
2223
username),
23-
"GET").Result;
24+
GithubSharpHttpVerbs.GET).Result;
2425
}
2526
public IEnumerable<Models.Gist> List()
2627
{
2728
return base.Get<IEnumerable<Models.Gist>>(
2829
string.Format(
2930
"users/{0}/gists",
3031
base.AuthProvider.Username),
31-
"GET").Result;
32+
GithubSharpHttpVerbs.GET).Result;
3233
}
3334

3435
public IEnumerable<Models.Gist> Public()
3536
{
3637
return base.Get<IEnumerable<Models.Gist>>(
3738
"gists/public",
38-
"GET").Result;
39+
GithubSharpHttpVerbs.GET).Result;
3940
}
4041

4142
public IEnumerable<Models.Gist> Starred()
4243
{
4344
return base.Get<IEnumerable<Models.Gist>>(
4445
"gists/starred",
45-
"GET").Result;
46+
GithubSharpHttpVerbs.GET).Result;
4647
}
4748

4849
public Models.Gist Get(string id)
@@ -51,14 +52,14 @@ public Models.Gist Get(string id)
5152
string.Format(
5253
"gists/{0}",
5354
id),
54-
"GET").Result;
55+
GithubSharpHttpVerbs.GET).Result;
5556
}
5657

5758
public Models.Gist Create(Models.GistToCreateOrEdit gist)
5859
{
5960
return base.Get<Models.Gist, Models.GistToCreateOrEdit>(
6061
"gists",
61-
"POST",
62+
GithubSharpHttpVerbs.POST,
6263
gist).Result;
6364
}
6465

@@ -68,7 +69,7 @@ public Models.Gist Edit(string id, Models.GistToCreateOrEdit gist)
6869
string.Format(
6970
"gists/{0}",
7071
id),
71-
"PATCH",
72+
GithubSharpHttpVerbs.PATCH,
7273
gist).Result;
7374
}
7475

@@ -78,7 +79,7 @@ public void Star(string id)
7879
string.Format(
7980
"gists/{0}/star",
8081
id),
81-
"PUT");
82+
GithubSharpHttpVerbs.PUT);
8283
}
8384

8485
public void Unstar(string id)
@@ -87,7 +88,7 @@ public void Unstar(string id)
8788
string.Format(
8889
"gists/{0}/star",
8990
id),
90-
"DELETE");
91+
GithubSharpHttpVerbs.DELETE);
9192
}
9293

9394
public bool HasStar(string id)
@@ -96,7 +97,7 @@ public bool HasStar(string id)
9697
string.Format(
9798
"gists/{0}/star",
9899
id),
99-
"GET"
100+
GithubSharpHttpVerbs.GET
100101
).StatusCode == 204;
101102
}
102103

@@ -106,7 +107,7 @@ public Models.Gist Fork(string id)
106107
string.Format(
107108
"gists/{0}/fork",
108109
id),
109-
"POST").Result;
110+
GithubSharpHttpVerbs.POST).Result;
110111
}
111112

112113
public void Delete(string id)
@@ -115,46 +116,46 @@ public void Delete(string id)
115116
string.Format(
116117
"gists/{0}",
117118
id),
118-
"DELETE");
119+
GithubSharpHttpVerbs.DELETE);
119120

120121
}
121122

122-
public IEnumerable<Models.GistComment> ListComments (string gistid)
123+
public IEnumerable<Models.Comment> ListComments (string gistid)
123124
{
124-
return base.Get<IEnumerable<Models.GistComment>>(
125+
return base.Get<IEnumerable<Models.Comment>>(
125126
string.Format(
126127
"gists/{0}/comments",
127128
gistid),
128-
"GET").Result;
129+
GithubSharpHttpVerbs.GET).Result;
129130
}
130131

131-
public Models.GistComment CreateComment(string gistid, Models.GistCommentForCreationOrEdit comment)
132+
public Models.Comment CreateComment(string gistid, Models.CommentForCreationOrEdit comment)
132133
{
133-
return base.Get<Models.GistComment, Models.GistCommentForCreationOrEdit>(
134+
return base.Get<Models.Comment, Models.CommentForCreationOrEdit>(
134135
string.Format(
135136
"gists/{0}/comments",
136137
gistid),
137-
"POST",
138+
GithubSharpHttpVerbs.POST,
138139
comment).Result;
139140
}
140141

141142

142-
public Models.GistComment GetComment(string gistcommentid)
143+
public Models.Comment GetComment(string gistcommentid)
143144
{
144-
return base.Get<Models.GistComment>(
145+
return base.Get<Models.Comment>(
145146
string.Format(
146147
"gists/comments/{0}",
147148
gistcommentid),
148-
"GET").Result;
149+
GithubSharpHttpVerbs.GET).Result;
149150
}
150151

151-
public Models.GistComment EditComment(string gistcommentid, Models.GistCommentForCreationOrEdit comment)
152+
public Models.Comment EditComment(string gistcommentid, Models.CommentForCreationOrEdit comment)
152153
{
153-
return base.Get<Models.GistComment, Models.GistCommentForCreationOrEdit>(
154+
return base.Get<Models.Comment, Models.CommentForCreationOrEdit>(
154155
string.Format(
155156
"gists/comments/{0}",
156157
gistcommentid),
157-
"PATCH",
158+
GithubSharpHttpVerbs.PATCH,
158159
comment).Result;
159160
}
160161

@@ -164,7 +165,7 @@ public void DeleteComment(string gistcommentid)
164165
string.Format(
165166
"gists/comments/{0}",
166167
gistcommentid),
167-
"DELETE");
168+
GithubSharpHttpVerbs.DELETE);
168169
}
169170

170171

Core/Base/Enums.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
3+
namespace GithubSharp.Core.Base
4+
{
5+
public enum GithubSharpHttpVerbs
6+
{
7+
GET,
8+
POST,
9+
PATCH,
10+
DELETE,
11+
PUT,
12+
HEAD,
13+
OPTIONS
14+
}
15+
16+
public enum GithubSharpMimeTypes
17+
{
18+
Raw,
19+
Text,
20+
Html,
21+
Full,
22+
Json
23+
}
24+
25+
public static class EnumHelper
26+
{
27+
public static string ToString(this GithubSharpHttpVerbs verb)
28+
{
29+
switch (verb)
30+
{
31+
case GithubSharpHttpVerbs.GET : return "GET";
32+
case GithubSharpHttpVerbs.POST : return "POST";
33+
case GithubSharpHttpVerbs.PATCH : return "PATCH";
34+
case GithubSharpHttpVerbs.DELETE : return "DELETE";
35+
case GithubSharpHttpVerbs.PUT : return "PUT";
36+
case GithubSharpHttpVerbs.HEAD : return "HEAD";
37+
default : return "OPTIONS";
38+
}
39+
}
40+
41+
public static string ToString(this GithubSharpMimeTypes mime)
42+
{
43+
var mimeBase = "application/vnd.github.{0}+json";
44+
switch (mime)
45+
{
46+
case GithubSharpMimeTypes.Raw : return string.Format(mimeBase, "raw");
47+
case GithubSharpMimeTypes.Text : return string.Format(mimeBase, "text");
48+
case GithubSharpMimeTypes.Html : return string.Format(mimeBase, "html");
49+
case GithubSharpMimeTypes.Full : return string.Format(mimeBase, "full");
50+
default : return "application/vnd.github.json";
51+
}
52+
}
53+
}
54+
}
55+

Core/Base/GithubApiBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public GithubApiBase (
2020

2121
protected IGithubResponse Get(
2222
string Path,
23-
string Method)
23+
GithubSharpHttpVerbs Method)
2424
{
2525
return new GithubRequest(
2626
LogProvider,
@@ -32,7 +32,7 @@ protected IGithubResponse Get(
3232

3333
protected IGithubResponseWithReturnType<TReturnType> Get<TReturnType>(
3434
string Path,
35-
string Method)
35+
GithubSharpHttpVerbs Method)
3636
where TReturnType : class
3737
{
3838
return new GithubRequestWithReturnType<TReturnType>(
@@ -46,7 +46,7 @@ protected IGithubResponseWithReturnType<TReturnType> Get<TReturnType>(
4646

4747
protected IGithubResponseWithReturnType<TReturnType> Get<TReturnType, TInputType>(
4848
string Path,
49-
string Method,
49+
GithubSharpHttpVerbs Method,
5050
TInputType ToSend)
5151
where TReturnType : class
5252
{

Core/Core.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@
114114
<Compile Include="GithubRequestWithInputAndReturnType.cs" />
115115
<Compile Include="Base\GithubApiBase.cs" />
116116
<Compile Include="GithubException.cs" />
117+
<Compile Include="Base\Enums.cs" />
118+
<Compile Include="Models\Comment.cs" />
117119
</ItemGroup>
118120
<ItemGroup>
119121
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">

Core/GithubRequest.cs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace GithubSharp.Core
55
{
66
public interface IGithubRequest
77
{
8-
string Method { get;}
8+
GithubSharpHttpVerbs Method { get;}
99
string Path {get;set;}
1010
IGithubResponse GetResponse();
1111
Core.Services.ILogProvider LogProvider {get;set;}
@@ -14,6 +14,7 @@ public interface IGithubRequest
1414
int? PagingRequestAmount {get;set;}
1515
int? PagingCurrentPage {get;set;}
1616
System.Net.HttpWebRequest PrepareWebRequest(System.Net.HttpWebRequest webRequest);
17+
GithubSharpMimeTypes GithubSharpMimeType {get;set;}
1718
}
1819

1920

@@ -22,17 +23,23 @@ public class GithubRequest : IGithubRequest
2223
public Core.Services.ILogProvider LogProvider {get;set;}
2324
public Core.Services.IAuthProvider AuthProvider {get;set;}
2425
public Core.Services.ICacheProvider CacheProvider {get;set;}
25-
public virtual string Method { get;set; }
26+
public virtual GithubSharpHttpVerbs Method { get;set; }
2627
public virtual string Path { get;set; }
2728
public virtual int? PagingRequestAmount { get;set; }
2829
public virtual int? PagingCurrentPage { get;set; }
30+
public virtual GithubSharpMimeTypes GithubSharpMimeType {get;set;}
2931

3032
public GithubRequest(
3133
Core.Services.ILogProvider logProvider,
3234
Core.Services.ICacheProvider cacheProvider,
3335
Core.Services.IAuthProvider authProvider,
3436
string path)
35-
:this(logProvider, cacheProvider, authProvider, path, "GET")
37+
:this(
38+
logProvider,
39+
cacheProvider,
40+
authProvider,
41+
path,
42+
GithubSharpHttpVerbs.GET)
3643
{
3744
}
3845

@@ -41,7 +48,7 @@ public GithubRequest(
4148
Core.Services.ICacheProvider cacheProvider,
4249
Core.Services.IAuthProvider authProvider,
4350
string path,
44-
string method)
51+
GithubSharpHttpVerbs method)
4552
:this(
4653
logProvider,
4754
cacheProvider,
@@ -60,9 +67,32 @@ public GithubRequest(
6067
Core.Services.ICacheProvider cacheProvider,
6168
Core.Services.IAuthProvider authProvider,
6269
string path,
63-
string method,
70+
GithubSharpHttpVerbs method,
6471
int? pagingLimit,
6572
int? currentPage)
73+
:this(
74+
logProvider,
75+
cacheProvider,
76+
authProvider,
77+
path,
78+
method,
79+
pagingLimit,
80+
currentPage,
81+
GithubSharpMimeTypes.Json
82+
)
83+
{
84+
85+
}
86+
87+
public GithubRequest(
88+
Core.Services.ILogProvider logProvider,
89+
Core.Services.ICacheProvider cacheProvider,
90+
Core.Services.IAuthProvider authProvider,
91+
string path,
92+
GithubSharpHttpVerbs method,
93+
int? pagingLimit,
94+
int? currentPage,
95+
GithubSharpMimeTypes mime)
6696
{
6797
LogProvider = logProvider;
6898
CacheProvider = cacheProvider;
@@ -71,6 +101,7 @@ public GithubRequest(
71101
Method = method;
72102
PagingCurrentPage = currentPage;
73103
PagingRequestAmount = pagingLimit;
104+
GithubSharpMimeType = mime;
74105
}
75106

76107
public virtual bool IsCached(string uri)
@@ -91,7 +122,7 @@ public virtual void Cache(IGithubResponse response, string uri)
91122

92123
public virtual System.Net.HttpWebRequest PrepareWebRequest(System.Net.HttpWebRequest webRequest)
93124
{
94-
webRequest.Accept = "application/vnd.github+json";
125+
webRequest.Accept = EnumHelper.ToString(GithubSharpMimeType);
95126

96127
return webRequest;
97128
}
@@ -167,7 +198,7 @@ public virtual IGithubResponse GetResponse ()
167198

168199
var webRequest = System.Net.HttpWebRequest.Create(new Uri(uri)) as System.Net.HttpWebRequest;
169200

170-
webRequest.Method = Method;
201+
webRequest.Method = EnumHelper.ToString(Method);
171202

172203
var authResult = AuthProvider.PreRequestAuth(this, webRequest);
173204

Core/GithubRequestWithInputAndReturnType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using GithubSharp.Core.Services;
3+
using GithubSharp.Core.Base;
34

45
namespace GithubSharp.Core
56
{
@@ -24,7 +25,7 @@ public GithubRequestWithInputAndReturnType (
2425
ICacheProvider cacheProvider,
2526
IAuthProvider authProvider,
2627
string path,
27-
string method,
28+
GithubSharpHttpVerbs method,
2829
TInputType input)
2930
:base(logProvider,
3031
cacheProvider,

0 commit comments

Comments
 (0)