I want to import APIs from swagger/open api specification to Azure Api Management (APIM) using Microsoft.Azure.Management.ApiManagement
it so easy to do it with powershel:
Import-AzApiManagementApi -Context $ApiMgmtContext -SpecificationFormat "Swagger" -SpecificationPath $swaggerOutputFilePath -Path $apiPath -ApiType "Http" -Protocol "Https"
I have a technical need to do it pragmatically with C#
using Azure SDK through Microsoft.Azure.Management.ApiManagement
I started from this post :
ServiceClientCredentials
to retrieve valid token and add it to headers authorizationpublic class AzureApiManagementServiceCredentials : ServiceClientCredentials
{
private string AuthenticationToken { get; set; }
public override void InitializeServiceClient<T>(ServiceClient<T> client)
{
var authenticationContext = new AuthenticationContext("https://login.windows.net/{tenantID}");
var credential = new ClientCredential(clientId: "xxxxx-xxxx-xx-xxxx-xxx", clientSecret: "{clientSecret}");
var result = authenticationContext.AcquireToken(resource: "https://management.core.windows.net/", clientCredential: credential);
if (result == null)
{
throw new InvalidOperationException("Failed to obtain the JWT token");
}
AuthenticationToken = result.AccessToken;
}
// override ProcessHttpRequestAsync to use the authentication token as an Authorization header:
public override async Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request == null) throw new ArgumentNullException("request");
if (AuthenticationToken == null) throw new InvalidOperationException("Token Provider Cannot Be Null");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationToken);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
await base.ProcessHttpRequestAsync(request, cancellationToken);
}
}
From the other side, I have an AzureApiManagementService
class to handle APIs and products:
public async Task<ApiContract> CreateApi(ApimCreateApi apimCreateApi )
{
var serviceCredentials = new AzureApiManagementServiceCredentials();
ApiManagementClient myClient = new ApiManagementClient(serviceCredentials);
//here where I stuck
//I can use myClient.Api.CreateOrUpdateAsync(...)
//but it's going to create just an empty API on APIM
myClient.
...
}
I'm able just to create an API entity, I can not find the right methods to create/import an API from an openApi specification /swagger.
So I'm posting this this to get more ideas and challenge me in this topic!
Thank you!
从 APIM 的角度来看,导入与创建新 API 的手势相同(下面的 PUT 调用相同),所以我希望这会在 ApimCreateContract 上公开,查找类似于 REST API 构建方式的格式/值属性: https://docs .microsoft.com/en-us/rest/api/apimanagement/2019-12-01/apis/createorupdate
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.