투케이2K

137. (TWOK/UTIL) [Android/Java] C_GraphQL_Client_Module : GraphQL 통신 수행 클라이언트 유틸 파일 본문

투케이2K 유틸파일

137. (TWOK/UTIL) [Android/Java] C_GraphQL_Client_Module : GraphQL 통신 수행 클라이언트 유틸 파일

투케이2K 2024. 10. 3. 08:32

[설 명]

프로그램 : Android / Java

설 명 : C_GraphQL_Client_Module : GraphQL 통신 수행 클라이언트 유틸 파일

 

[소스 코드]

 

package com.example.javaproject.C_Module;

import android.content.Context;

import com.example.javaproject.C_ErrorCheck;
import com.example.javaproject.C_Util;
import com.example.javaproject.S_FileManager;
import com.example.javaproject.S_FinalData;
import com.example.javaproject.S_Log;

import org.json.JSONObject;

import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.core.Observable;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okhttp3.logging.HttpLoggingInterceptor;
import okio.ByteString;

public class C_GraphQL_Client_Module {


    /**
     * // --------------------------------------------------------------------------------------
     * TODO [클래스 설명]
     * // --------------------------------------------------------------------------------------
     * 1. GraphQL 네트워크 통신 수행 클래스
     * // --------------------------------------------------------------------------------------
     * 2. 필요 라이브러리 :
     *
     * [OK HTTP]
     * implementation("com.squareup.okhttp3:okhttp:4.9.0")
     *
     * [RX]
     * implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
     * implementation 'io.reactivex.rxjava3:rxkotlin:3.0.1'
     * implementation 'io.reactivex.rxjava3:rxjava:3.0.7'
     *
     * // --------------------------------------------------------------------------------------
     * */





    /**
     * // --------------------------------------------------------------------------------------
     * TODO [개념 설명]
     * // --------------------------------------------------------------------------------------
     * 1. GraphQL 요약 설명 :
     *
     * - GraphQL 은 페이스북에서 만든 쿼리 언어로, API 를 위한 쿼리 언어로 주로 사용됩니다
     * - GraphQL 서비스는 타입과 필드를 정의하고, 각 타입의 필드에 대한 함수로 구현됩니다
     * - GraphQL 서비스가 실행되면 GraphQL 쿼리를 전송하여 유효성 검사 및 실행 >> 수신된 쿼리는 정의된 타입과 필드를 참조해 함수를 실행하여 결과를 생성합니다
     * - GraphQL 서비스에는 Queries , Mutations , Subscriptions 방식이 있으며, 해당 방식을 통해 데이터 읽기 및 수정 등 다양한 통신을 시도할 수 있습니다
     * - REST API 는 URL, METHOD 등을 조합해 요청하는 방식으로 다양한 Endpoint 가 존재하지만, gql 은 단 하나의 Endpoint 를 사용하며 쿼리를 조합해 결과를 요청합니다
     * // --------------------------------------------------------------------------------------
     * 2. TODO Queries 설명 :
     *
     * - 쿼리는 읽기 전용으로 Http Get 방식과 유사하며, [서버에 정보 요청 >> 정보 조회 >> 응답 결과 반환] 방식으로 사용합니다
     * - 쿼리 사용시 정의 된 Query 키워드는 서버에 요청을 보내는 클라이언트의 모든 진입점을 정의합니다
     * // --------------------------------------------------------------------------------------
     * 3. TODO Mutations 설명 :
     *
     * - Mutations 은 쓰기 작업을 수행하며, Http Post , Put 방식과 유사하며, [서버에 수정 요청 >> 정보 수정 >> 응답 결과 반환] 방식으로 사용합니다
     * - Mutations 사용시 정의 된 Mutation 키워드는 서버에 요청을 보내는 클라이언트의 수정 요청의 진입점을 정의합니다
     * // --------------------------------------------------------------------------------------
     * 4. TODO Subscriptions 설명 :
     *
     * - Subscriptions (구독) 은 서버와 클라이언트 간의 지속적인 양방향 연결을 여는 WebSockets 에 사용됩니다
     * - Subscriptions 은 일반적으로 클라이언트가 서버를 구독 후 >> 실시간 변경 된 데이터에 관해 메시지를 수신 받을 때 사용합니다
     *   >> 서버 측 변경이 있거나 서버에서 이벤트를 수행할 때마다 구독한 클라이언트가 업데이트를 받게 됩니다
     * - Subscriptions 작성 예시 : 새로운 Person이 추가될 때마다 구독한 클라이언트에게 메시지를 보냅니다
     * // --------------------------------------------------------------------------------------
     * 5. 개념 설명 및 테스트 참고 사이트 :
     *
     * https://docs.aws.amazon.com/ko_kr/appsync/latest/devguide/graphql-types.html
     *
     * https://www.postman.com/postman/published-postman-templates/graphql-request/63ce7518fe63376ed0693d9e
     *
     * https://lucasconstantino.github.io/graphiql-online/
     *
     * https://stackoverflow.com/questions/66010484/how-to-excute-graphql-query-with-okhttpclient
     *
     * https://docs.aws.amazon.com/ko_kr/appsync/latest/devguide/real-time-websocket-client.html
     * // --------------------------------------------------------------------------------------
     * */





    /**
     * // --------------------------------------------------------------------------------------
     * TODO [빠른 로직 찾기 : 주석 로직 찾기]
     * // --------------------------------------------------------------------------------------
     * [SEARCH FAST] : requestQueriesHttp
     * // --------------------------------------------------------------------------------------
     * [SEARCH FAST] : requestMutationHttp
     * // --------------------------------------------------------------------------------------
     * [SEARCH FAST] : requestSubscriptionHttp
     * // --------------------------------------------------------------------------------------
     */





    // -----------------------------------------------------------------------------------------
    private static final String ACTIVITY_NAME = "C_GraphQL_Client_Module";
    private static final int TIME_OUT_SECOND = 30;





    // -----------------------------------------------------------------------------------------
    // TODO [SEARCH FAST] : requestQueriesHttp
    // -----------------------------------------------------------------------------------------
    // TODO [호출 방법 소스 코드]
    // -----------------------------------------------------------------------------------------
    /*
    try {

        // [URL 주소 선언]
        String url = "https://graphql.postman-echo.com/graphql";

        // [파라미터 생성]
        Map<String, Object> headers = new HashMap<>();
        //headers.put("x-api-key", "sample_key"); // [key 인증시 사용]

        // [GraphQL 요청 형식 선언 : .graphql]
        String graphql = "";
        graphql += "query Hello {\n" +
                "    hello\n" +
                "}";

        // [http 요청 수행]
        C_GraphQL_Client_Module.requestQueriesHttp(A_Intro.this, "GraphQL Http 요청", url, headers, graphql) // [http 요청]
                .subscribeOn(AndroidSchedulers.mainThread()) // [Observable (생성자) 로직을 IO 스레드에서 실행 : 백그라운드]
                .observeOn(Schedulers.io()) // [Observer (관찰자) 로직을 메인 스레드에서 실행]
                .subscribe(new Observer<String>() { // [Observable.create 타입 지정]
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }

                    @Override
                    public void onNext(@NonNull String value) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }
    catch (Exception e){
        S_Log._printStackTrace_(null, S_FinalData.LOG_BUG_STATE, null, e);
    }
    // */
    // -----------------------------------------------------------------------------------------
    // TODO [샘플 로그]
    // -----------------------------------------------------------------------------------------
    /*
    V  ===================================================================
    [LOG :: CLASS PLACE :: com.example.javaproject.C_Module.C_GraphQL_Client_Module.lambda$requestQueriesHttp$0(C_GraphQL_Client_Module.java:306)]
    ----------------------------------------------------
    [LOG :: NOW TIME :: 2024-09-04 08:43:00 수요일]
    ----------------------------------------------------
    [LOG :: DESCRIPTION :: requestQueriesHttp :: GraphQL Queries Http [요청] 실시]
    ----------------------------------------------------
    [LOG :: TAG :: GraphQL Http 요청]
    ----------------------------------------------------
    [LOG :: TYPE :: POST BODY JSON >> REQUEST]
    ----------------------------------------------------
    [LOG :: URL :: https://graphql.postman-echo.com/graphql]
    ----------------------------------------------------
    [LOG :: HEADER :: {Cache-Control=no-cache, Content-Type=application/json}]
    ----------------------------------------------------
    [LOG :: PARAMS :: {"query":"query Hello {\n    hello\n}"}]
    V  ===================================================================


    V  ===================================================================
    [LOG :: CLASS PLACE :: com.example.javaproject.C_Module.C_GraphQL_Client_Module$2.onResponse(C_GraphQL_Client_Module.java:335)]
    ----------------------------------------------------
    [LOG :: NOW TIME :: 2024-09-04 08:43:01 수요일]
    ----------------------------------------------------
    [LOG :: DESCRIPTION :: requestQueriesHttp :: GraphQL Queries Http [응답] 확인]
    ----------------------------------------------------
    [LOG :: TAG :: GraphQL Http 요청]
    ----------------------------------------------------
    [LOG :: TYPE :: POST BODY JSON >> RESPONSE]
    ----------------------------------------------------
    [LOG :: CODE :: 200]
    ----------------------------------------------------
    [LOG :: URL :: https://graphql.postman-echo.com/graphql]
    ----------------------------------------------------
    [LOG :: HEADER :: date: Tue, 03 Sep 2024 23:43:02 GMT
    ----------------------------------------------------
    [LOG :: DATA :: {"data":{"hello":"Hello John Doe"}}]
    V  ===================================================================
    */
    // -----------------------------------------------------------------------------------------
    public static Observable<String> requestQueriesHttp(Context mContext, String tag, String url, Map header, String graphql){

        // [로직 처리 실시]
        return Observable.create(subscriber -> {

            try {

                // ------------------------------------------------------
                // TODO [사전 방어 로직 : body 데이터 값 체크 실시]
                // ------------------------------------------------------

                if (C_Util.stringNotNull(graphql) == false || graphql.startsWith("query") == false){
                    try { subscriber.onError(new Throwable("[FAIL] : [requestQueriesHttp] : Input Query Is Null or startsWith False")); } catch (Exception ex){ ex.printStackTrace(); }
                    return;
                }
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [사전 방어 로직 : url 데이터 값 체크 실시]
                // ------------------------------------------------------
                if (C_Util.stringNotNull(url) == false){
                    try { subscriber.onError(new Throwable("[FAIL] : [requestQueriesHttp] : Input Url Is Null")); } catch (Exception ex){ ex.printStackTrace(); }
                    return;
                }
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [URL 변수 선언 실시]
                // ------------------------------------------------------
                String urlData = String.valueOf(url);
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [BODY에 파라미터 추가 실시]
                // ------------------------------------------------------
                RequestBody requestBody = null;
                String bodyData = "";
                if (C_Util.stringNotNull(graphql) == true) { // [파라미터가 있는 경우]

                    // [json 데이터로 변경 실시]
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("query", graphql);

                    bodyData = jsonObject.toString();
                    requestBody = RequestBody.create(bodyData, MediaType.parse("application/json; charset=utf-8"));

                }
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [HTTP 통신 전문 로그 기록]
                // ------------------------------------------------------
                S_FileManager.appHttpLogSave(mContext, "\n\n\n\n\n\n\n\n\n");
                S_FileManager.appHttpLogSave(mContext, "TAG :: " + String.valueOf(tag));
                HttpLoggingInterceptor httpLogger = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(String message) {
                        if (mContext != null){
                            S_FileManager.appHttpLogSave(mContext, message);
                        }
                    }
                });
                httpLogger.setLevel(HttpLoggingInterceptor.Level.BODY);
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [OK HTTP 객체 선언 실시]
                // ------------------------------------------------------
                //OkHttpClient client = new OkHttpClient();
                OkHttpClient client = new OkHttpClient.Builder()
                        .connectTimeout(TIME_OUT_SECOND, TimeUnit.SECONDS) // [커넥션 제한 시간]
                        .readTimeout(TIME_OUT_SECOND, TimeUnit.SECONDS)
                        .writeTimeout(TIME_OUT_SECOND, TimeUnit.SECONDS)
                        .addInterceptor(httpLogger) // [Http 통신 로그]
                        .retryOnConnectionFailure(false)
                        .build();

                Request.Builder request = new Request.Builder();
                request.addHeader("Content-Type", "application/json"); //TODO [헤더]
                request.addHeader("Cache-Control", "no-cache"); //TODO [헤더]

                // [로그 출력 헤더 삽입]
                Map<String, String> requestHeader = new HashMap<>();
                requestHeader.put("Content-Type", "application/json");
                requestHeader.put("Cache-Control", "no-cache");

                if (C_Util.mapNotNull(header) == true){
                    Set set = header.keySet();
                    Iterator iterator = set.iterator();
                    while(iterator.hasNext()){
                        String key = (String) iterator.next();
                        requestHeader.put(key, String.valueOf(header.get(key)));
                        request.addHeader(key, String.valueOf(header.get(key))); //TODO [헤더 추가]
                    }
                }

                HttpUrl.Builder httpBuilder = HttpUrl.get(URI.create(urlData)).newBuilder();

                if (requestBody != null){
                    request.post(requestBody);
                }
                else {
                    request.post(new FormBody.Builder().build());
                }

                request.url(httpBuilder.build()); //TODO [httpBuilder 추가]
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [요청 로그 출력 실시]
                // ------------------------------------------------------
                S_Log._F_(mContext, ACTIVITY_NAME + " :: requestQueriesHttp :: GraphQL Queries Http [요청] 실시", new String[] {
                        "TAG :: " + String.valueOf(tag),
                        "TYPE :: " + "POST BODY JSON >> REQUEST",
                        "URL :: " + String.valueOf(urlData),
                        "HEADER :: " + String.valueOf(requestHeader),
                        "PARAMS :: " + String.valueOf(bodyData)
                });
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [비동기 처리 (enqueue 사용)]
                // ------------------------------------------------------
                String finalUrlData = urlData;
                client.newCall(request.build()).enqueue(new Callback() {

                    // TODO [성공한 경우]
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String responseUrl = String.valueOf(finalUrlData);
                        final String responseHeader = String.valueOf(response.headers().toString());
                        final String responseCode = String.valueOf(response.code());
                        final String responseData = String.valueOf(response.body().string());


                        // ------------------------------------------------------
                        // [로그 출력]
                        // ------------------------------------------------------
                        S_Log._F_(mContext, ACTIVITY_NAME + " :: requestQueriesHttp :: GraphQL Queries Http [응답] 확인", new String[] {
                                "TAG :: " + String.valueOf(tag),
                                "TYPE :: " + "POST BODY JSON >> RESPONSE",
                                "CODE :: " + String.valueOf(responseCode),
                                "URL :: " + String.valueOf(responseUrl),
                                "HEADER :: " + String.valueOf(responseHeader),
                                "DATA :: " + String.valueOf(responseData)
                        });
                        // ------------------------------------------------------


                        // ------------------------------------------------------
                        // TODO [응답 코드 값 체크 및 리턴 데이터 반환]
                        // ------------------------------------------------------
                        try {
                            int statusCode = response.code();
                            if (statusCode >= 200 && statusCode < 300){ // TODO [정상 응답]

                                // TODO [결과 전송 실시]
                                subscriber.onNext(responseData);
                                subscriber.onComplete();

                            }
                            else { // TODO [에러 응답]
                                subscriber.onError(new Throwable("[ERROR] : : [requestQueriesHttp] : " + C_ErrorCheck.getErrorHttpMsg(responseCode)));
                            }
                        }
                        catch (Exception e){
                            S_Log._printStackTrace_(mContext, S_FinalData.LOG_BUG_STATE, null, e);
                            subscriber.onError(new Throwable("[EXCEPTION] : [1] : [requestQueriesHttp] : " + e.getMessage()));
                        }
                        // ------------------------------------------------------

                    }

                    //TODO [실패한 경우]
                    @Override
                    public void onFailure(Call call, IOException e) {
                        final String responseUrl = String.valueOf(finalUrlData);
                        final String responseError = String.valueOf(e.getMessage());


                        // ------------------------------------------------------
                        // [로그 출력]
                        // ------------------------------------------------------
                        S_Log._F_(mContext, ACTIVITY_NAME + " :: requestQueriesHttp :: GraphQL Queries Http [에러] 확인", new String[] {
                                "TAG :: " + String.valueOf(tag),
                                "TYPE :: " + "POST BODY JSON >> FAIL",
                                "URL :: " + String.valueOf(responseUrl),
                                "DATA :: " + String.valueOf(responseError)
                        });
                        // ------------------------------------------------------


                        // ------------------------------------------------------
                        // TODO [리턴 데이터 반환]
                        // ------------------------------------------------------
                        try { subscriber.onError(new Throwable("[FAIL] : [requestQueriesHttp] : " + responseError)); } catch (Exception ex){ ex.printStackTrace(); }
                        // ------------------------------------------------------
                    }
                });

            } catch (final Exception e){
                // ------------------------------------------------------
                // [로그 출력]
                // ------------------------------------------------------
                S_Log._printStackTrace_(mContext, S_FinalData.LOG_BUG_STATE, new String[] {
                        ACTIVITY_NAME + " :: requestQueriesHttp :: GraphQL Queries Http [EXCEPTION] 확인",
                        "TAG :: " + String.valueOf(tag),
                        "TYPE :: " + "POST BODY JSON >> EXCEPTION",
                        "EXCEPTION :: " + String.valueOf(e.getMessage())
                }, e);
                // ------------------------------------------------------


                // ------------------------------------------------------
                // TODO [리턴 데이터 반환]
                // ------------------------------------------------------
                try { subscriber.onError(new Throwable("[EXCEPTION] : [2] : [requestQueriesHttp] : " + String.valueOf(e.getMessage()))); } catch (Exception ex){ ex.printStackTrace(); }
                // ------------------------------------------------------
            }

        });
    }





    // -----------------------------------------------------------------------------------------
    // TODO [SEARCH FAST] : requestMutationHttp
    // -----------------------------------------------------------------------------------------
    // TODO [호출 방법 소스 코드]
    // -----------------------------------------------------------------------------------------
    /*
    try {

        // [URL 주소 선언]
        String url = "https://graphql.postman-echo.com/graphql";

        // [파라미터 생성]
        Map<String, Object> headers = new HashMap<>();
        //headers.put("x-api-key", "sample_key"); // [key 인증시 사용]

        // [GraphQL 요청 형식 선언 : .graphql]
        String graphql = "";
        graphql += "mutation CreatePerson {\n" +
                "    createPerson(person: {name: \"Larry David\", age: 47}) {\n" +
                "        age\n" +
                "        id\n" +
                "        name\n" +
                "    }\n" +
                "}";

        // [http 요청 수행]
        C_GraphQL_Client_Module.requestMutationHttp(A_Intro.this, "GraphQL Http 요청", url, headers, graphql) // [http 요청]
                .subscribeOn(AndroidSchedulers.mainThread()) // [Observable (생성자) 로직을 IO 스레드에서 실행 : 백그라운드]
                .observeOn(Schedulers.io()) // [Observer (관찰자) 로직을 메인 스레드에서 실행]
                .subscribe(new Observer<String>() { // [Observable.create 타입 지정]
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }

                    @Override
                    public void onNext(@NonNull String value) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }
    catch (Exception e){
        S_Log._printStackTrace_(null, S_FinalData.LOG_BUG_STATE, null, e);
    }
    // */
    // -----------------------------------------------------------------------------------------
    // TODO [샘플 로그]
    // -----------------------------------------------------------------------------------------
    /*
    V  ===================================================================
    [LOG :: CLASS PLACE :: com.example.javaproject.C_Module.C_GraphQL_Client_Module.lambda$requestMutationHttp$1(C_GraphQL_Client_Module.java:642)]
    ----------------------------------------------------
    [LOG :: NOW TIME :: 2024-09-04 08:57:09 수요일]
    ----------------------------------------------------
    [LOG :: DESCRIPTION :: requestMutationHttp :: GraphQL Mutation Http [요청] 실시]
    ----------------------------------------------------
    [LOG :: TAG :: GraphQL Http 요청]
    ----------------------------------------------------
    [LOG :: TYPE :: POST BODY JSON >> REQUEST]
    ----------------------------------------------------
    [LOG :: URL :: https://graphql.postman-echo.com/graphql]
    ----------------------------------------------------
    [LOG :: HEADER :: {Cache-Control=no-cache, Content-Type=application/json}]
    ----------------------------------------------------
    [LOG :: PARAMS :: {"query":"mutation CreatePerson {\n    createPerson(person: {name: \"Larry David\", age: 47}) {\n        age\n        id\n        name\n    }\n}"}]
    V  ===================================================================

    V  ===================================================================
    [LOG :: CLASS PLACE :: com.example.javaproject.C_Module.C_GraphQL_Client_Module$4.onResponse(C_GraphQL_Client_Module.java:671)]
    ----------------------------------------------------
    [LOG :: NOW TIME :: 2024-09-04 08:57:11 수요일]
    ----------------------------------------------------
    [LOG :: DESCRIPTION :: requestMutationHttp :: GraphQL Mutation Http [응답] 확인]
    ----------------------------------------------------
    [LOG :: TAG :: GraphQL Http 요청]
    ----------------------------------------------------
    [LOG :: TYPE :: POST BODY JSON >> RESPONSE]
    ----------------------------------------------------
    [LOG :: CODE :: 200]
    ----------------------------------------------------
    [LOG :: URL :: https://graphql.postman-echo.com/graphql]
    ----------------------------------------------------
    [LOG :: HEADER :: date: Tue, 03 Sep 2024 23:57:11 GMT
    ----------------------------------------------------
    [LOG :: DATA :: {"data":{"createPerson":{"age":47,"id":"61","name":"Larry David"}}}]
    V  ===================================================================
    */
    // -----------------------------------------------------------------------------------------
    public static Observable<String> requestMutationHttp(Context mContext, String tag, String url, Map header, String graphql){

        // [로직 처리 실시]
        return Observable.create(subscriber -> {

            try {

                // ------------------------------------------------------
                // TODO [사전 방어 로직 : body 데이터 값 체크 실시]
                // ------------------------------------------------------
                if (C_Util.stringNotNull(graphql) == false || graphql.startsWith("mutation") == false){
                    try { subscriber.onError(new Throwable("[FAIL] : [requestMutationHttp] : Input Query Is Null or startsWith False")); } catch (Exception ex){ ex.printStackTrace(); }
                    return;
                }
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [사전 방어 로직 : url 데이터 값 체크 실시]
                // ------------------------------------------------------
                if (C_Util.stringNotNull(url) == false){
                    try { subscriber.onError(new Throwable("[FAIL] : [requestMutationHttp] : Input Url Is Null")); } catch (Exception ex){ ex.printStackTrace(); }
                    return;
                }
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [URL 변수 선언 실시]
                // ------------------------------------------------------
                String urlData = String.valueOf(url);
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [BODY에 파라미터 추가 실시]
                // ------------------------------------------------------
                RequestBody requestBody = null;
                String bodyData = "";
                if (C_Util.stringNotNull(graphql) == true) { // [파라미터가 있는 경우]

                    // [json 데이터로 변경 실시]
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("query", graphql);

                    bodyData = jsonObject.toString();
                    requestBody = RequestBody.create(bodyData, MediaType.parse("application/json; charset=utf-8"));

                }
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [HTTP 통신 전문 로그 기록]
                // ------------------------------------------------------
                S_FileManager.appHttpLogSave(mContext, "\n\n\n\n\n\n\n\n\n");
                S_FileManager.appHttpLogSave(mContext, "TAG :: " + String.valueOf(tag));
                HttpLoggingInterceptor httpLogger = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(String message) {
                        if (mContext != null){
                            S_FileManager.appHttpLogSave(mContext, message);
                        }
                    }
                });
                httpLogger.setLevel(HttpLoggingInterceptor.Level.BODY);
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [OK HTTP 객체 선언 실시]
                // ------------------------------------------------------

                //OkHttpClient client = new OkHttpClient();
                OkHttpClient client = new OkHttpClient.Builder()
                        .connectTimeout(TIME_OUT_SECOND, TimeUnit.SECONDS) // [커넥션 제한 시간]
                        .readTimeout(TIME_OUT_SECOND, TimeUnit.SECONDS)
                        .writeTimeout(TIME_OUT_SECOND, TimeUnit.SECONDS)
                        .addInterceptor(httpLogger) // [Http 통신 로그]
                        .retryOnConnectionFailure(false)
                        .build();

                Request.Builder request = new Request.Builder();
                request.addHeader("Content-Type", "application/json"); //TODO [헤더]
                request.addHeader("Cache-Control", "no-cache"); //TODO [헤더]

                // [로그 출력 헤더 삽입]
                Map<String, String> requestHeader = new HashMap<>();
                requestHeader.put("Content-Type", "application/json");
                requestHeader.put("Cache-Control", "no-cache");

                if (C_Util.mapNotNull(header) == true){
                    Set set = header.keySet();
                    Iterator iterator = set.iterator();
                    while(iterator.hasNext()){
                        String key = (String) iterator.next();
                        requestHeader.put(key, String.valueOf(header.get(key)));
                        request.addHeader(key, String.valueOf(header.get(key))); //TODO [헤더 추가]
                    }
                }

                HttpUrl.Builder httpBuilder = HttpUrl.get(URI.create(urlData)).newBuilder();

                if (requestBody != null){
                    request.post(requestBody);
                }
                else {
                    request.post(new FormBody.Builder().build());
                }

                request.url(httpBuilder.build()); //TODO [httpBuilder 추가]
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [요청 로그 출력 실시]
                // ------------------------------------------------------
                S_Log._F_(mContext, ACTIVITY_NAME + " :: requestMutationHttp :: GraphQL Mutation Http [요청] 실시", new String[] {
                        "TAG :: " + String.valueOf(tag),
                        "TYPE :: " + "POST BODY JSON >> REQUEST",
                        "URL :: " + String.valueOf(urlData),
                        "HEADER :: " + String.valueOf(requestHeader),
                        "PARAMS :: " + String.valueOf(bodyData)
                });
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [비동기 처리 (enqueue 사용)]
                // ------------------------------------------------------
                String finalUrlData = urlData;
                client.newCall(request.build()).enqueue(new Callback() {

                    // TODO [성공한 경우]
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String responseUrl = String.valueOf(finalUrlData);
                        final String responseHeader = String.valueOf(response.headers().toString());
                        final String responseCode = String.valueOf(response.code());
                        final String responseData = String.valueOf(response.body().string());


                        // ------------------------------------------------------
                        // [로그 출력]
                        // ------------------------------------------------------
                        S_Log._F_(mContext, ACTIVITY_NAME + " :: requestMutationHttp :: GraphQL Mutation Http [응답] 확인", new String[] {
                                "TAG :: " + String.valueOf(tag),
                                "TYPE :: " + "POST BODY JSON >> RESPONSE",
                                "CODE :: " + String.valueOf(responseCode),
                                "URL :: " + String.valueOf(responseUrl),
                                "HEADER :: " + String.valueOf(responseHeader),
                                "DATA :: " + String.valueOf(responseData)
                        });
                        // ------------------------------------------------------


                        // ------------------------------------------------------
                        // TODO [응답 코드 값 체크 및 리턴 데이터 반환]
                        // ------------------------------------------------------
                        try {
                            int statusCode = response.code();
                            if (statusCode >= 200 && statusCode < 300){ // TODO [정상 응답]

                                // TODO [결과 전송 실시]
                                subscriber.onNext(responseData);
                                subscriber.onComplete();

                            }
                            else { // TODO [에러 응답]
                                subscriber.onError(new Throwable("[ERROR] : : [requestMutationHttp] : " + C_ErrorCheck.getErrorHttpMsg(responseCode)));
                            }
                        }
                        catch (Exception e){
                            S_Log._printStackTrace_(mContext, S_FinalData.LOG_BUG_STATE, null, e);
                            subscriber.onError(new Throwable("[EXCEPTION] : [1] : [requestMutationHttp] : " + e.getMessage()));
                        }
                        // ------------------------------------------------------

                    }

                    //TODO [실패한 경우]
                    @Override
                    public void onFailure(Call call, IOException e) {
                        final String responseUrl = String.valueOf(finalUrlData);
                        final String responseError = String.valueOf(e.getMessage());


                        // ------------------------------------------------------
                        // [로그 출력]
                        // ------------------------------------------------------
                        S_Log._F_(mContext, ACTIVITY_NAME + " :: requestMutationHttp :: GraphQL Mutation Http [에러] 확인", new String[] {
                                "TAG :: " + String.valueOf(tag),
                                "TYPE :: " + "POST BODY JSON >> FAIL",
                                "URL :: " + String.valueOf(responseUrl),
                                "DATA :: " + String.valueOf(responseError)
                        });
                        // ------------------------------------------------------


                        // ------------------------------------------------------
                        // TODO [리턴 데이터 반환]
                        // ------------------------------------------------------
                        try { subscriber.onError(new Throwable("[FAIL] : [requestMutationHttp] : " + responseError)); } catch (Exception ex){ ex.printStackTrace(); }
                        // ------------------------------------------------------
                    }
                });

            } catch (final Exception e){
                // ------------------------------------------------------
                // [로그 출력]
                // ------------------------------------------------------
                S_Log._printStackTrace_(mContext, S_FinalData.LOG_BUG_STATE, new String[] {
                        ACTIVITY_NAME + " :: requestMutationHttp :: GraphQL Mutation Http [EXCEPTION] 확인",
                        "TAG :: " + String.valueOf(tag),
                        "TYPE :: " + "POST BODY JSON >> EXCEPTION",
                        "EXCEPTION :: " + String.valueOf(e.getMessage())
                }, e);
                // ------------------------------------------------------


                // ------------------------------------------------------
                // TODO [리턴 데이터 반환]
                // ------------------------------------------------------
                try { subscriber.onError(new Throwable("[EXCEPTION] : [2] : [requestMutationHttp] : " + String.valueOf(e.getMessage()))); } catch (Exception ex){ ex.printStackTrace(); }
                // ------------------------------------------------------
            }

        });
    }





    // -----------------------------------------------------------------------------------------
    // TODO [SEARCH FAST] : requestSubscriptionHttp
    // -----------------------------------------------------------------------------------------
    // TODO [호출 방법 소스 코드]
    // -----------------------------------------------------------------------------------------
    /*
    try {

        // [URL 주소 선언]
        String url = "https://graphql.postman-echo.com/graphql";

        // [파라미터 생성]
        Map<String, Object> headers = new HashMap<>();
        //headers.put("x-api-key", "sample_key"); // [key 인증시 사용]

        // [GraphQL 요청 형식 선언 : .graphql]
        String graphql = "";
        graphql += "subscription Greetings {\n" +
                            "    greetings\n" +
                            "}";

        // [http 요청 수행]
        C_GraphQL_Client_Module.requestSubscriptionHttp(A_Intro.this, "GraphQL Http 요청", url, headers, graphql) // [http 요청]
                .subscribeOn(AndroidSchedulers.mainThread()) // [Observable (생성자) 로직을 IO 스레드에서 실행 : 백그라운드]
                .observeOn(Schedulers.io()) // [Observer (관찰자) 로직을 메인 스레드에서 실행]
                .subscribe(new Observer<String>() { // [Observable.create 타입 지정]
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }

                    @Override
                    public void onNext(@NonNull String value) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }
    catch (Exception e){
        S_Log._printStackTrace_(null, S_FinalData.LOG_BUG_STATE, null, e);
    }
    // */
    // -----------------------------------------------------------------------------------------
    // TODO [샘플 로그]
    // -----------------------------------------------------------------------------------------
    /*
    V  ===================================================================
    [LOG :: CLASS PLACE :: com.example.javaproject.C_Module.C_GraphQL_Client_Module.lambda$requestSubscriptionHttp$2(C_GraphQL_Client_Module.java:986)]
    ----------------------------------------------------
    [LOG :: NOW TIME :: 2024-09-04 14:05:56 수요일]
    ----------------------------------------------------
    [LOG :: DESCRIPTION :: requestSubscriptionHttp :: GraphQL Subscription Http [요청] 실시]
    ----------------------------------------------------
    [LOG :: TAG :: GraphQL Http 요청]
    ----------------------------------------------------
    [LOG :: TYPE :: POST BODY JSON >> REQUEST]
    ----------------------------------------------------
    [LOG :: URL :: https://graphql.postman-echo.com/graphql]
    ----------------------------------------------------
    [LOG :: HEADER :: {Cache-Control=no-cache, Content-Type=application/json}]
    ----------------------------------------------------
    [LOG :: PARAMS :: {"query":"subscription Greetings {\n    greetings\n}"}]
    V  ===================================================================


    V  ===================================================================
    [LOG :: CLASS PLACE :: com.example.javaproject.C_Module.C_GraphQL_Client_Module$6.onResponse(C_GraphQL_Client_Module.java:1015)]
    ----------------------------------------------------
    [LOG :: NOW TIME :: 2024-09-04 14:05:58 수요일]
    ----------------------------------------------------
    [LOG :: DESCRIPTION :: requestSubscriptionHttp :: GraphQL Subscription Http [응답] 확인]
    ----------------------------------------------------
    [LOG :: TAG :: GraphQL Http 요청]
    ----------------------------------------------------
    [LOG :: TYPE :: POST BODY JSON >> RESPONSE]
    ----------------------------------------------------
    [LOG :: CODE :: 200]
    ----------------------------------------------------
    [LOG :: URL :: https://graphql.postman-echo.com/graphql]
    ----------------------------------------------------
    [LOG :: HEADER :: date: Wed, 04 Sep 2024 05:05:58 GMT
    ----------------------------------------------------
    [LOG :: DATA :: :

             event: next
             data: {"data":{"greetings":"Hi"}}

             event: next
             data: {"data":{"greetings":"Bonjour"}}

             event: next
             data: {"data":{"greetings":"Hola"}}

             event: next
             data: {"data":{"greetings":"Ciao"}}

             event: next
             data: {"data":{"greetings":"Zdravo"}}

             event: complete
             data:

    ]
    V  ===================================================================
    */
    // -----------------------------------------------------------------------------------------
    public static Observable<String> requestSubscriptionHttp(Context mContext, String tag, String url, Map header, String graphql){

        // [로직 처리 실시]
        return Observable.create(subscriber -> {

            try {

                // ------------------------------------------------------
                // TODO [사전 방어 로직 : body 데이터 값 체크 실시]
                // ------------------------------------------------------
                if (C_Util.stringNotNull(graphql) == false || graphql.startsWith("subscription") == false){
                    try { subscriber.onError(new Throwable("[FAIL] : [requestSubscriptionHttp] : Input Query Is Null or startsWith False")); } catch (Exception ex){ ex.printStackTrace(); }
                    return;
                }
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [사전 방어 로직 : url 데이터 값 체크 실시]
                // ------------------------------------------------------
                if (C_Util.stringNotNull(url) == false){
                    try { subscriber.onError(new Throwable("[FAIL] : [requestSubscriptionHttp] : Input Url Is Null")); } catch (Exception ex){ ex.printStackTrace(); }
                    return;
                }
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [URL 변수 선언 실시]
                // ------------------------------------------------------
                String urlData = String.valueOf(url);
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [BODY에 파라미터 추가 실시]
                // ------------------------------------------------------
                RequestBody requestBody = null;
                String bodyData = "";
                if (C_Util.stringNotNull(graphql) == true) { // [파라미터가 있는 경우]

                    // [json 데이터로 변경 실시]
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("query", graphql);

                    bodyData = jsonObject.toString();
                    requestBody = RequestBody.create(bodyData, MediaType.parse("application/json; charset=utf-8"));

                }
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [HTTP 통신 전문 로그 기록]
                // ------------------------------------------------------
                S_FileManager.appHttpLogSave(mContext, "\n\n\n\n\n\n\n\n\n");
                S_FileManager.appHttpLogSave(mContext, "TAG :: " + String.valueOf(tag));
                HttpLoggingInterceptor httpLogger = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(String message) {
                        if (mContext != null){
                            S_FileManager.appHttpLogSave(mContext, message);
                        }
                    }
                });
                httpLogger.setLevel(HttpLoggingInterceptor.Level.BODY);
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [OK HTTP 객체 선언 실시]
                // ------------------------------------------------------
                //OkHttpClient client = new OkHttpClient();
                OkHttpClient client = new OkHttpClient.Builder()
                        .connectTimeout(TIME_OUT_SECOND, TimeUnit.SECONDS) // [커넥션 제한 시간]
                        .readTimeout(TIME_OUT_SECOND, TimeUnit.SECONDS)
                        .writeTimeout(TIME_OUT_SECOND, TimeUnit.SECONDS)
                        .addInterceptor(httpLogger) // [Http 통신 로그]
                        .retryOnConnectionFailure(false)
                        .build();

                Request.Builder request = new Request.Builder();
                request.addHeader("Content-Type", "application/json"); //TODO [헤더]
                request.addHeader("Cache-Control", "no-cache"); //TODO [헤더]

                // [로그 출력 헤더 삽입]
                Map<String, String> requestHeader = new HashMap<>();
                requestHeader.put("Content-Type", "application/json");
                requestHeader.put("Cache-Control", "no-cache");

                if (C_Util.mapNotNull(header) == true){
                    Set set = header.keySet();
                    Iterator iterator = set.iterator();
                    while(iterator.hasNext()){
                        String key = (String) iterator.next();
                        requestHeader.put(key, String.valueOf(header.get(key)));
                        request.addHeader(key, String.valueOf(header.get(key))); //TODO [헤더 추가]
                    }
                }

                HttpUrl.Builder httpBuilder = HttpUrl.get(URI.create(urlData)).newBuilder();

                if (requestBody != null){
                    request.post(requestBody);
                }
                else {
                    request.post(new FormBody.Builder().build());
                }

                request.url(httpBuilder.build()); //TODO [httpBuilder 추가]
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [요청 로그 출력 실시]
                // ------------------------------------------------------
                S_Log._F_(mContext, ACTIVITY_NAME + " :: requestSubscriptionHttp :: GraphQL Subscription Http [요청] 실시", new String[] {
                        "TAG :: " + String.valueOf(tag),
                        "TYPE :: " + "POST BODY JSON >> REQUEST",
                        "URL :: " + String.valueOf(urlData),
                        "HEADER :: " + String.valueOf(requestHeader),
                        "PARAMS :: " + String.valueOf(bodyData)
                });
                // ------------------------------------------------------



                // ------------------------------------------------------
                // TODO [비동기 처리 (enqueue 사용)]
                // ------------------------------------------------------
                String finalUrlData = urlData;
                client.newCall(request.build()).enqueue(new Callback() {

                    // TODO [성공한 경우]
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String responseUrl = String.valueOf(finalUrlData);
                        final String responseHeader = String.valueOf(response.headers().toString());
                        final String responseCode = String.valueOf(response.code());
                        final String responseData = String.valueOf(response.body().string());


                        // ------------------------------------------------------
                        // [로그 출력]
                        // ------------------------------------------------------
                        S_Log._F_(mContext, ACTIVITY_NAME + " :: requestSubscriptionHttp :: GraphQL Subscription Http [응답] 확인", new String[] {
                                "TAG :: " + String.valueOf(tag),
                                "TYPE :: " + "POST BODY JSON >> RESPONSE",
                                "CODE :: " + String.valueOf(responseCode),
                                "URL :: " + String.valueOf(responseUrl),
                                "HEADER :: " + String.valueOf(responseHeader),
                                "DATA :: " + String.valueOf(responseData)
                        });
                        // ------------------------------------------------------


                        // ------------------------------------------------------
                        // TODO [응답 코드 값 체크 및 리턴 데이터 반환]
                        // ------------------------------------------------------
                        try {
                            int statusCode = response.code();
                            if (statusCode >= 200 && statusCode < 300){ // TODO [정상 응답]

                                // TODO [결과 전송 실시]
                                subscriber.onNext(responseData);
                                subscriber.onComplete();

                            }
                            else { // TODO [에러 응답]
                                subscriber.onError(new Throwable("[ERROR] : : [requestSubscriptionHttp] : " + C_ErrorCheck.getErrorHttpMsg(responseCode)));
                            }
                        }
                        catch (Exception e){
                            S_Log._printStackTrace_(mContext, S_FinalData.LOG_BUG_STATE, null, e);
                            subscriber.onError(new Throwable("[EXCEPTION] : [1] : [requestSubscriptionHttp] : " + e.getMessage()));
                        }
                        // ------------------------------------------------------

                    }

                    //TODO [실패한 경우]
                    @Override
                    public void onFailure(Call call, IOException e) {
                        final String responseUrl = String.valueOf(finalUrlData);
                        final String responseError = String.valueOf(e.getMessage());

                        // ------------------------------------------------------
                        // [로그 출력]
                        // ------------------------------------------------------
                        S_Log._F_(mContext, ACTIVITY_NAME + " :: requestSubscriptionHttp :: GraphQL Subscription Http [에러] 확인", new String[] {
                                "TAG :: " + String.valueOf(tag),
                                "TYPE :: " + "POST BODY JSON >> FAIL",
                                "URL :: " + String.valueOf(responseUrl),
                                "DATA :: " + String.valueOf(responseError)
                        });
                        // ------------------------------------------------------


                        // ------------------------------------------------------
                        // TODO [리턴 데이터 반환]
                        // ------------------------------------------------------
                        try { subscriber.onError(new Throwable("[FAIL] : [requestSubscriptionHttp] : " + responseError)); } catch (Exception ex){ ex.printStackTrace(); }
                        // ------------------------------------------------------
                    }
                });

            } catch (final Exception e){
                // ------------------------------------------------------
                // [로그 출력]
                // ------------------------------------------------------
                S_Log._printStackTrace_(mContext, S_FinalData.LOG_BUG_STATE, new String[] {
                        ACTIVITY_NAME + " :: requestSubscriptionHttp :: GraphQL Subscription Http [EXCEPTION] 확인",
                        "TAG :: " + String.valueOf(tag),
                        "TYPE :: " + "POST BODY JSON >> EXCEPTION",
                        "EXCEPTION :: " + String.valueOf(e.getMessage())
                }, e);
                // ------------------------------------------------------


                // ------------------------------------------------------
                // TODO [리턴 데이터 반환]
                // ------------------------------------------------------
                try { subscriber.onError(new Throwable("[EXCEPTION] : [2] : [requestSubscriptionHttp] : " + String.valueOf(e.getMessage()))); } catch (Exception ex){ ex.printStackTrace(); }
                // ------------------------------------------------------
            }

        });
    }



} // TODO [클래스 종료]

 

반응형
Comments