Tingkatkan API Google Analytics. Baca waktu habis, ubah kode java menjadi coldfusion

Dec 23 2020

Pertanyaan singkat: Bagaimana cara mengubah kode java berikut ke kode ColdFusion?

private static HttpRequestInitializer setHttpTimeout(final HttpRequestInitializer requestInitializer) {
      return new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest httpRequest) throws IOException {
          requestInitializer.initialize(httpRequest);
          httpRequest.setConnectTimeout(3 * 60000);  // 3 minutes connect timeout
          httpRequest.setReadTimeout(3 * 60000);  // 3 minutes read timeout
        }};

 }

Penjelasan: Saya menggunakan komponen untuk berkomunikasi dengan Google analytics API. Ini berfungsi dengan baik namun terkadang waktu tunggu pembacaan terjadi. Saya ingin meningkatkan koneksi dan membaca waktu habis. Saya menemukan posting di sini di StackOverflow yang menjelaskan bagaimana melakukan ini di Java (https://stackoverflow.com/a/30721185/2482184). Namun, saya mengalami masalah saat mengubah kode java menjadi kode ColdFusion. Di bawah komponen ColdFusion yang saat ini saya gunakan untuk berkomunikasi dengan API Google Analytics:

<cfcomponent displayname="cfanalyticsConnector" output="false">

    <cffunction name="init" access="public" output="false" returntype="cfanalyticsConnector">
        <cfargument name="serviceAccountId" type="string" required="true" />
        <cfargument name="pathToKeyFile" type="string" required="true" />
        <cfargument name="analyticsAppName" type="string" required="true" />
        <cfscript>
            variables.serviceAccountId         = arguments.serviceAccountId;
            variables.pathToKeyFile            = arguments.pathToKeyFile;
            variables.analyticsAppName         = arguments.analyticsAppName;

            variables.HTTP_Transport           = createObject("java", "com.google.api.client.http.javanet.NetHttpTransport").init();
            variables.JSON_Factory             = createObject("java", "com.google.api.client.json.jackson2.JacksonFactory").init();
            variables.HTTP_Request_Initializer = createObject("java", "com.google.api.client.http.HttpRequestInitializer");
            variables.Credential_Builder       = createObject("java", "com.google.api.client.googleapis.auth.oauth2.GoogleCredential$Builder"); variables.Analytics_Scopes = createObject("java", "com.google.api.services.analytics.AnalyticsScopes"); variables.Analytics_Builder = createObject("java", "com.google.api.services.analytics.Analytics$Builder").init(
                                                                         variables.HTTP_Transport, 
                                                                         variables.JSON_Factory, 
                                                                         javaCast("null", ""));
                                                                        
            variables.Collections              = createObject("java", "java.util.Collections");
            variables.File_Obj                 = createObject("java", "java.io.File");

            variables.credential               = "";
            variables.analytics                = "";
        </cfscript>
        <cfreturn this />
    </cffunction>

    <cffunction name="buildAnalytics" access="public" output="true" returntype="struct" hint="creates analytics object">
        <cfset local = {} />
        <cfset local.credential = "" />
        <cfset local.returnStruct = {} />
        <cfset local.returnStruct.success = true />
        <cfset local.returnStruct.error = "" />

        <!--- Access tokens issued by the Google OAuth 2.0 Authorization Server expire in one hour. 
        When an access token obtained using the assertion flow expires, then the application should 
        generate another JWT, sign it, and request another access token. 
        https://developers.google.com/accounts/docs/OAuth2ServiceAccount --->

        <cftry>
             <cfset local.credential = Credential_Builder
                            .setTransport(variables.HTTP_Transport)
                            .setJsonFactory(variables.JSON_Factory)
                            .setServiceAccountId(variables.serviceAccountId)
                            .setServiceAccountScopes(Collections.singleton(variables.Analytics_Scopes.ANALYTICS_READONLY))
                            .setServiceAccountPrivateKeyFromP12File(variables.File_Obj.Init(variables.pathToKeyFile))
                            .build() />

            <cfcatch type="any">
                <cfset local.returnStruct.error = "Credential Object Error: " & cfcatch.message & " - " & cfcatch.detail />
                <cfset local.returnStruct.success = false />
            </cfcatch>
        </cftry>

        <cfif  local.returnStruct.success>
            <cftry>
                <cfset variables.analytics = variables.Analytics_Builder
                                .setApplicationName(variables.analyticsAppName)
                                .setHttpRequestInitializer(local.credential)
                                .build() />

                <cfcatch type="any">
                    <cfset local.returnStruct.error = "Analytics Object Error: " & cfcatch.message & " - " & cfcatch.detail />
                    <cfset local.returnStruct.success = false />
                </cfcatch>
            </cftry>
        </cfif>
        
        <cfreturn local.returnStruct />
    </cffunction>
</cfcomponent>

Kode java menjelaskan cara meningkatkan koneksi dan membaca waktu habis:

private static HttpRequestInitializer setHttpTimeout(final HttpRequestInitializer requestInitializer) {
      return new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest httpRequest) throws IOException {
          requestInitializer.initialize(httpRequest);
          httpRequest.setConnectTimeout(3 * 60000);  // 3 minutes connect timeout
          httpRequest.setReadTimeout(3 * 60000);  // 3 minutes read timeout
        }};

 }

public static Analytics initializeAnalytics() throws Exception {
    // Initializes an authorized analytics service object.

    // Construct a GoogleCredential object with the service account email
    // and p12 file downloaded from the developer console.
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    GoogleCredential credential = new GoogleCredential.Builder()
        .setTransport(httpTransport)
        .setJsonFactory(JSON_FACTORY)
        .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
        .setServiceAccountPrivateKeyFromP12File(new File(KEY_FILE_LOCATION))
        .setServiceAccountScopes(AnalyticsScopes.all())
        .build();

    // Construct the Analytics service object.
    return new Analytics.Builder(httpTransport, JSON_FACTORY,setHttpTimeout(credential))
        .setApplicationName(APPLICATION_NAME).build();
  }

Jika saya mengerti dengan benar, saya perlu mengubah baris kode ini:

<cfset variables.analytics = variables.Analytics_Builder
    .setApplicationName(variables.analyticsAppName)
    .setHttpRequestInitializer(local.credential)
    .build() />

Untuk sesuatu seperti:

<cfset variables.analytics = createObject("java",
    "com.google.api.services.analytics.Analytics$Builder").init(
        variables.HTTP_Transport, 
        variables.JSON_Factory, 
        setHttpTimeout(local.credential)
    )
    .setApplicationName(variables.analyticsAppName)
    .setHttpRequestInitializer(local.credential)
    .build() />
>

Howerer saya belum berhasil dalam menutupi fungsi java setHttpTimeout ke coldfusion. Bantuan apa pun akan sangat dihargai.

Jawaban

user12031119 Dec 23 2020 at 23:00

Untuk membuat permintaan waktu tunggu 3 menit, Anda dapat memanggil baris kode ini sebelum membuat permintaan http Anda.

Format tag

<cfsetting RequestTimeout = 3 * 60>

Format CFScript

setting requesttimeout = 3 * 60;