-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClient.cs
More file actions
159 lines (133 loc) · 4.59 KB
/
Client.cs
File metadata and controls
159 lines (133 loc) · 4.59 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using Dev.Dres.Client.Api;
using Dev.Dres.Client.Client;
using Dev.Dres.Client.Model;
namespace csharp
{
public class Client
{
static void RunExample()
{
var config = new Configuration
{
BasePath = Settings.BasePath
};
// === Setup ===
// Initialize user api client
var userApi = new UserApi(config);
// initialize evaluation run info client
var runInfoApi = new ClientRunInfoApi(config);
// initialize submission api client
var submissionApi = new SubmissionApi(config);
// initialize logging api client
var logApi = new LogApi(config);
// === Handshake ===
Println($"Try to log in to {config.BasePath} with user {Settings.User}");
// Login request
UserDetails login = null;
try
{
login = userApi.PostApiV1Login(new LoginRequest(Settings.User, Settings.Pass));
}
catch(ApiException ex)
{
Console.Error.WriteLine("Could not log in due to exception: "+ex.Message);
return;
}
// Login successful
Println($"Successfully logged in.\n" +
$"user: '{login.Username}'\n" +
$"role: '{login.Role}'\n" +
$"session: '{login.SessionId}'");
// Store session token for future requests
var sessionid = login.SessionId;
Thread.Sleep(1000);
// === Example 1: Evaluation Runs Info ===
// Retrieve current evaluation runs
var currentRuns = runInfoApi.GetApiV1ClientRunInfoList(sessionid);
Println($"Found {currentRuns.Runs.Count} ongoing evaluation runs");
currentRuns.Runs.ForEach(info =>
{
Println($"{info.Name} ({info.Id}): {info.Status}");
if (info.Description != null)
{
Println(info.Description);
}
Println();
});
// === Example 2: Submission ===
// Setup Submission
SuccessfulSubmissionsStatus submissionResponse = null;
try
{
submissionResponse = submissionApi.GetApiV1Submit(
session: sessionid,
collection: null, //does not usually need to be set
item: "some_item_name",//item which is to be submitted
frame: null,// for items with temporal components, such as video
shot: null,// only one of the time fields needs to be set.
timecode: "00:00:10:00", //in this case, we use the timestamp in the form HH:MM:SS:FF
text: null //in case the task is not targeting a particular content object but plaintext
);
}
catch (ApiException ex)
{
switch (ex.ErrorCode)
{
case 401:
Console.Error.WriteLine("There was an authentication error during submission. Check the session id.");
break;
case 404:
Console.Error.WriteLine("There is currently no active task which would accept submissions.");
break;
default:
Console.Error.WriteLine($"Something unexpected went wrong during the submission: '{ex.Message}'.");
return;
}
}
if (submissionResponse != null && submissionResponse.Status)
{
Println("The submission was successfully sent to the server.");
// === Example 3: Log ===
logApi.PostApiV1LogResult(
session: sessionid,
new QueryResultLog(DateTimeOffset.Now.ToUnixTimeMilliseconds(),
sortType: "list",
results: CreateResultList(),
events: new List<QueryEvent>(),
resultSetAvailability: "")
);
}
// === Graceful logout ===
Thread.Sleep(1000);
var logout = userApi.GetApiV1Logout(sessionid);
if (logout.Status)
{
Println("Successfully logged out");
}
else
{
Println("Error during logout "+logout.Description);
}
}
/// <summary>
/// Dummy data for example log
/// </summary>
/// <returns></returns>
private static List<QueryResult> CreateResultList()
{
List<QueryResult> list = new List<QueryResult>();
list.Add(new QueryResult("some_item_name", segment: 3, score: 0.9, rank: 1));
list.Add(new QueryResult("some_item_name", segment: 5, score: 0.85, rank: 2));
list.Add(new QueryResult("some_ohter_item_name", segment: 12, score: 0.76, rank: 3));
return list;
}
private static void Println(String msg = "")
{
Console.Out.WriteLine(msg);
}
}
}