Przeglądaj źródła

GA accounts controller

wfansh 6 miesięcy temu
rodzic
commit
9e4d289388

+ 49 - 1
src/main/java/com/wechi/adweb/bridge/google/analytics/controller/GAController.java

@@ -7,11 +7,16 @@ import com.wechi.adweb.bridge.common.OpenAPIRequest;
 import com.wechi.adweb.bridge.common.OpenAPIResponse;
 import com.wechi.adweb.bridge.exception.BadRequestException;
 import com.wechi.adweb.bridge.exception.DataException;
+import com.wechi.adweb.bridge.google.analytics.dto.GAAccountDTO;
+import com.wechi.adweb.bridge.google.analytics.dto.GAPropertyDTO;
 import com.wechi.adweb.bridge.google.analytics.dto.report.GAReportRequestDTO;
 import com.wechi.adweb.bridge.google.analytics.dto.report.ReportType;
 import com.wechi.adweb.bridge.google.analytics.dto.report.data.GAReportDataDTO;
+import com.wechi.adweb.bridge.google.analytics.service.GAAdminService;
 import com.wechi.adweb.bridge.google.analytics.service.GADataService;
 
+import io.swagger.v3.oas.annotations.Operation;
+
 import lombok.extern.slf4j.Slf4j;
 
 import org.apache.commons.lang3.StringUtils;
@@ -31,8 +36,51 @@ import java.util.Objects;
 public class GAController extends BaseController {
     @Autowired private GADataService gaDataService;
 
+    @Autowired private GAAdminService gaAdminService;
+
     private final Gson gson = new Gson();
 
+    @PostMapping("/accounts/list")
+    @ResponseBody
+    public OpenAPIResponse<List<GAAccountDTO>> listGAAccounts() throws DataException {
+        long start = System.currentTimeMillis();
+        log.info("****** listGAAccounts() ******");
+
+        List<GAAccountDTO> gaAccounts = gaAdminService.listGAAccounts();
+
+        log.info("****** listGAAccounts() ****** duration = {} seconds", getElapsedSeconds(start));
+        return OpenAPIResponse.<List<GAAccountDTO>>builder()
+                .status(APIStatus.SUCCESS)
+                .data(gaAccounts)
+                .build();
+    }
+
+    @Operation(summary = "List the properties under a GA account name.")
+    @PostMapping("/properties/list")
+    @ResponseBody
+    public OpenAPIResponse<List<GAPropertyDTO>> listGAProperties(
+            @RequestBody OpenAPIRequest<String> apiRequest)
+            throws BadRequestException, DataException {
+        long start = System.currentTimeMillis();
+        log.info("****** listGAProperties() ****** apiRequest = {}", gson.toJson(apiRequest));
+
+        // 1. Validates request parameters - GA account name.
+        if (StringUtils.isEmpty(apiRequest.getData())) {
+            throw new BadRequestException(apiRequest);
+        }
+
+        // 2. Executes the API request.
+        List<GAPropertyDTO> gaProperties =
+                gaAdminService.listGAProperties(
+                        GAAccountDTO.getResourceName(apiRequest.getData()), false);
+        log.info(
+                "****** listGAProperties() ****** duration = {} seconds", getElapsedSeconds(start));
+        return OpenAPIResponse.<List<GAPropertyDTO>>builder()
+                .status(APIStatus.SUCCESS)
+                .data(gaProperties)
+                .build();
+    }
+
     @PostMapping("/report")
     @ResponseBody
     public OpenAPIResponse<List<? extends GAReportDataDTO>> getGAReport(
@@ -54,7 +102,7 @@ public class GAController extends BaseController {
             throw new BadRequestException(apiRequest);
         }
 
-        // 2. Executes API requests.
+        // 2. Executes the API request.
         List<? extends GAReportDataDTO> report = gaDataService.runReport(apiRequest.getData());
         log.info("****** getGAReport() ****** duration = {} seconds", getElapsedSeconds(start));
         return OpenAPIResponse.<List<? extends GAReportDataDTO>>builder()

+ 6 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/GAAccountDTO.java

@@ -2,6 +2,8 @@ package com.wechi.adweb.bridge.google.analytics.dto;
 
 import lombok.Data;
 
+import org.apache.commons.lang3.StringUtils;
+
 /**
  * @author wfansh
  */
@@ -16,4 +18,8 @@ public class GAAccountDTO {
     private long createTime;
 
     private long updateTime;
+
+    public static final String getResourceName(String idOrName) {
+        return StringUtils.isNumeric(idOrName) ? "accounts/" + idOrName : idOrName;
+    }
 }