-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOmniaClient.cs
112 lines (92 loc) · 5.45 KB
/
OmniaClient.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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Threading.Tasks;
using omnia_blazor_demo.Shared.Model;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Microsoft.AspNetCore.JsonPatch;
using Newtonsoft.Json;
using Spike_ExternalWebApp.Shared.Model;
using System.Linq;
namespace omnia_blazor_demo
{
public class OmniaClient
{
public readonly HttpClient http;
public OmniaClient(HttpClient http)
{
this.http = http;
}
public async Task<Temporary> CreateTemporary(EntityMetadata entityMetadata)
{
var tmpResponse = await http.PostAsJsonAsync<EmptyQuery>($"{entityMetadata.Tenant}/{entityMetadata.Environment}/application/{entityMetadata.Entity}/{entityMetadata.DataSource}/Temporaries", new EmptyQuery());
if (!tmpResponse.IsSuccessStatusCode)
throw new Exception("An error occurred when trying to create the temporary.");
return JsonConvert.DeserializeObject<Temporary>(await tmpResponse.Content.ReadAsStringAsync());
}
public async Task<EntityTemporary<T>> CreateTemporary<T>(EntityMetadata entityMetadata, string code)
{
var tmpResponse = await http.PostAsJsonAsync<EmptyQuery>($"{entityMetadata.Tenant}/{entityMetadata.Environment}/application/{entityMetadata.Entity}/{entityMetadata.DataSource}/{code}/CreateTemporary", new EmptyQuery());
if (!tmpResponse.IsSuccessStatusCode)
throw new Exception("An error occurred when trying to create the temporary.");
return JsonConvert.DeserializeObject<EntityTemporary<T>>(await tmpResponse.Content.ReadAsStringAsync());
}
public async Task<Temporary> UpdateTemporary(EntityMetadata entityMetadata, string temporaryId, JsonPatchDocument jsonPatchDocument)
{
var uri = $"{entityMetadata.Tenant}/{entityMetadata.Environment}/application/{entityMetadata.Entity}/{entityMetadata.DataSource}/Temporaries/{temporaryId}";
var requestBodyInJson = JsonConvert.SerializeObject(jsonPatchDocument);
var buffer = System.Text.Encoding.UTF8.GetBytes(requestBodyInJson);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var tmpResponse = await http.PatchAsync(uri, byteContent);
if (!tmpResponse.IsSuccessStatusCode)
throw new Exception("An error occurred when trying to patch the temporary.");
return JsonConvert.DeserializeObject<Temporary>(await tmpResponse.Content.ReadAsStringAsync());
}
public async Task<TemporarySaveResponse> SaveTemporary(EntityMetadata entityMetadata, string temporaryId)
{
var uri = $"{entityMetadata.Tenant}/{entityMetadata.Environment}/application/{entityMetadata.Entity}/{entityMetadata.DataSource}/Temporaries/{temporaryId}/Save";
var tmpResponse = await http.PostAsJsonAsync<EmptyQuery>(uri, new EmptyQuery());
if (!tmpResponse.IsSuccessStatusCode)
{
var response = JsonConvert.DeserializeObject<ErrorResponse>(await tmpResponse.Content.ReadAsStringAsync());
if (response == null)
throw new Exception("An error occurred when trying to save the temporary.");
else
{
var errors = response.errors == null || response.errors.Count == 0 ? response.message : string.Join(Environment.NewLine, response.errors.Select(e => $"{e.name}: {e.message}").ToList());
throw new Exception($"Error save the temporary: {errors}");
}
}
return JsonConvert.DeserializeObject<TemporarySaveResponse>(await tmpResponse.Content.ReadAsStringAsync());
}
public async Task<List<T>> ExecuteQuery<T>(QueryMetadata queryMetadata)
{
var uri = $"{queryMetadata.Tenant}/{queryMetadata.Environment}/application/queries/{queryMetadata.Query}/{queryMetadata.DataSource}?page={queryMetadata.Page}&pageSize={queryMetadata.PageSize}";
var response = await http.PostAsJsonAsync<OmniaQueryRequestBody>(uri, new OmniaQueryRequestBody
{
parameters = queryMetadata.parameters
});
if (!response.IsSuccessStatusCode)
throw new Exception("An error occurred when trying to execute the query.");
return JsonConvert.DeserializeObject<List<T>>(await response.Content.ReadAsStringAsync());
}
public async void GetMe()
{
var tmpResponse = await http.GetAsync($"me");
if (!tmpResponse.IsSuccessStatusCode)
throw new Exception("An error occurred when trying to get me.");
}
public async Task<DeleteEntityResponse> DeleteEntity(EntityMetadata entityMetadata, string code)
{
var uri = $"{entityMetadata.Tenant}/{entityMetadata.Environment}/application/{entityMetadata.Entity}/{entityMetadata.DataSource}/{code}";
var tmpResponse = await http.DeleteAsync(uri);
if (!tmpResponse.IsSuccessStatusCode)
throw new Exception("An error occurred when trying to delete entity.");
return JsonConvert.DeserializeObject<DeleteEntityResponse>(await tmpResponse.Content.ReadAsStringAsync());
}
}
}