wfansh 6 months ago
parent
commit
2d8fe27003

+ 9 - 4
build.gradle

@@ -1,7 +1,7 @@
 plugins {
     id 'java'
-    id 'org.springframework.boot' version '3.3.3'
-    id 'io.spring.dependency-management' version '1.1.6'
+    id 'org.springframework.boot' version '3.2.3'
+    id 'io.spring.dependency-management' version '1.1.4'
 }
 
 group = 'com.wechi.adweb'
@@ -18,7 +18,7 @@ repositories {
 }
 
 dependencies {
-    implementation 'org.springframework.boot:spring-boot-starter'
+    implementation 'org.springframework.boot:spring-boot-starter-web'
 
     implementation 'org.springframework:spring-context'
     implementation 'org.slf4j:slf4j-api'
@@ -28,13 +28,18 @@ dependencies {
     compileOnly 'org.projectlombok:lombok'
     annotationProcessor 'org.projectlombok:lombok'
 
+    // Swagger docs without additional configuration.
+    // swagger-ui/index.html & v3/api-docs
+    implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.4.0'
+
     // Google
     implementation 'com.google.apis:google-api-services-tagmanager:v2-rev20240701-2.0.0'
     implementation 'com.google.analytics:google-analytics-admin:0.59.0'
     implementation 'com.google.analytics:google-analytics-data:0.60.0'
 
     testImplementation 'org.springframework.boot:spring-boot-starter-test'
-    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
+    testImplementation 'org.junit.jupiter:junit-jupiter-api'
+    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
 }
 
 tasks.named('test') {

+ 6 - 3
src/main/java/com/wechi/adweb/bridge/google/analytics/GAAdminService.java

@@ -15,6 +15,7 @@ import jakarta.annotation.PostConstruct;
 
 import lombok.extern.slf4j.Slf4j;
 
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import java.io.IOException;
@@ -26,6 +27,10 @@ import java.util.List;
 @Slf4j
 @Service
 public class GAAdminService {
+
+    @Value("${google.service.account.key}")
+    private String serviceAccountKey;
+
     private AnalyticsAdminServiceSettings adminServiceSettings;
 
     private Gson gson = new Gson();
@@ -34,9 +39,7 @@ public class GAAdminService {
     private void init() throws IOException {
         GoogleCredentials credentials =
                 GoogleCredentials.fromStream(
-                        this.getClass()
-                                .getClassLoader()
-                                .getResourceAsStream("google/service-account-key.json"));
+                        this.getClass().getClassLoader().getResourceAsStream(serviceAccountKey));
 
         this.adminServiceSettings =
                 AnalyticsAdminServiceSettings.newBuilder()

+ 8 - 3
src/main/java/com/wechi/adweb/bridge/google/analytics/GADataService.java

@@ -16,6 +16,7 @@ import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 
 import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Value;
 
 import java.io.IOException;
 import java.util.List;
@@ -25,6 +26,12 @@ import java.util.Objects;
 @Data
 public class GADataService {
 
+    @Value("${spring.application.name}")
+    private String applicationName;
+
+    @Value("${google.service.account.key}")
+    private String serviceAccountKey;
+
     private BetaAnalyticsDataSettings dataSettings;
 
     private Gson gson = new Gson();
@@ -33,9 +40,7 @@ public class GADataService {
     private void init() throws IOException {
         GoogleCredentials credentials =
                 GoogleCredentials.fromStream(
-                        this.getClass()
-                                .getClassLoader()
-                                .getResourceAsStream("google/service-account-key.json"));
+                        this.getClass().getClassLoader().getResourceAsStream(serviceAccountKey));
 
         this.dataSettings =
                 BetaAnalyticsDataSettings.newBuilder()

+ 22 - 0
src/main/java/com/wechi/adweb/bridge/google/gtm/controller/GTMController.java

@@ -0,0 +1,22 @@
+package com.wechi.adweb.bridge.google.gtm.controller;
+
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.commons.lang3.RandomStringUtils;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @author wfansh
+ */
+@Slf4j
+@RestController
+@RequestMapping("/api/google/gtm")
+public class GTMController {
+
+    @GetMapping("/test")
+    public String test() {
+        return RandomStringUtils.randomAlphabetic(10);
+    }
+}

+ 7 - 2
src/main/java/com/wechi/adweb/bridge/google/gtm/GTMService.java → src/main/java/com/wechi/adweb/bridge/google/gtm/service/GTMService.java

@@ -1,4 +1,4 @@
-package com.wechi.adweb.bridge.google.gtm;
+package com.wechi.adweb.bridge.google.gtm.service;
 
 import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
 import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
@@ -11,6 +11,7 @@ import jakarta.annotation.PostConstruct;
 
 import lombok.extern.slf4j.Slf4j;
 
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import java.io.IOException;
@@ -22,6 +23,10 @@ import java.security.GeneralSecurityException;
 @Slf4j
 @Service
 public class GTMService {
+
+    @Value("${google.service.account.key}")
+    private String serviceAccountKey;
+
     private TagManager tagManager;
 
     @PostConstruct
@@ -32,7 +37,7 @@ public class GTMService {
                 GoogleCredential.fromStream(
                                 this.getClass()
                                         .getClassLoader()
-                                        .getResourceAsStream("google/service-account-key.json"))
+                                        .getResourceAsStream(serviceAccountKey))
                         .createScoped(TagManagerScopes.all());
 
         this.tagManager =

+ 4 - 2
src/main/resources/application.properties

@@ -1,3 +1,5 @@
-spring.application.name=data-bridge
+spring.application.name=adweb3-data-bridge
 ## Spring Boot
-server.port=8090
+server.port=9002
+## Google Services
+google.service.account.key=google/service-account-key.json

+ 1 - 17
src/test/java/com/wechi/adweb/bridge/DataBridgeApplicationTests.java

@@ -1,32 +1,16 @@
 package com.wechi.adweb.bridge;
 
-import com.google.analytics.data.v1beta.Dimension;
-import com.google.common.base.Splitter;
-import com.google.common.base.Strings;
 import com.wechi.adweb.bridge.google.analytics.GAAdminService;
 
-import com.wechi.adweb.bridge.google.analytics.dto.GAReportRequestDTO;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 
-import java.util.List;
-import java.util.Optional;
-
 @SpringBootTest
 class DataBridgeApplicationTests {
 
     @Autowired GAAdminService gaAdminService;
 
     @Test
-    void contextLoads() throws Exception {
-
-//        List<Dimension> dimensions =
-//                Splitter.on(GAReportRequestDTO.DEFAULT_SPLITTER)
-//                        .splitToStream(Strings.nullToEmpty("aaa,bbb"))
-//                        .map(dimension -> Dimension.newBuilder().setName(dimension).build())
-//                        .toList();
-//
-//        System.out.println(dimensions);
-    }
+    void contextLoads() throws Exception {}
 }