-
Notifications
You must be signed in to change notification settings - Fork 2
/
Schemas.cs
184 lines (165 loc) · 6.9 KB
/
Schemas.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
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using EncompassREST.Exceptions;
using EncompassREST.HelperClasses;
using EncompassREST.JsonHelpers;
using Newtonsoft.Json.Linq;
namespace EncompassREST
{
public class Schemas
{
private const string _apiPath = "encompass/v1/schema";
public Session Session { get; }
public Schemas(Session session)
{
Session = session;
}
public Task<string> GetSchemaAsync() => GetSchemaAsync(null, true);
public async Task<string> GetSchemaAsync(IList<string> entities, bool includeFieldExtensions)
{
var rp = new RequestParameters();
if (entities != null &&
entities.Count > 0)
{
rp.Add("entities", string.Join(",", entities));
}
rp.Add("includeFieldExtensions", includeFieldExtensions.ToString());
var message = new HttpRequestMessage(HttpMethod.Get, $"{_apiPath}/loan{rp}");
var response = await Session.RESTClient.SendAsync(message);
//await _Session.RESTClient.GetAsync(API_PATH + "/loan" + rp.ToString());
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return await response.Content.ReadAsStringAsync();
}
else
{
throw new RESTException(nameof(GetSchemaAsync), response);
}
}
public async Task<string> GetSchemaFieldAsync(string fieldId)
{
var message = new HttpRequestMessage(HttpMethod.Get, $"{_apiPath}/loan/{fieldId}");
//var response = await _Session.RESTClient.GetAsync(API_PATH + "/loan/" + FieldID);
var response = await Session.RESTClient.SendAsync(message);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return await response.Content.ReadAsStringAsync();
}
else
{
throw new RESTException(nameof(GetSchemaFieldAsync), response);
}
}
public async Task<string> GetFieldPathAsync(string fieldId)
{
string schemaJson;
var returnPath = new StringBuilder();
try
{
schemaJson = await GetSchemaFieldAsync(fieldId);
}
catch (RESTException re)
{
throw new RESTException("GetSchemaFieldAsync", re.Response);
}
var jsonMain = JObject.Parse(schemaJson);
var entityTypes = jsonMain["entity_types"];
foreach (var token in entityTypes)
{
var p = (JProperty)token;
returnPath.Append(p.Name + ".");
}
//jsonMain["entity_types"].Last["properties"]
var fieldIdTokens = jsonMain.FindTokens("field_id");
if (fieldIdTokens.Count == 1)
{
var path = fieldIdTokens.FirstOrDefault().Path;
var itemsInPath = path.Split('.');
returnPath.Append(itemsInPath.GetValue(itemsInPath.Count() - 2));
}
else
{
var fieldInstanceTokens = jsonMain.FindTokens("field_instances");
var path = fieldInstanceTokens.FirstOrDefault().Path;
var itemsInPath = path.Split('.');
returnPath.Append(itemsInPath.GetValue(itemsInPath.Count() - 2));
}
return returnPath.ToString();
}
public async Task GenerateClassFilesFromSchemaAsync(string destinationPath, string @namespace)
{
var RawSchema = await GetSchemaAsync();
var jo = JToken.Parse(RawSchema);
var entities = jo["entity_types"];
foreach (var jt in entities.Children())
await GenerateClassFileFromSchemaAsync(destinationPath, @namespace, ((JProperty)jt).Name, jo);
}
private async Task GenerateClassFileFromSchemaAsync(string destinationPath, string @namespace, string section, JToken schemaToken)
{
string entity;
var sb = new StringBuilder();
var partial = (section == "Loan") ? "partial " : "";
sb.Append(
$@"using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace {@namespace}
{{
public {partial}class {section}
{{
");
var sectionProperties = schemaToken["entity_types"][section]["properties"];
foreach (var sectionToken in sectionProperties.Children())
{
var variableProperty = (JProperty)sectionToken;
var vName = variableProperty.Name;
vName = vName.Substring(0, 1).ToLower() + vName.Substring(1); //set proper case
var vType = schemaToken["entity_types"][section]["properties"][variableProperty.Name]["type"];
switch(vType.ToString())
{
case "string":
case "uuid":
sb.AppendLine($" public string {vName} {{ get; set; }}");
break;
case "decimal":
case "bool":
case "int":
sb.AppendLine($" public {vType}? {vName} {{ get; set; }}");
break;
case "date":
case "datetime":
sb.AppendLine($" public DateTime? {vName} {{ get; set; }}");
break;
//validate reserialization of these elements
case "set":
case "list":
entity = schemaToken["entity_types"][section]["properties"][variableProperty.Name]["element_type"].ToString();
entity = entity.Substring(0, 1).ToUpper() + entity.Substring(1);
sb.AppendLine($" public List<{entity}> {vName} {{ get; set; }}");
break;
case "entity":
entity = schemaToken["entity_types"][section]["properties"][variableProperty.Name]["element_type"].ToString();
entity = entity.Substring(0, 1).ToUpper() + entity.Substring(1);
sb.AppendLine($" public {entity} {vName} {{ get; set; }}");
break;
default:
sb.AppendLine($" public PROBLEM<{variableProperty.Name}> {vName} {{ get; set; }}");
break;
}
}
sb.Append(
@" }
}");
using (var sw = new StreamWriter(Path.Combine(destinationPath, section + ".cs")))
{
await sw.WriteAsync(sb.ToString());
}
}
}
}