> ## Documentation Index
> Fetch the complete documentation index at: https://docs-staging.auth0-mintlify.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Auth0.Android カスタムネットワーククライアント

> Auth0 Android SDKが行うリクエストをカスタマイズするために、カスタムネットワーククライアントを指定する方法について説明します。

export const AuthCodeBlock = ({filename, icon, language, highlight, children}) => {
  const [displayText, setDisplayText] = useState(children);
  const [copyText, setCopyText] = useState(children);
  const wrapperRef = React.useRef(null);
  useEffect(() => {
    let unsubscribe = null;
    function init() {
      if (!window.autorun || !window.rootStore) {
        return;
      }
      unsubscribe = window.autorun(() => {
        let processedChildrenForDisplay = children;
        let processedChildrenForCopy = children;
        for (const [key, value] of window.rootStore.variableStore.values.entries()) {
          const escapedKey = key.replaceAll(/[.*+?^${}()|[\]\\]/g, (String.raw)`\$&`);
          let displayValue = value;
          if (key === "{yourClientSecret}" && value !== "{yourClientSecret}") {
            displayValue = value.substring(0, 3) + "*****MASKED*****";
          }
          processedChildrenForDisplay = processedChildrenForDisplay.replaceAll(new RegExp(escapedKey, "g"), displayValue);
          processedChildrenForCopy = processedChildrenForCopy.replaceAll(new RegExp(escapedKey, "g"), value);
        }
        setDisplayText(processedChildrenForDisplay);
        setCopyText(processedChildrenForCopy);
      });
    }
    if (window.rootStore) {
      init();
    } else {
      window.addEventListener("adu:storeReady", init);
    }
    return () => {
      window.removeEventListener("adu:storeReady", init);
      unsubscribe?.();
    };
  }, [children]);
  useEffect(() => {
    if (!wrapperRef.current) return;
    const originalWriteText = navigator.clipboard.writeText.bind(navigator.clipboard);
    let isOverriding = false;
    const handleClick = e => {
      const button = e.target.closest('[data-testid="copy-code-button"]');
      if (!button || !wrapperRef.current.contains(button)) return;
      isOverriding = true;
      navigator.clipboard.writeText = text => {
        if (isOverriding) {
          isOverriding = false;
          navigator.clipboard.writeText = originalWriteText;
          return originalWriteText(copyText);
        }
        return originalWriteText(text);
      };
      setTimeout(() => {
        if (isOverriding) {
          isOverriding = false;
          navigator.clipboard.writeText = originalWriteText;
        }
      }, 100);
    };
    const wrapper = wrapperRef.current;
    wrapper.addEventListener('click', handleClick, true);
    return () => {
      wrapper.removeEventListener('click', handleClick, true);
      if (navigator.clipboard.writeText !== originalWriteText) {
        navigator.clipboard.writeText = originalWriteText;
      }
    };
  }, [copyText]);
  return <div ref={wrapperRef}>
      <CodeBlock filename={filename} icon={icon} language={language} lines highlight={highlight}>
        {displayText}
      </CodeBlock>
    </div>;
};

export const codeExample1 = `val netClient = DefaultClient.Builder()
    .connectTimeout(30)
    .readTimeout(30)
    .writeTimeout(30)
    .callTimeout(120)
    .build()

val account = Auth0.getInstance("{yourClientId}", "{yourDomain}")
account.networkingClient = netClient`;

export const codeExample2 = `val netClient = DefaultClient.Builder()
    .enableLogging(true)
    .build()

val account = Auth0.getInstance("{yourClientId}", "{yourDomain}")
account.networkingClient = netClient`;

export const codeExample3 = `val netClient = DefaultClient.Builder()
    .defaultHeaders(mapOf("YOUR_HEADER_NAME" to "YOUR_HEADER_VALUE"))
    .build()

val account = Auth0.getInstance("{yourClientId}", "{yourDomain}")
account.networkingClient = netClient`;

export const codeExample5 = `val netClient = DefaultClient.Builder()
    .enableLogging(true)
    .logger(HttpLoggingInterceptor.Logger { message -> Log.d("Auth0Http", message) })
    .build()`;

export const codeExample4 = `class CustomNetClient : NetworkingClient {
    override fun load(url: String, options: RequestOptions): ServerResponse {
        // 指定されたオプションを使って、指定したURLへのリクエストを作成して実行する
        val response = // ...

        // 受信したレスポンスデータからServerResponseを生成して返す
        return ServerResponse(responseCode, responseBody, responseHeaders)
    }
}

val account = Auth0.getInstance("{yourClientId}", "{yourDomain}")
account.networkingClient = netClient`;

Auth0 クラスに `NetworkingClient` を設定することで、SDKがリクエストを行う方法をカスタマイズできます。デフォルトのクライアントは、カスタムのタイムアウト値、すべてのリクエストで送信されるヘッダー、非本番環境でのデバッグ向けのリクエスト／レスポンスのログに対応しています。より高度なユースケースでは、独自の `NetworkingClient` 実装を指定できます。

<h2 id="configure-timeouts">
  タイムアウトを構成する
</h2>

<AuthCodeBlock children={codeExample1} language="kotlin" />

<h2 id="configure-logging">
  ログの設定
</h2>

<AuthCodeBlock children={codeExample2} language="kotlin" />

カスタムのloggerを指定して、logsの出力先を制御することもできます。

<AuthCodeBlock children={codeExample5} language="kotlin" />

<h2 id="set-additional-headers-for-all-requests">
  すべてのリクエストに追加ヘッダーを設定する
</h2>

<AuthCodeBlock children={codeExample3} language="kotlin" />

<h2 id="configure-advanced-networking">
  高度なネットワーク設定を行う
</h2>

高度なネットワーク設定を行うには、`NetworkingClient` のカスタム実装を指定してください。これは、独自のネットワーククライアントを再利用したい場合や、プロキシを設定したい場合などに便利です。

<AuthCodeBlock children={codeExample4} language="kotlin" />
