/// <summary> /// Import or Update Api By swagger Url /// 1,check Api exists by {aid} /// 2,if exists update Api /// 3,if not exists import Api /// 4,if set authorizationServerId, update Api authenticationSettings /// </summary> /// <param name="aid">Api id, not null</param> /// <param name="suffix">Api service suffix, not null</param> /// <param name="swaggerUrl">Swagger doc url, not null</param> /// <param name="productIds">Product id collection</param> /// <param name="authorizationServerId">authorize server Id</param> /// <param name="protocols">protocols</param> /// <param name="scope">scope</param> /// <param name="openid">openid</param> /// <returns></returns> public async Task <HttpResponseMessage> ImportOrUpdateAsync( string aid, string suffix, string swaggerUrl, string[] productIds = null, string authorizationServerId = null, List <string> protocols = null, string scope = null, string openid = null) { if (string.IsNullOrWhiteSpace(aid) || string.IsNullOrWhiteSpace(suffix) || string.IsNullOrWhiteSpace(swaggerUrl)) { return(new HttpResponseMessage(System.Net.HttpStatusCode.NotModified)); } var path = $"/apis/{aid}"; var queryParams = new Dictionary <string, string> { { "import", "false" }, { "path", suffix }, }; #region content var body = new JObject(); body["id"] = path; body["link"] = swaggerUrl; body["protocols"] = JsonConvert.SerializeObject( protocols == null || protocols.Count < 1 ? new List <string> { "https" } : protocols); var content = new StringContent(body.ToString(), Encoding.UTF8, "application/vnd.swagger.link+json"); #endregion #region headerItems Dictionary <string, string> headerItems = null; if (await MetadataAsync(aid)) { headerItems = new Dictionary <string, string>() { { "If-Match", "*" } }; } #endregion var result = await RequestAsync(path, HttpMethod.Put.Method, queryParams, content, headerItems); if (result.IsSuccessStatusCode) { #region Add Api to Product // 如果为空,设置到Unlimited 这个Product里,否则需要带上subkey才能call if (productIds != null && productIds.Length > 0) { foreach (var productId in productIds) { try { var addApiResult = await prdService.AddApiAsync(productId, aid); } catch { } } } #endregion #region Update Api OAuth2 Settings try { var oAuth2result = await UpdateOAuth2Async(aid, authorizationServerId, scope, openid); } catch { } #endregion } return(result); }
/// <summary> /// Import or Update Api By swagger Url /// 1,check Api exists by {aid} /// 2,if exists update Api /// 3,if not exists import Api /// 4,if set authorizationServerId, update Api authenticationSettings /// </summary> /// <param name="aid">Api id, not null</param> /// <param name="suffix">Api service suffix, not null</param> /// <param name="swaggerUrl">Swagger doc url, not null</param> /// <param name="productId">Product id</param> /// <param name="authorizationServerId">authorize server Id</param> /// <param name="scope">scope</param> /// <param name="openid">openid</param> /// <returns></returns> public async Task <bool> ImportOrUpdateAsync( string aid, string suffix, string swaggerUrl, string productId = null, string authorizationServerId = null, string scope = null, string openid = null) { if (string.IsNullOrWhiteSpace(aid) || string.IsNullOrWhiteSpace(suffix) || string.IsNullOrWhiteSpace(swaggerUrl)) { return(false); } var path = $"/apis/{aid}"; var queryParams = new Dictionary <string, string> { { "import", "false" }, { "path", suffix }, }; #region content var body = new JObject(); body["id"] = path; body["link"] = swaggerUrl; var content = new StringContent(body.ToString(), Encoding.UTF8, "application/vnd.swagger.link+json"); #endregion #region headerItems Dictionary <string, string> headerItems = null; if (await MetadataAsync(aid)) { headerItems = new Dictionary <string, string>() { { "If-Match", "*" } }; } #endregion var result = await RequestAsync(path, HttpMethod.Put.Method, queryParams, content, headerItems); if (result.IsSuccessStatusCode) { #region Add Api to Product // 如果为空,设置到Unlimited 这个Product里,否则需要带上subkey才能call if (!string.IsNullOrWhiteSpace(productId)) { var addApiResult = await prdService.AddApiAsync(productId, aid); } #endregion #region Update Api OAuth2 Settings var oAuth2result = await UpdateOAuth2Async(aid, authorizationServerId, scope, openid); #endregion } return(result.IsSuccessStatusCode); }