【问题标题】:Java Design pattern: Avoid Duplication of code for Azure Rest api callJava 设计模式:避免 Azure Rest api 调用的代码重复
【发布时间】:2022-01-23 20:46:21
【问题描述】:

我编写了一个小型 java 程序,它会进行一次休息调用,它可以按预期正常工作。我会。现在,我必须为其他 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){}
}

【问题讨论】:

    标签: java design-patterns azure-web-app-service


    【解决方案1】:

    您可以使用 Azure Java SDK 调用 Azure REST API。 Azure SDK for Java 库构建在底层 Azure REST API 之上,允许您通过熟悉的 Java 范例使用这些 API。

    要首先使用 Azure Java SDK 调用 Azure REST API,您需要创建一个服务主体 (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 Design Guidelines

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 2023-03-22
      • 2014-03-07
      相关资源
      最近更新 更多