Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit d51f5f5

Browse files
committed
Added GraphQL to GitHub.Api.
And associated factory/keychain classes.
1 parent 7300754 commit d51f5f5

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@
4646
</PropertyGroup>
4747
<Import Project="$(SolutionDir)\src\common\signing.props" />
4848
<ItemGroup>
49+
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
50+
<HintPath>..\..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
51+
<Private>True</Private>
52+
</Reference>
53+
<Reference Include="Octokit.GraphQL, Version=0.0.1.0, Culture=neutral, PublicKeyToken=0be8860aee462442, processorArchitecture=MSIL">
54+
<HintPath>..\..\packages\Octokit.GraphQL.0.0.1\lib\netstandard1.1\Octokit.GraphQL.dll</HintPath>
55+
<Private>True</Private>
56+
</Reference>
57+
<Reference Include="Octokit.GraphQL.Core, Version=0.0.1.0, Culture=neutral, PublicKeyToken=0be8860aee462442, processorArchitecture=MSIL">
58+
<HintPath>..\..\packages\Octokit.GraphQL.0.0.1\lib\netstandard1.1\Octokit.GraphQL.Core.dll</HintPath>
59+
<Private>True</Private>
60+
</Reference>
4961
<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
5062
<HintPath>..\..\packages\Serilog.2.5.0\lib\net46\Serilog.dll</HintPath>
5163
<Private>True</Private>
@@ -66,6 +78,9 @@
6678
<Link>ApiClientConfiguration_User.cs</Link>
6779
</Compile>
6880
<Compile Include="ApiClientConfiguration_User.cs" Condition="$(Buildtype) != 'Internal'" />
81+
<Compile Include="GraphQLKeychainCredentialStore.cs" />
82+
<Compile Include="IGraphQLClientFactory.cs" />
83+
<Compile Include="GraphQLClientFactory.cs" />
6984
<Compile Include="IKeychain.cs" />
7085
<Compile Include="ILoginManager.cs" />
7186
<Compile Include="IOAuthCallbackListener.cs" />
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.ComponentModel.Composition;
3+
using System.Threading.Tasks;
4+
using GitHub.Models;
5+
using GitHub.Primitives;
6+
using Octokit.GraphQL;
7+
8+
namespace GitHub.Api
9+
{
10+
/// <summary>
11+
/// Creates GraphQL <see cref="Octokit.GraphQL.IConnection"/>s for querying the
12+
/// GitHub GraphQL API.
13+
/// </summary>
14+
[Export(typeof(IGraphQLClientFactory))]
15+
[PartCreationPolicy(CreationPolicy.Shared)]
16+
public class GraphQLClientFactory : IGraphQLClientFactory
17+
{
18+
readonly IKeychain keychain;
19+
readonly IProgram program;
20+
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="GraphQLClientFactory"/> class.
23+
/// </summary>
24+
/// <param name="keychain">The <see cref="IKeychain"/> to use.</param>
25+
/// <param name="program">The program details.</param>
26+
[ImportingConstructor]
27+
public GraphQLClientFactory(IKeychain keychain, IProgram program)
28+
{
29+
this.keychain = keychain;
30+
this.program = program;
31+
}
32+
33+
/// <inheirtdoc/>
34+
public Task<Octokit.GraphQL.IConnection> CreateConnection(HostAddress address)
35+
{
36+
var credentials = new GraphQLKeychainCredentialStore(keychain, address);
37+
var header = new ProductHeaderValue(program.ProductHeader.Name, program.ProductHeader.Version);
38+
return Task.FromResult<Octokit.GraphQL.IConnection>(new Connection(header, credentials));
39+
}
40+
}
41+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using GitHub.Extensions;
4+
using GitHub.Primitives;
5+
using Octokit.GraphQL;
6+
7+
namespace GitHub.Api
8+
{
9+
/// <summary>
10+
/// An Octokit.GraphQL credential store that reads from an <see cref="IKeychain"/>.
11+
/// </summary>
12+
public class GraphQLKeychainCredentialStore : ICredentialStore
13+
{
14+
readonly IKeychain keychain;
15+
readonly HostAddress address;
16+
17+
public GraphQLKeychainCredentialStore(IKeychain keychain, HostAddress address)
18+
{
19+
Guard.ArgumentNotNull(keychain, nameof(keychain));
20+
Guard.ArgumentNotNull(address, nameof(keychain));
21+
22+
this.keychain = keychain;
23+
this.address = address;
24+
}
25+
26+
public async Task<string> GetCredentials()
27+
{
28+
var userPass = await keychain.Load(address).ConfigureAwait(false);
29+
return userPass?.Item2;
30+
}
31+
}
32+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Threading.Tasks;
2+
using GitHub.Primitives;
3+
4+
namespace GitHub.Api
5+
{
6+
/// <summary>
7+
/// Creates GraphQL <see cref="Octokit.GraphQL.IConnection"/>s for querying the
8+
/// GitHub GraphQL API.
9+
/// </summary>
10+
public interface IGraphQLClientFactory
11+
{
12+
/// <summary>
13+
/// Creates a new <see cref="Octokit.GraphQL.IConnection"/>.
14+
/// </summary>
15+
/// <param name="address">The address of the server.</param>
16+
/// <returns>A task returning the created connection.</returns>
17+
Task<Octokit.GraphQL.IConnection> CreateConnection(HostAddress address);
18+
}
19+
}

src/GitHub.Api/packages.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
4+
<package id="Octokit.GraphQL" version="0.0.1" targetFramework="net461" />
35
<package id="Serilog" version="2.5.0" targetFramework="net461" />
46
</packages>

0 commit comments

Comments
 (0)