-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_jobs.cs
192 lines (144 loc) · 6.22 KB
/
api_jobs.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* snapWONDERS OpenAPI Specification
* API version: 1.0
*
* Copyright (c) snapWONDERS.com, All rights reserved 2023
*
* Author: Kenneth Springer (https://kennethbspringer.au)
*
* All the snapWONDERS API services is available over the Clearnet / **Web** and Dark Web **Tor** and **I2P**
* Read details: https://snapwonders.com/snapwonders-openapi-specification
*
*/
using System.Text;
using System.Text.Json;
namespace snapwonders_csharp_client
{
public class ApiJobs
{
private static readonly HttpClient httpClient = new();
// Create an analyse job and display results
public static void AnalyseJob(string pathFileName)
{
// Create upload media url and upload file in data chunks
string urlUploadMedia = ApiUpload.CreateUploadMediaUrl(pathFileName);
ApiUpload.UploadMedia(urlUploadMedia, pathFileName);
// Create an analyse job url
string urlJobStatus = CreateAnalyseJob(urlUploadMedia);
// Track the job status, wait until analyse job is completed
string urlJobResults = String.Empty;
while (true)
{
JobStatus jsonJobStatus = GetJobStatus(urlJobStatus);
// Wait for the job to be completed
if (jsonJobStatus.status.Equals(ApiHelper.JOB_STATUS_WAITING)
|| jsonJobStatus.status.Equals(ApiHelper.JOB_STATUS_PROCESSING))
{
Console.WriteLine("INFO: Sleeping for a few seconds...");
Thread.Sleep(5000);
}
// If completed we break out
else if (jsonJobStatus.status.Equals(ApiHelper.JOB_STATUS_COMPLETED))
{
urlJobResults = jsonJobStatus.resultUrl;
break;
}
// Some unknown state?
else
{
LogHelper.LogAndExit(string.Format("ERROR: Analyse job failed with status:[{0}], message:[{1}]",
jsonJobStatus.status,
jsonJobStatus.message));
}
}
// Get and display results
string dataResults = GetJobResults(urlJobResults);
// NOTE: You can call getJobResults() for image url resources contained within the JSON result
Console.WriteLine(JsonPrettify(dataResults));
}
// Create an analyse job
private static string CreateAnalyseJob(string urlUploadMedia)
{
Console.WriteLine("CALL: createAnalyseJob()");
// Build up Json Analyse Job
JobAnalyse jobAnalyse = new()
{
key = Path.GetFileName(urlUploadMedia),
enableTips = true,
enableExtraAnalysis = true
};
string jsonJobAnalyse = JsonSerializer.Serialize(jobAnalyse);
// Call API to create the media url for uploading
HttpRequestMessage requestMessage = new(HttpMethod.Post,
ApiHelper.URL_SNAPWONDERS_API + ApiHelper.URL_JOB_CREATE_ANALYSE)
{
Content = new StringContent(jsonJobAnalyse, Encoding.UTF8, ApiHelper.HTTP_CONTENT_TYPE_JSON)
};
ApiHelper.AddApiHeaders(ref requestMessage);
Task<HttpResponseMessage> result = Task.Run(async () => await httpClient.SendAsync(requestMessage));
HttpResponseMessage response = result.Result;
Task<string> resultContent = Task.Run(response.Content.ReadAsStringAsync);
string jsonContent = resultContent.Result;
// Check POST status for errors
if (!response.IsSuccessStatusCode)
{
LogHelper.LogAndExit(string.Format("ERROR: Send POST request failed:[{0}]", jsonContent));
}
// Success - Extract the media url
string urlJobAnalyse = string.Empty;
IEnumerable<string> headers;
if (response.Headers.TryGetValues("Location", out headers))
{
urlJobAnalyse = headers.First();
}
if (urlJobAnalyse.Length <= 0)
{
LogHelper.LogAndExit("ERROR: Job URL extraction failed");
}
Console.WriteLine(string.Format("SUCCESS: Created analyse job located at url:[{0}]", urlJobAnalyse));
return urlJobAnalyse;
}
// Gets the job status
private static JobStatus GetJobStatus(string urlJobStatus)
{
Console.WriteLine("CALL: getJobStatus()");
HttpRequestMessage requestMessage = new(HttpMethod.Post, urlJobStatus);
ApiHelper.AddApiHeaders(ref requestMessage);
Task<HttpResponseMessage> result = Task.Run(async () => await httpClient.SendAsync(requestMessage));
HttpResponseMessage response = result.Result;
Task<string> resultContent = Task.Run(response.Content.ReadAsStringAsync);
string jsonContent = resultContent.Result;
// Check POST status for errors
if (!response.IsSuccessStatusCode)
{
LogHelper.LogAndExit(string.Format("Create analyse job failed:[{0}]", jsonContent));
}
JobStatus jobStatus = JsonSerializer.Deserialize<JobStatus>(jsonContent);
Console.WriteLine(string.Format("SUCCESS: Have job status:[{0}]", jobStatus.status));
return jobStatus;
}
// Gets the job results
private static string GetJobResults(string urlJobResults)
{
Console.WriteLine("CALL: getJobResult()");
HttpRequestMessage requestMessage = new(HttpMethod.Get, urlJobResults);
ApiHelper.AddApiHeaders(ref requestMessage);
Task<HttpResponseMessage> result = Task.Run(async () => await httpClient.SendAsync(requestMessage));
HttpResponseMessage response = result.Result;
Task<string> resultContent = Task.Run(response.Content.ReadAsStringAsync);
string jsonContent = resultContent.Result;
// Check POST status for errors
if (!response.IsSuccessStatusCode)
{
LogHelper.LogAndExit(string.Format("Create analyse job failed:[{0}]", jsonContent));
}
return jsonContent;
}
// Json prettify a JSON unknown object
private static string JsonPrettify(string json)
{
using var jDoc = JsonDocument.Parse(json);
return JsonSerializer.Serialize(jDoc, new JsonSerializerOptions { WriteIndented = true });
}
}
}