-
Notifications
You must be signed in to change notification settings - Fork 0
/
TranskribusClient.cs
114 lines (99 loc) · 3.61 KB
/
TranskribusClient.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
using System.Xml.Linq;
using Flurl.Http;
using Flurl.Http.Xml;
using Newtonsoft.Json;
class TranskribusClient
{
private FlurlClient Client { get; set; }
private string Username { get; set; }
private string Password { get; set; }
private Throttle Throttle { get; } = new Throttle(TimeSpan.FromSeconds(2));
private AuthResponse AuthResponse { get; set; }
private DateTimeOffset AuthResponseRetrieved { get; set; }
private const string ApiUri = "https://transkribus.eu/processing/v1/processes";
private const string AuthUri = "https://account.readcoop.eu/auth/realms/readcoop/protocol/openid-connect/token";
public TranskribusClient(string username, string password)
{
Username = username;
Password = password;
Client = new FlurlClient(ApiUri).BeforeCall(call => Authorize(call.Request));
}
private async Task Authorize(IFlurlRequest request)
{
if (AuthResponse is null || AuthResponseRetrieved.AddSeconds(AuthResponse.RefreshExpiresIn) < DateTimeOffset.Now.AddMinutes(-5))
{
AuthResponse = await AuthUri.PostUrlEncodedAsync(new
{
username = Username,
password = Password,
grant_type = "password",
client_id = "processing-api-client"
}).ReceiveJson<AuthResponse>();
AuthResponseRetrieved = DateTimeOffset.Now;
}
else if (AuthResponseRetrieved.AddSeconds(AuthResponse.ExpiresIn - 30) < DateTimeOffset.Now)
{
AuthResponse = await AuthUri.PostUrlEncodedAsync(new
{
refresh_token = AuthResponse.RefreshToken,
grant_type = "refresh_token",
client_id = "processing-api-client"
}).ReceiveJson<AuthResponse>();
AuthResponseRetrieved = DateTimeOffset.Now;
}
request.WithHeader("Authorization", "Bearer " + AuthResponse.AccessToken);
}
public async Task<int> Process(int htrId, string imageBase64)
{
return await Throttle.RunThrottled(async () =>
{
var response = await Client.Request().PostJsonAsync(new
{
config = new
{
textRecognition = new
{
htrId
}
},
image = new
{
base64 = imageBase64
}
}).ReceiveJson();
return (int)response.processId;
});
}
public async Task<string> GetProcessStatus(int processId)
{
return await Throttle.RunThrottled(async () =>
{
var response = await Client.Request(processId).GetJsonAsync();
return response.status;
});
}
public async Task<XDocument> GetAltoXml(int processId)
{
return await Throttle.RunThrottled(() => Client.Request(processId, "alto").GetXDocumentAsync());
}
}
// generated by quicktype.io
class AuthResponse
{
[JsonProperty("access_token")]
public string AccessToken { get; set; }
[JsonProperty("expires_in")]
public long ExpiresIn { get; set; }
[JsonProperty("refresh_expires_in")]
public long RefreshExpiresIn { get; set; }
[JsonProperty("refresh_token")]
public string RefreshToken { get; set; }
[JsonProperty("token_type")]
public string TokenType { get; set; }
[JsonProperty("not-before-policy")]
public long NotBeforePolicy { get; set; }
[JsonProperty("session_state")]
public Guid SessionState { get; set; }
[JsonProperty("scope")]
public string Scope { get; set; }
}