Ver Fonte

GTM containers

wfansh há 6 meses atrás
pai
commit
bfdf548a34

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

@@ -2,13 +2,18 @@ package com.wechi.adweb.bridge.google.gtm.controller;
 
 import com.wechi.adweb.bridge.common.APIStatus;
 import com.wechi.adweb.bridge.common.BaseController;
+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.gtm.dto.GTMAccountDTO;
+import com.wechi.adweb.bridge.google.gtm.dto.GTMContainerDTO;
 import com.wechi.adweb.bridge.google.gtm.service.GTMService;
+import com.wechi.adweb.bridge.util.JsonUtils;
 
 import lombok.extern.slf4j.Slf4j;
 
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
@@ -38,4 +43,32 @@ public class GTMController extends BaseController {
                 .data(gtmAccounts)
                 .build();
     }
+
+    @PostMapping("/containers/list")
+    @ResponseBody
+    public OpenAPIResponse<List<GTMContainerDTO>> listGTMContainers(
+            @RequestBody OpenAPIRequest<String> apiRequest)
+            throws BadRequestException, DataException {
+        long start = System.currentTimeMillis();
+        log.info("****** listGTMContainers() ****** apiRequest = {}", JsonUtils.toJson(apiRequest));
+        String accountResourceName = apiRequest.getData();
+
+        // 1. Validates the request parameters.
+        if (StringUtils.isEmpty(accountResourceName)) {
+            throw new BadRequestException(apiRequest);
+        }
+
+        // 2. Executes the API request.
+        List<GTMContainerDTO> gtmContainers =
+                gtmService.listGTMContainers(
+                        // Converts to resource name if the account ID is provided by mistake.
+                        GTMAccountDTO.toResourceName(accountResourceName));
+        log.info(
+                "****** listGTMContainers() ****** duration = {} seconds",
+                getElapsedSeconds(start));
+        return OpenAPIResponse.<List<GTMContainerDTO>>builder()
+                .status(APIStatus.SUCCESS)
+                .data(gtmContainers)
+                .build();
+    }
 }

+ 6 - 0
src/main/java/com/wechi/adweb/bridge/google/gtm/dto/GTMAccountDTO.java

@@ -7,6 +7,8 @@ import lombok.EqualsAndHashCode;
 import lombok.ToString;
 import lombok.experimental.SuperBuilder;
 
+import org.apache.commons.lang3.StringUtils;
+
 /**
  * @author wfansh
  */
@@ -19,4 +21,8 @@ public class GTMAccountDTO extends ResourceDTO {
     private boolean supportUserPermissions;
 
     private boolean supportMultipleContainers;
+
+    public static String toResourceName(String id) {
+        return StringUtils.isNumeric(id) ? "accounts" + RESOURCE_NAME_SPLITTER + id : id;
+    }
 }

+ 40 - 0
src/main/java/com/wechi/adweb/bridge/google/gtm/dto/GTMContainerDTO.java

@@ -0,0 +1,40 @@
+package com.wechi.adweb.bridge.google.gtm.dto;
+
+import com.wechi.adweb.bridge.google.common.ResourceDTO;
+
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.ToString;
+import lombok.experimental.SuperBuilder;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.List;
+
+/**
+ * @author wfansh
+ */
+@Data
+@SuperBuilder
+@ToString(callSuper = true)
+@EqualsAndHashCode(callSuper = true)
+public class GTMContainerDTO extends ResourceDTO {
+
+    private String accountId;
+
+    private List<String> domainNames;
+
+    private String publicId;
+
+    private List<String> tagIds;
+
+    private String notes;
+
+    private List<String> usageContext;
+
+    private String fingerprint;
+
+    public static String toResourceName(String id) {
+        return StringUtils.isNumeric(id) ? "accounts" + RESOURCE_NAME_SPLITTER + id : id;
+    }
+}

+ 62 - 0
src/main/java/com/wechi/adweb/bridge/google/gtm/service/GTMService.java

@@ -6,10 +6,12 @@ import com.google.api.client.util.Lists;
 import com.google.api.services.tagmanager.TagManager;
 import com.google.api.services.tagmanager.TagManagerScopes;
 import com.google.api.services.tagmanager.model.Account;
+import com.google.api.services.tagmanager.model.Container;
 import com.google.auth.http.HttpCredentialsAdapter;
 import com.google.auth.oauth2.GoogleCredentials;
 import com.wechi.adweb.bridge.exception.DataException;
 import com.wechi.adweb.bridge.google.gtm.dto.GTMAccountDTO;
+import com.wechi.adweb.bridge.google.gtm.dto.GTMContainerDTO;
 import com.wechi.adweb.bridge.util.JsonUtils;
 
 import lombok.extern.slf4j.Slf4j;
@@ -79,4 +81,64 @@ public class GTMService {
             throw new DataException(e);
         }
     }
+
+    public List<GTMContainerDTO> listGTMContainers(String accountResourceName)
+            throws DataException {
+        try {
+            List<GTMContainerDTO> gtmContainers = Lists.newArrayList();
+            for (Container container :
+                    tagManager
+                            .accounts()
+                            .containers()
+                            .list(accountResourceName)
+                            .execute()
+                            .getContainer()) {
+                GTMContainerDTO gtmContainer =
+                        GTMContainerDTO.builder()
+                                .id(container.getContainerId())
+                                .resourceName(container.getPath())
+                                .displayName(container.getName())
+                                .accountId(container.getAccountId())
+                                .domainNames(container.getDomainName())
+                                .publicId(container.getPublicId())
+                                .tagIds(container.getTagIds())
+                                .notes(container.getNotes())
+                                .usageContext(container.getUsageContext())
+                                .fingerprint(container.getFingerprint())
+                                .build();
+
+                // Adds into the list;
+                gtmContainers.add(gtmContainer);
+            }
+
+            log.info("listGTMContainers : {}", JsonUtils.toJson(gtmContainers));
+            return gtmContainers;
+        } catch (IOException e) {
+            log.error(e.getMessage());
+            throw new DataException(e);
+        }
+    }
+
+    //    public GTMContainerDTO createGTMContainer(String accountResourceName, String displayName)
+    //            throws DataException {
+    //        try {
+    //            Container container = new Container();
+    //            container.setName(displayName);
+    //            container.setUsageContext(List.of("web", "android", "ios"));
+    //
+    //            Container createdContainer =
+    //                    tagManager
+    //                            .accounts()
+    //                            .containers()
+    //                            .create(GTMAccountDTO.toResourceName(accountResourceName),
+    // container)
+    //                            .execute();
+    //
+    //            return null;
+    //
+    //        } catch (IOException e) {
+    //            log.error(e.getMessage());
+    //            throw new DataException(e);
+    //        }
+    //    }
 }