Skip to content

Commit ae8a638

Browse files
committed
Implement header provider
Supplement the rest request factory with a header provider which adds its headers to a request.
1 parent a0cc458 commit ae8a638

6 files changed

Lines changed: 148 additions & 3 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using FluentAssertions;
2+
using NUnit.Framework;
3+
using RestSharp;
4+
using System;
5+
using System.Linq;
6+
using System.Text;
7+
using csharp_github_api.Core;
8+
9+
namespace csharp_github_api.IntegrationTests
10+
{
11+
[TestFixture]
12+
public class HeaderProviderTests
13+
{
14+
[Test]
15+
[Ignore("Not sure what default headers to provide yet.")]
16+
public void should_provide_default_headers()
17+
{
18+
var provider = new HeaderProvider();
19+
20+
var headers = provider.Headers;
21+
22+
headers.Should().NotBeEmpty("Default headers should be provided.");
23+
}
24+
25+
[Test]
26+
public void addheader_should_add_to_default_headers()
27+
{
28+
var provider = new HeaderProvider();
29+
var header = new Header("name", "value");
30+
provider.AddHeader(header);
31+
32+
provider.Headers.Contains(header).Should().BeTrue();
33+
}
34+
}
35+
}

csharp-github-api.IntegrationTests/csharp-github-api.IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
</ItemGroup>
8787
<ItemGroup>
8888
<Compile Include="Authentication\BasicAuthenticationTest.cs" />
89+
<Compile Include="HeaderProviderTests.cs" />
8990
<Compile Include="Properties\AssemblyInfo.cs" />
9091
<Compile Include="Properties\TestResources.Designer.cs">
9192
<AutoGen>True</AutoGen>

csharp-github-api/Core/Header.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace csharp_github_api.Core
7+
{
8+
public interface IHeader
9+
{
10+
string Name { get; set; }
11+
string Value { get; set; }
12+
}
13+
14+
public class Header : IHeader, IEquatable<Header>
15+
{
16+
public Header(string name, string value)
17+
{
18+
Name = name;
19+
Value = value;
20+
}
21+
22+
public bool Equals(Header other)
23+
{
24+
if (ReferenceEquals(null, other)) return false;
25+
if (ReferenceEquals(this, other)) return true;
26+
return string.Equals(Name, other.Name) && string.Equals(Value, other.Value);
27+
}
28+
29+
public override int GetHashCode()
30+
{
31+
unchecked
32+
{
33+
return ((Name != null ? Name.GetHashCode() : 0)*397) ^ (Value != null ? Value.GetHashCode() : 0);
34+
}
35+
}
36+
37+
public static bool operator ==(Header left, Header right)
38+
{
39+
return Equals(left, right);
40+
}
41+
42+
public static bool operator !=(Header left, Header right)
43+
{
44+
return !Equals(left, right);
45+
}
46+
47+
public string Name { get; set; }
48+
public string Value { get; set; }
49+
50+
public override bool Equals(object obj)
51+
{
52+
if (ReferenceEquals(null, obj)) return false;
53+
if (ReferenceEquals(this, obj)) return true;
54+
var other = obj as Header;
55+
return other != null && Equals(other);
56+
}
57+
}
58+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using RestSharp;
4+
5+
namespace csharp_github_api.Core
6+
{
7+
public interface IHeaderProvider
8+
{
9+
HashSet<IHeader> Headers { get; set; }
10+
void AddHeader(IHeader header);
11+
void PopulateHeaders(ref IRestRequest request);
12+
}
13+
14+
public class HeaderProvider : IHeaderProvider
15+
{
16+
private HashSet<IHeader> _headers = new HashSet<IHeader>();
17+
18+
public HashSet<IHeader> Headers
19+
{
20+
get { return _headers; }
21+
set { _headers = value; }
22+
}
23+
24+
public void AddHeader(IHeader header)
25+
{
26+
_headers.Add(header);
27+
}
28+
29+
public void PopulateHeaders(ref IRestRequest request)
30+
{
31+
if(request == null) throw new ArgumentNullException("request", "The request must not be null!");
32+
33+
foreach (var header in Headers)
34+
{
35+
request.AddHeader(header.Name, header.Value);
36+
}
37+
}
38+
}
39+
}

csharp-github-api/Resource/RestRequestFactory.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
using System;
22
using System.Collections.Generic;
33
using RestSharp;
4+
using csharp_github_api.Core;
45

56
namespace csharp_github_api.Resource
67
{
78
public class RestRequestFactory
89
{
9-
private RestRequestFactory()
10+
private static IHeaderProvider _headerProvider;
11+
12+
private RestRequestFactory(IHeaderProvider headerProvider = null)
1013
{
14+
_headerProvider = headerProvider;
1115
}
1216

1317
public static IRestRequest CreateRequest(Func<IRestRequest> request)
@@ -20,7 +24,7 @@ public static IRestRequest CreateRequest(string resource, Method method = Method
2024
return CreateRequest(
2125
() =>
2226
{
23-
var request = new RestRequest
27+
IRestRequest request = new RestRequest
2428
{
2529
Resource = resource,
2630
Method = method
@@ -33,6 +37,9 @@ public static IRestRequest CreateRequest(string resource, Method method = Method
3337
request.AddParameter(parameter);
3438
}
3539

40+
if (_headerProvider != null)
41+
_headerProvider.PopulateHeaders(ref request);
42+
3643
return request;
3744
}
3845
);
@@ -43,7 +50,7 @@ public static IRestRequest CreateRequest(string resource, Method method = Method
4350
return CreateRequest(
4451
() =>
4552
{
46-
var request = new RestRequest
53+
IRestRequest request = new RestRequest
4754
{
4855
Resource = resource,
4956
Method = method
@@ -56,6 +63,9 @@ public static IRestRequest CreateRequest(string resource, Method method = Method
5663
request.AddUrlSegment(kvp.Key, kvp.Value);
5764
}
5865

66+
if (_headerProvider != null)
67+
_headerProvider.PopulateHeaders(ref request);
68+
5969
return request;
6070
}
6171
);

csharp-github-api/csharp-github-api.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
</Compile>
6363
<Compile Include="Api.cs" />
6464
<Compile Include="Authentication\AuthenticatingInterceptor.cs" />
65+
<Compile Include="Core\Header.cs" />
66+
<Compile Include="Core\HeaderProvider.cs" />
6567
<Compile Include="Exceptions\GitHubAuthorizationException.cs" />
6668
<Compile Include="Exceptions\GitHubResponseException.cs" />
6769
<Compile Include="Extensions\AuthenticatingExtension.cs" />

0 commit comments

Comments
 (0)