-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranskribusClient.cs
More file actions
134 lines (118 loc) · 4.4 KB
/
TranskribusClient.cs
File metadata and controls
134 lines (118 loc) · 4.4 KB
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
using System.Xml.Linq;
using Flurl.Http;
using Flurl.Http.Configuration;
using Flurl.Http.Xml;
using Newtonsoft.Json;
using System.Text.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 Task<int> Process(int htrId, string imageBase64)
{
var options = new MicroservicePageOptions
{
HtrId = htrId
};
return Process(options, imageBase64);
}
public async Task<int> Process(MicroservicePageOptions options, string imageBase64)
{
return await Throttle.RunThrottled(async () =>
{
var request = Client.Request();
var response = await request.PostJsonAsync(new
{
config = new
{
textRecognition = new
{
htrId = options.HtrId
},
lineDetection = new
{
modelId = options.LineDetectionModelId,
minimalBaselineLength = options.MinimalBaselineLength,
baselineAccuracyThreshold = options.BaselineAccuracyThreshold,
maxDistForMerging = options.MaxDistForMerging,
numTextRegions = options.NumTextRegions
}
},
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; }
}