/// <summary>Get latest swagger definitions from cloud.</summary> /// <param name="config">Contains config info, including profile to use</param> /// <returns>Success or failure</returns> public bool GetSwagger(CentrifyCliConfig config) { try { Ccli.ConditionalWrite($"Fetching latest swagger definitions from cloud.", config.Silent); // Doesn't require auth InitializeClient(config.Profile.URL); var runTask = PlaceCall("/vfslow/lib/api/swagger.json", ""); runTask.Wait(); Tuple <Runner.ResultCode, string> callResult = runTask.Result; if (callResult.Item1 == Runner.ResultCode.Success) { // Write item2 to swagger.json file. There's no JSON to process. using (StreamWriter file = File.CreateText(config.SwaggerDirectory)) { file.Write(callResult.Item2); } return(true); } else { Ccli.WriteErrorText($"Error fetching swagger definitions from cloud: {callResult}"); } } catch (Exception e) { Ccli.WriteErrorText($"Exception fetching swagger definitions from cloud: {e.Message}"); } return(false); }
/// <summary>Loads the swagger.json file from, typically, depot2\Cloud\Lib\Api\swagger.json /// Builds API Resource from it.</summary> /// <param name="config">Contains config info, including profile to use</param> /// <returns>Success or failure</returns> public bool LoadSwagger(CentrifyCliConfig config) { string swaggerPath = config.SwaggerDirectory; if (String.IsNullOrEmpty(swaggerPath)) { Ccli.WriteErrorText("No swagger path defined in config."); return(false); } Ccli.ConditionalWrite($"Loading swagger definitions from {swaggerPath}", config.Silent); bool exists = File.Exists(swaggerPath); if ((!exists) || (File.GetCreationTimeUtc(swaggerPath) < (DateTime.UtcNow - SWAGGER_REFRESH))) { // Fetch from cloud if no swagger or swagger is 'old' if (!GetSwagger(config)) { if (exists) { Ccli.ConditionalWrite($"Using existing swagger defintiions.", config.Silent); } else { Ccli.WriteErrorText($"No swagger definitions available from cloud."); return(false); } } } JObject swagSet = null; try { using (StreamReader file = File.OpenText(swaggerPath)) using (JsonTextReader reader = new JsonTextReader(file)) { swagSet = (JObject)JToken.ReadFrom(reader); } } catch (Exception e) { Ccli.WriteErrorText($"Error loading swagger definitions from {swaggerPath}: {e.Message}"); return(false); } JArray calls = new JArray(); foreach (JProperty path in swagSet["paths"]) { JProperty restPath = new JProperty("path", (string)path.Name); JProperty group = new JProperty("group", (string)path.First["post"]["tags"].First); string[] pathBits = ((string)path.Name).Split(new char[] { '/' }); JProperty desc = new JProperty("api", pathBits[pathBits.Length - 1]); JProperty reference = new JProperty("reference", (string)path.First["post"]["summary"]); string parameters = "{"; int paramCount = 0; JToken pathParams = path.First["post"]["parameters"].First; if (pathParams != null) { try { foreach (JProperty prop in pathParams["schema"]["properties"]) { if (paramCount++ > 0) { parameters += ",\n"; } parameters += " \"" + (string)prop.Name + "\": \"\""; } } catch { try { foreach (JToken tok in pathParams.First) { if (tok is JProperty prop) { if (paramCount++ > 0) { parameters += ",\n"; } parameters += " \"" + (string)prop + "\": \"\""; } } } catch (Exception e) { Ccli.WriteErrorText($"Error parsing swagger properties from {swaggerPath}: {e.Message}"); return(false); }; } } if (paramCount > 0) { parameters += "\n"; } parameters += "}"; JProperty sample = new JProperty("sample", parameters); JObject thisCall = new JObject { restPath, // path == REST endpoint sample, // parameters group, // Grouping of calls reference, // Reference (not really API, misnamed) desc // Name of call }; calls.Add(thisCall); } m_apiCalls = new JObject(); JProperty callWrapper = new JProperty("apis", calls); m_apiCalls.Add(callWrapper); return(true); }