|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +#if UNITY_EDITOR |
| 7 | +using UnityEditor; |
| 8 | +#endif |
| 9 | +using UnityEngine; |
| 10 | +using UnityOpenApi; |
| 11 | +using UnityOpenApi.ApiModels.OpenApi; |
| 12 | + |
| 13 | + |
| 14 | +public class ApiAssetsGenerator |
| 15 | +{ |
| 16 | + public void GenerateOrUpdateApiAssets(string basePath, OpenApi3 openApi3, bool considerOptionHttpWord = false) |
| 17 | + { |
| 18 | + |
| 19 | + string apiConfigAssetFilename = openApi3.info.title; |
| 20 | + string localApiDirectory = CreateDirectoryIfNotExists(basePath, openApi3.info.title); |
| 21 | + |
| 22 | + // create paths subdirectory |
| 23 | + string resDirectoryPath = CreateDirectoryIfNotExists(localApiDirectory, "resources"); |
| 24 | + |
| 25 | + // create data model directory |
| 26 | + string dataModelsDirectory = CreateDirectoryIfNotExists(localApiDirectory, "data_model"); |
| 27 | + |
| 28 | + |
| 29 | + // Create/update api config |
| 30 | + ApiConfig apiConfig = GetOrCreateAsset<ApiConfig>(localApiDirectory, apiConfigAssetFilename); |
| 31 | + |
| 32 | + apiConfig.rawData = openApi3; |
| 33 | + |
| 34 | + //apiConfig.baseUri = openApi3.servers[0].url.Replace( |
| 35 | + // "/{basePath}" |
| 36 | + // , openApi3.servers[0].variables.basePath._default); |
| 37 | + |
| 38 | + // for now consider only first server |
| 39 | + apiConfig.serverVariableValues = new List<ApiConfig.ServerVariableValue>(); |
| 40 | + foreach (var sv in openApi3.servers[0].variables) |
| 41 | + { |
| 42 | + apiConfig.serverVariableValues.Add(new ApiConfig.ServerVariableValue() |
| 43 | + { |
| 44 | + varName = sv.Key, |
| 45 | + varValue = sv.Value.@default |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + // Authorization resources |
| 50 | + apiConfig.authorizers = new List<ApiAuthorizer>(); |
| 51 | + if (openApi3.components.securitySchemes != null) |
| 52 | + { |
| 53 | + foreach (var s in openApi3.components.securitySchemes) |
| 54 | + { |
| 55 | + switch (s.Value.@in) |
| 56 | + { |
| 57 | + case OpenApi3ParameterIn.query: |
| 58 | + break; |
| 59 | + case OpenApi3ParameterIn.path: |
| 60 | + break; |
| 61 | + case OpenApi3ParameterIn.header: |
| 62 | + string fName = apiConfigAssetFilename + "_auth_" + s.Key; |
| 63 | + ApiHeaderAuthorizer hAuth = GetOrCreateAsset<ApiHeaderAuthorizer>(localApiDirectory, fName); |
| 64 | + hAuth.authName = s.Key; |
| 65 | + hAuth.securityScheme = s.Value; |
| 66 | + apiConfig.authorizers.Add(hAuth); |
| 67 | + break; |
| 68 | + case OpenApi3ParameterIn.cookie: |
| 69 | + break; |
| 70 | + default: |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // create/update individual api resources |
| 77 | + foreach (var openApiPath in openApi3.paths) |
| 78 | + { |
| 79 | + |
| 80 | + string resSubdirectory = CreateDirectoryIfNotExists(resDirectoryPath, openApiPath.Key); |
| 81 | + |
| 82 | + //string resUri = apiConfig.baseUri; |
| 83 | + //if (openApiPath.Key != "/") resUri += openApiPath.Key; |
| 84 | + |
| 85 | + foreach (var res in openApiPath.Value) |
| 86 | + { |
| 87 | + string fName = apiConfig.name |
| 88 | + + openApiPath.Key.Replace("/", "_") |
| 89 | + + "_" |
| 90 | + + res.Key.ToString().ToUpper(); |
| 91 | + |
| 92 | + if (considerOptionHttpWord == false && res.Key == OpenApi3HttpWords.options) |
| 93 | + { |
| 94 | + Debug.Log("Skipping OPTION http resource for " + fName); |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + ApiResource apiResource = GetOrCreateAsset<ApiResource>(resSubdirectory, fName); |
| 99 | + |
| 100 | + |
| 101 | + apiResource.apiConfig = apiConfig; |
| 102 | + apiResource.httpWord = res.Key; |
| 103 | + apiResource.path = openApiPath.Key; |
| 104 | + apiResource.method = res.Value; |
| 105 | + apiResource.ResetParametersValues(); |
| 106 | + |
| 107 | + if (res.Value.security != null && res.Value.security.Count > 0) |
| 108 | + { |
| 109 | + string authName = res.Value.security[0].First().Key; |
| 110 | + apiResource.apiAuthorizer = apiConfig.GetAuthorizer(authName); |
| 111 | + if (apiResource.apiAuthorizer == null) |
| 112 | + { |
| 113 | + Debug.LogError("Can't find authorizer " + authName + " for " + apiResource.name); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + EditorUtility.SetDirty(apiResource); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + AssetDatabase.Refresh(); |
| 122 | + AssetDatabase.SaveAssets(); |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + private T GetOrCreateAsset<T>(string path, string subpath, string ext = ".asset", bool setDirty = true) where T : ScriptableObject |
| 127 | + { |
| 128 | + if (path[path.Length - 1] != '/') path += "/"; |
| 129 | + if (subpath[0] == '/') subpath = subpath.Substring(1); |
| 130 | + string assetFullPath = path + subpath + ext; |
| 131 | + T asset = AssetDatabase.LoadAssetAtPath<T>(assetFullPath); |
| 132 | + if (asset == null) |
| 133 | + { |
| 134 | + asset = ScriptableObject.CreateInstance<T>(); |
| 135 | + Debug.Log("Creating new asset at: " + assetFullPath); |
| 136 | + AssetDatabase.CreateAsset(asset, assetFullPath); |
| 137 | + } |
| 138 | + if (setDirty) |
| 139 | + { |
| 140 | + EditorUtility.SetDirty(asset); |
| 141 | + } |
| 142 | + return asset; |
| 143 | + } |
| 144 | + |
| 145 | + private string CreateDirectoryIfNotExists(params string[] paths) |
| 146 | + { |
| 147 | + string path = string.Empty; |
| 148 | + foreach (var p in paths) |
| 149 | + { |
| 150 | + if (string.IsNullOrEmpty(p)) |
| 151 | + { |
| 152 | + continue; |
| 153 | + } |
| 154 | + string pathPart = p; |
| 155 | + char separator = '/'; |
| 156 | + if (pathPart[0] != separator) |
| 157 | + { |
| 158 | + pathPart = separator + pathPart; |
| 159 | + } |
| 160 | + if (pathPart[pathPart.Length - 1] == separator) |
| 161 | + { |
| 162 | + pathPart = pathPart.Remove(pathPart.Length - 1); |
| 163 | + } |
| 164 | + path += pathPart; |
| 165 | + } |
| 166 | + // remove the initial slash |
| 167 | + path = path.Substring(1); |
| 168 | + if (Directory.Exists(path) == false) |
| 169 | + { |
| 170 | + Debug.Log("Creating directory: " + path); |
| 171 | + Directory.CreateDirectory(path); |
| 172 | + } |
| 173 | + return path; |
| 174 | + } |
| 175 | +} |
0 commit comments