wfansh 6 mesiacov pred
rodič
commit
da15545d04

+ 22 - 0
src/main/java/com/wechi/adweb/bridge/exception/DataException.java

@@ -0,0 +1,22 @@
+package com.wechi.adweb.bridge.exception;
+
+/**
+ * @author wfansh
+ */
+public class DataException extends Exception {
+    public DataException() {
+        super();
+    }
+
+    public DataException(String message) {
+        super(message);
+    }
+
+    public DataException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public DataException(Throwable cause) {
+        super(cause);
+    }
+}

+ 42 - 2
src/main/java/com/wechi/adweb/bridge/google/analytics/GAAdminService.java

@@ -1,8 +1,15 @@
 package com.wechi.adweb.bridge.google.analytics;
 
+import com.google.analytics.admin.v1beta.Account;
+import com.google.analytics.admin.v1beta.AnalyticsAdminServiceClient;
 import com.google.analytics.admin.v1beta.AnalyticsAdminServiceSettings;
+import com.google.analytics.admin.v1beta.ListAccountsRequest;
+import com.google.api.client.util.Lists;
 import com.google.api.gax.core.FixedCredentialsProvider;
 import com.google.auth.oauth2.GoogleCredentials;
+import com.google.common.base.Splitter;
+import com.wechi.adweb.bridge.exception.DataException;
+import com.wechi.adweb.bridge.google.analytics.dto.GAAccountDTO;
 
 import jakarta.annotation.PostConstruct;
 
@@ -11,6 +18,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
 import java.io.IOException;
+import java.util.List;
 
 /**
  * @author wfansh
@@ -18,7 +26,7 @@ import java.io.IOException;
 @Slf4j
 @Service
 public class GAAdminService {
-    private AnalyticsAdminServiceSettings analyticsAdminServiceSettings;
+    private AnalyticsAdminServiceSettings adminServiceSettings;
 
     @PostConstruct
     private void init() throws IOException {
@@ -28,9 +36,41 @@ public class GAAdminService {
                                 .getClassLoader()
                                 .getResourceAsStream("google/service-account-key.json"));
 
-        this.analyticsAdminServiceSettings =
+        this.adminServiceSettings =
                 AnalyticsAdminServiceSettings.newBuilder()
                         .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
                         .build();
     }
+
+    public List<GAAccountDTO> listGAAccounts() throws DataException {
+        List<GAAccountDTO> gaAccounts = Lists.newArrayList();
+        try (AnalyticsAdminServiceClient analyticsAdminServiceClient =
+                AnalyticsAdminServiceClient.create(adminServiceSettings)) {
+            // Initializes the list request with default settings.
+            ListAccountsRequest request = ListAccountsRequest.getDefaultInstance();
+
+            for (Account account : analyticsAdminServiceClient.listAccounts(request).iterateAll()) {
+                GAAccountDTO gaAccount = new GAAccountDTO();
+                gaAccount.setAccountId(parseId(account.getName()));
+                gaAccount.setAccountName(account.getName());
+                gaAccount.setDisplayName(account.getDisplayName());
+                gaAccount.setCreateTime(account.getCreateTime().getSeconds());
+                gaAccount.setUpdateTime(account.getUpdateTime().getSeconds());
+
+                // Adds into the list;
+                gaAccounts.add(gaAccount);
+            }
+
+            return gaAccounts;
+        } catch (IOException e) {
+            log.error(e.getMessage());
+            throw new DataException(e);
+        }
+    }
+
+    /** Parses GA account ID from name. */
+    private static String parseId(String name) {
+        List<String> parts = Splitter.on('/').splitToList(name);
+        return parts.size() == 2 ? parts.get(1) : null;
+    }
 }

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

@@ -0,0 +1,19 @@
+package com.wechi.adweb.bridge.google.analytics.dto;
+
+import lombok.Data;
+
+/**
+ * @author wfansh
+ */
+@Data
+public class GAAccountDTO {
+    public String accountId;
+
+    public String accountName;
+
+    public String displayName;
+
+    public long createTime;
+
+    public long updateTime;
+}