mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 10:56:31 +00:00
throttle reimplemented
This commit is contained in:
@@ -10,7 +10,7 @@ public interface IAzaionApi
|
||||
{
|
||||
ApiCredentials Credentials { get; }
|
||||
User CurrentUser { get; }
|
||||
T? Get<T>(string url);
|
||||
void UpdateOffsets(UserQueueOffsets offsets);
|
||||
Stream GetResource(string filename);
|
||||
}
|
||||
|
||||
@@ -28,13 +28,27 @@ public class AzaionApi(HttpClient client, ICache cache, ApiCredentials credentia
|
||||
() => Get<User>("currentUser"));
|
||||
if (user == null)
|
||||
throw new Exception("Can't get current user");
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private HttpResponseMessage Send(HttpRequestMessage request, CancellationToken ct = default)
|
||||
public Stream GetResource(string filename)
|
||||
{
|
||||
var hardware = cache.GetFromCache(SecurityConstants.HARDWARE_INFO_KEY, hardwareService.GetHardware);
|
||||
|
||||
var response = Send(new HttpRequestMessage(HttpMethod.Post, $"/resources/get/{credentials.Folder}")
|
||||
{
|
||||
Content = new StringContent(JsonConvert.SerializeObject(new { filename, credentials.Password, hardware }), Encoding.UTF8, APP_JSON)
|
||||
});
|
||||
return response.Content.ReadAsStream();
|
||||
}
|
||||
|
||||
public void UpdateOffsets(UserQueueOffsets offsets)
|
||||
{
|
||||
Put($"/users/queue-offsets/{CurrentUser.Email}", offsets);
|
||||
}
|
||||
|
||||
private HttpResponseMessage Send(HttpRequestMessage request)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_jwtToken))
|
||||
Authorize();
|
||||
@@ -61,15 +75,20 @@ public class AzaionApi(HttpClient client, ICache cache, ApiCredentials credentia
|
||||
throw new Exception($"Failed: {response.StatusCode}! Result: {content}");
|
||||
}
|
||||
|
||||
public Stream GetResource(string filename)
|
||||
private T? Get<T>(string url)
|
||||
{
|
||||
var hardware = cache.GetFromCache(SecurityConstants.HARDWARE_INFO_KEY, hardwareService.GetHardware);
|
||||
var response = Send(new HttpRequestMessage(HttpMethod.Get, url));
|
||||
var stream = response.Content.ReadAsStream();
|
||||
var json = new StreamReader(stream).ReadToEnd();
|
||||
return JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
|
||||
var response = Send(new HttpRequestMessage(HttpMethod.Post, $"/resources/get/{credentials.Folder}")
|
||||
private void Put<T>(string url, T obj)
|
||||
{
|
||||
Send(new HttpRequestMessage(HttpMethod.Put, url)
|
||||
{
|
||||
Content = new StringContent(JsonConvert.SerializeObject(new { filename, credentials.Password, hardware }), Encoding.UTF8, APP_JSON)
|
||||
Content = new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, APP_JSON)
|
||||
});
|
||||
return response.Content.ReadAsStream();
|
||||
}
|
||||
|
||||
private void Authorize()
|
||||
@@ -107,11 +126,5 @@ public class AzaionApi(HttpClient client, ICache cache, ApiCredentials credentia
|
||||
}
|
||||
}
|
||||
|
||||
public T? Get<T>(string url)
|
||||
{
|
||||
var response = Send(new HttpRequestMessage(HttpMethod.Get, url));
|
||||
var stream = response.Content.ReadAsStream();
|
||||
var json = new StreamReader(stream).ReadToEnd();
|
||||
return JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user