簡體   English   中英

Java 設計模式:避免重復代碼 Azure Rest Z8A5DA52ED1264417A8AAZE 調用7CA5772

[英]Java Design pattern: Avoid Duplication of code for Azure Rest api call

我寫了一個小的 java 程序,它將進行 rest 調用,它按預期工作。 我會。 現在,我必須為其他 API 調用編寫一個類似的程序。 怎么做,我構造了代碼,這樣我就可以避免重復代碼!

System.out.println( "Usage Details!" );

AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
TokenCredential credential = new DefaultAzureCredentialBuilder()           
       .authorityHost(profile.getEnvironment()
                             .getActiveDirectoryEndpoint())
       .build();
ConsumptionManager consumptionManager = ConsumptionManager.authenticate(credential, profile);

// the below detail changes for different api's
PagedIterable<UsageDetail> usageDetailList = consumptionManager.usageDetails()
        .list("url",<argument1>,<argument2>,null,null,Metrictype.USAGE,Context.NONE);
int count=1;

for(UsageDetail usageDetail : usageDetailList){
        LegacyUsageDetail legacyUsageDetail = (LegacyUsageDetail)usageDetail.innerModel();
        try{
            //if(legacyUsageDetail.date().toString().equals("2021-09-22T00:00Z") && 
                 legacyUsageDetail.resourceGroup().startsWith("F2BDEVC-ms")){
                if(count==1){
                    System.out.println("subscriptionName : " + 
                           legacyUsageDetail.subscriptionName());
                  }}
  }catch(Exception e){}
}

You can use Azure Java SDK to call Azure rest API. The Azure SDK for Java libraries build on top of the underlying Azure REST API, allowing you to use those APIs through familiar Java paradigms.

To use Azure Java SDK to call Azure rest API first you need to create a service principal (SP). 然后安裝 SDK,如下所示 -

<!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure -->
<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure</artifactId>
    <version>1.33.0</version>
</dependency>

然后編寫類似於下面顯示的代碼。

private static String tenantId=""; // sp tenant
private static String clientId = ""; // sp appid
private static String clientKey = "";// sp password
private static String subscriptionId=""; //sp subscription id

ApplicationTokenCredentials creds = new ApplicationTokenCredentials(clientId,tenantId,clientKey, AzureEnvironment.AZURE);

RestClient restClient =new RestClient.Builder()
                .withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
                .withSerializerAdapter(new AzureJacksonAdapter())
                .withReadTimeout(150, TimeUnit.SECONDS)
                .withLogLevel(LogLevel.BODY)
                .withResponseBuilderFactory(new AzureResponseBuilder.Factory())
                .withCredentials(creds)
                .build();
OkHttpClient httpClient = restClient.httpClient().newBuilder().build();
String url = "https://management.azure.com/subscriptions/"+subscriptionId+"/providers/Microsoft.Migrate/projects?api-version=2018-02-02";
Request request = new Request.Builder()
                              .url(url)
                              .method("get",null)
                              .build();
Response response1 = httpClient.newCall(request).execute();

if(response1.isSuccessful()){
        System.out.println(response1.headers().toString());
}

有關設計指南的更多信息,請查看此Java Azure SDK 設計指南

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM