/
GroupShareClient.cs
145 lines (123 loc) · 5.85 KB
/
GroupShareClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Sdl.Community.GroupShareKit.Clients;
using Sdl.Community.GroupShareKit.Clients.TranslationMemory;
using Sdl.Community.GroupShareKit.Helpers;
using Sdl.Community.GroupShareKit.Http;
namespace Sdl.Community.GroupShareKit
{
/// <summary>
/// A Client for the GroupShare API v1.
/// </summary>
public class GroupShareClient: IGroupShareClient
{
public const string ManagementApi = "ManagementRestApi";
public const string ProjectApi = "ProjectServerRestApi";
public const string MultiTermApi = "MultiTermRestApi";
public const string TmServerApi = "TMServerRestApi";
public static IEnumerable<string> AllScopes =
new[]
{
ManagementApi,
ProjectApi,
MultiTermApi,
TmServerApi
};
/// <summary>
/// Create a new instance of the GroupShare API v1 client pointing to the specified baseAddress.
/// </summary>
/// <param name="credentialStore">Provides credentials to the client when making requests</param>
/// <param name="baseAddress">
/// The address to point this client to.</param>
public GroupShareClient(ICredentialStore credentialStore, Uri baseAddress)
: this(new Connection(baseAddress, credentialStore))
{
}
/// <summary>
/// Create a new instance of the GroupShare API v1 client using the specified connection.
/// </summary>
/// <param name="connection">The underlying <seealso cref="IConnection"/> used to make requests</param>
public GroupShareClient(IConnection connection)
{
Ensure.ArgumentNotNull(connection,"connection");
Connection = connection;
var apiConnection = new ApiConnection(connection);
Project = new ProjectClient(apiConnection);
User = new UserClient(apiConnection);
ProjectsTemplates = new ProjectTemplatesClient(apiConnection);
Organization = new OrganizationClient(apiConnection);
Authenticate = new AuthenticateClient(apiConnection);
Role = new RoleClient(apiConnection);
Permission = new PermissionClient(apiConnection);
ModuleClient = new ModuleClient(apiConnection);
FileVersion = new FileVersionClient(apiConnection);
License = new LicenseClient(apiConnection);
TranslationMemories = new TranslationMemoriesClient(apiConnection);
TermBase = new TermBasesClient(apiConnection);
}
/// <summary>
/// Creates a new instance of the GroupShare API v1 client that has the authorization token configured
/// </summary>
/// <param name="user">Provides the user for which to obtain the token.</param>
/// <param name="password">Provides the password for the specified user.</param>
/// <param name="baseAddress">The address to point this client to.</param>
/// <param name="scopes">The token scope(s).</param>
/// <returns></returns>
public static async Task<string> GetRequestToken(string user, string password, Uri baseAddress,
IEnumerable<string> scopes)
{
var credentials = new Credentials(user, password);
var inMemoryCredentials = new InMemoryCredentialStore(credentials);
var tokenGroupShareClient = new GroupShareClient(inMemoryCredentials, baseAddress);
var authorization = await tokenGroupShareClient.Authenticate.Post(scopes);
return authorization.Token;
}
public static async Task<GroupShareClient> AuthenticateClient(string token,string user, string password, Uri baseAddress,
IEnumerable<string> scopes)
{
var credentials = new Credentials(token, user, password);
var inMemoryCredentials = new InMemoryCredentialStore(credentials);
var groupShareClient = new GroupShareClient(inMemoryCredentials, baseAddress);
return groupShareClient;
}
public IProjectClient Project { get; }
public ITranslationMemoriesClient TranslationMemories { get; set; }
public IUserClient User { get; }
public IModuleClient ModuleClient { get; set; }
public IOrganizationClient Organization { get; set; }
public IFileVersion FileVersion { get; set; }
public IProjectsTemplateClient ProjectsTemplates { get; set; }
public IAuthenticateClient Authenticate { get; set; }
public IRoleClient Role { get; set; }
public IPermissionClient Permission { get; set; }
public ILicense License { get; set; }
public ITermBases TermBase { get; set; }
/// <summary>
/// Convenience property for getting and setting credentials.
/// </summary>
/// <remarks>
/// You can use this property if you only have a single hard-coded credential. Otherwise, pass in an
/// <see cref="ICredentialStore"/> to the constructor.
/// Setting this property will change the <see cref="ICredentialStore"/> to use
/// the default <see cref="InMemoryCredentialStore"/> with just these credentials.
/// </remarks>
public Credentials Credentials
{
get { return Connection.Credentials; }
set
{
Ensure.ArgumentNotNull(value, "value");
Connection.Credentials = value;
}
}
/// <summary>
/// The base address of the GroupShare API.
/// </summary>
public Uri BaseAddress => Connection.BaseAddress;
/// <summary>
/// Provides a client connection to make rest requests to HTTP endpoints.
/// </summary>
public IConnection Connection { get; }
}
}