Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
phspies committed May 17, 2022
1 parent 3e8f9d9 commit 57b77b2
Show file tree
Hide file tree
Showing 1,481 changed files with 93,702 additions and 2,862 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var testvm = await vsphere.VcenterSubModule.VMModule.GetAsync(vms[0].Vm);
var resourcepools = await vsphere.VcenterSubModule.ResourcePoolModule.ListAsync();
var rpdetails = await vsphere.VcenterSubModule.ResourcePoolModule.GetAsync(resourcepools[0].ResourcePool);
```

## Custom JsonSerializerSettings
Expand All @@ -37,6 +36,5 @@ vSphereClient vsphere = new vSphereClient(
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
Converters = new List<JsonConverter>() { new Newtonsoft.Json.Converters.StringEnumConverter() }
});
```

33 changes: 29 additions & 4 deletions SDKGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ static void Main(string[] args)
Helpers.Register(nameof(GetResponseName), GetResponseName);
Helpers.Register(nameof(GetParameterPascalCase), GetParameterPascalCase);
Helpers.Register(nameof(SetDefaultValue), SetDefaultValue);
Helpers.Register(nameof(GetEvaluateResponse), GetEvaluateResponse);




Expand Down Expand Up @@ -107,7 +109,7 @@ static void Main(string[] args)
{
currentEnum.EnumName = models.Single(x => x.Key.Equals(model.Key, StringComparison.Ordinal)).Value;
}


currentEnum.Description = model.Value.Description;
foreach (var enumValue in model.Value.Enumeration)
Expand Down Expand Up @@ -146,7 +148,7 @@ static void Main(string[] args)
{
rootmodule = apiDoc.Key,
modules = tags.Select(x => x.pascalname),
}, Path.Combine(projectDirectory,$"{apiDoc.Key}Module.cs")); ;
}, Path.Combine(projectDirectory, $"{apiDoc.Key}Module.cs")); ;


}
Expand All @@ -159,7 +161,30 @@ static void Main(string[] args)
var test = models.Where(x => x.Value.Equals("VcenterVMsummary", StringComparison.InvariantCultureIgnoreCase));
}


public static string RemoveSpecialCharacters(string str)
{
return Regex.Replace(str, "[^a-zA-Z0-9_. ]+", "", RegexOptions.Compiled);
}
private static void GetEvaluateResponse(RenderContext context, IList<object> arguments, IDictionary<string, object> options, RenderBlock fn, RenderBlock inverse)
{
int code = int.Parse((string)((DictionaryEntry)arguments[0]).Key);
OpenApiResponse response = (OpenApiResponse)((DictionaryEntry)arguments[0]).Value;
if (!(200 <= code && code <= 300))
{
context.Write("else if ((int)response.StatusCode == " + code + ") { throw new vSphereException(" + $"\"{ RemoveSpecialCharacters(response.Description)}\"" + ", (int)response.StatusCode, response.Content, response.Headers, response.ErrorException); }");
}
else
{
if (String.IsNullOrWhiteSpace(response.Description) && response.Schema == null)
{
context.Write("if (200 <= (int)response.StatusCode && (int)response.StatusCode <= 300) { }");
}
else
{
context.Write("if (200 <= (int)response.StatusCode && (int)response.StatusCode <= 300) { return response.Data; }");
}
}
}

private string ConvertToString(object value, System.Globalization.CultureInfo cultureInfo)
{
Expand Down Expand Up @@ -597,7 +622,7 @@ private static string GetDotNetType(JsonSchemaProperty jsonType, IList<object> a
{

}

switch (jsonType.Type)
{
case JsonObjectType.Boolean:
Expand Down
10 changes: 5 additions & 5 deletions SDKGenerator/Templates/Module.cs.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ using Newtonsoft.Json;
using System.Net;
using vspheresdk;
using vspheresdk.{{rootmodule}}.Models;
using vspheresdk.{{rootmodule}}.Models.Enums;

namespace vspheresdk.{{rootmodule}}.Modules
{
Expand Down Expand Up @@ -45,11 +46,10 @@ namespace vspheresdk.{{rootmodule}}.Modules
{{/Item4.Parameters}}
request.Resource = {{GetMethodName .}}ServiceURL.ToString();
RestResponse{{GetOperationReturnType . "bare"}} response = await restClient.ExecuteTaskAsyncWithPolicy{{GetOperationReturnType . "bare"}}(request, cancellationToken, timeout, retry);
if (response.StatusCode != HttpStatusCode.OK)
{
var message = "HTTP {{GetOperationHttpMethod .}} operation to " + {{GetMethodName .}}ServiceURL.ToString() + " did not complete successfull";
throw new vSphereException(message, (int)response.StatusCode, response.Content, response.Headers, response.ErrorException);
}
{{#each Item4.responses}}
{{GetEvaluateResponse .}}
{{/each Item4.responses}}
else { throw new vSphereException(response.ErrorMessage, (int)response.StatusCode, response.Content, response.Headers, response.ErrorException); }
{{GetOperationReturnType . "return"}}
}
{{/operations}}
Expand Down
4 changes: 4 additions & 0 deletions SDKGenerator/Templates/RootModule.cs.template
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ namespace vspheresdk
{
await AuthenticationHelper.LogoutAsync(sessionRestClient, cancellationToken, timeout, retry);
}
public async Task<string> RetrieveAsync()
{
return await AuthenticationHelper.RetrieveAsync(sessionRestClient, cancellationToken, timeout, retry);
}
{{#submodules}}
public {{.}}SubModule {{.}}SubModule => new {{.}}SubModule(tokenRestClient, cancellationToken, timeout, retry);
{{/submodules}}
Expand Down
Loading

0 comments on commit 57b77b2

Please sign in to comment.