WeChat.Storage.ComponentHub behaviour (elixir_wechat v0.4.5) View Source

The storage adapter specification for WeChat component application.

Since we need to temporarily storage some key data(e.g. access_token/component_access_token) for invoking WeChat APIs, this module is used for customizing the persistence when use elixir_wechat in the centralization nodes(hereinafter "hub") side of WeChat component application.

Notice: the scenario as a hub, we need to implement the completed functions to maintain the persistence.

Writing custom storage adapter

Example for WeChat 3rd-party Platform application

defmodule MyComponentAppStorageHub do
  @behaviour WeChat.Storage.ComponentHub

  @impl true
  def fetch_access_token(appid, authorizer_appid, args) do
    access_token = "Get authorizer's access_token for WeChat component application from your persistence..."
    {:ok, %WeChat.Token{access_token: access_token}}
  end

  @impl true
  def fetch_component_access_token(appid, args) do
    access_token = "Get component access_token for WeChat component application from your persistence..."
    {:ok, %WeChat.Token{access_token: access_token}}
  end

  @impl true
  def fetch_component_verify_ticket(appid, args) do
    component_verify_ticket = "Get component_verify_ticket for WeChat component application from your persistence..."
    {:ok, component_verify_ticket}
  end

  @impl true
  def fetch_secret_key(appid, args) do
    secret_key = "Get component application secret_key from your persistence..."
    secret_key
  end

  @impl true
  def fetch_ticket(appid, authorizer_appid, type, args) do
    ticket = "Get authorizer account's ticket for WeChat component application from your persistence..."
    {:ok, ticket}
  end

  @impl true
  def refresh_access_token(appid, authorizer_appid, access_token, args) do
    access_token = "Refresh authorizer's access_token for WeChat component application from your persistence..."
    {:ok, %WeChat.Token{access_token: access_token}}
  end

  @impl true
  def refresh_component_access_token(appid, component_access_token, args) do
    access_token = "Refresh access_token of WeChat component application..."
    {:ok, %WeChat.Token{access_token: access_token}}
  end

  @impl true
  def save_access_token(appid, authorizer_appid, access_token, refresh_token, args) do
    # Save authorizer's access_token and its refresh_token to your persistence
  end

  @impl true
  def save_component_access_token(appid, component_access_token, args) do
    # Save access_token of WeChat component application
  end

  @impl true
  def save_component_verify_ticket(appid, component_verify_ticket, args) do
    # Save component_verify_ticket to your persistence
  end

  @impl true
  def save_ticket(appid, authorizer_appid, ticket, type, args) do
    # Save authorizer account's ticket to your persistence
  end
end

Use MyComponentAppStorageHub

Global configure MyComponentAppStorageHub

defmodule Client do
  use WeChat,
    adapter_storage: {MyComponentAppStorageHub, args}
end

defmodule Client do
  use WeChat,
    adapter_storage: MyComponentAppStorageHub
end

Dynamically set MyComponentAppStorageHub when call WeChat.request/2

WeChat.request(:post, url: ..., adapter_storage: {MyComponentAppStorageHub, args}, ...)
WeChat.request(:post, url: ..., adapter_storage: MyComponentAppStorageHub, ...)

Notice: The above args will be returned back into each implement of callback function, if not input it, args will be as an empty list in callback.

Link to this section Summary

Callbacks

Get authorizer's access_token for WeChat component application.

Get access_token of WeChat component application.

Fetch component_verify_ticket of WeChat component application.

Get secret_key of WeChat component application.

Fetch authorizer account's ticket in WeChat component application.

Refresh authorizer's access_token for WeChat component application.

Refresh access_token of WeChat component application.

Save authorizer's access_token for WeChat component application.

Save access_token of WeChat component application.

Save component_verify_ticket of WeChat component application.

Save authorizer account's ticket in WeChat component application.

Link to this section Callbacks

Link to this callback

fetch_access_token(appid, authorizer_appid, args)

View Source

Specs

fetch_access_token(
  appid :: String.t(),
  authorizer_appid :: String.t(),
  args :: term()
) ::
  {:ok,
   %WeChat.Token{
     access_token: term(),
     expires_in: term(),
     refresh_token: term(),
     timestamp: term()
   }}
  | {:error,
     %WeChat.Error{
       __exception__: term(),
       errcode: term(),
       http_status: term(),
       message: term(),
       reason: term()
     }}

Get authorizer's access_token for WeChat component application.

Link to this callback

fetch_component_access_token(appid, args)

View Source

Specs

fetch_component_access_token(appid :: String.t(), args :: term()) ::
  {:ok,
   %WeChat.Token{
     access_token: term(),
     expires_in: term(),
     refresh_token: term(),
     timestamp: term()
   }}

Get access_token of WeChat component application.

Link to this callback

fetch_component_verify_ticket(appid, args)

View Source

Specs

fetch_component_verify_ticket(appid :: String.t(), args :: term()) ::
  {:ok, String.t()}
  | {:error,
     %WeChat.Error{
       __exception__: term(),
       errcode: term(),
       http_status: term(),
       message: term(),
       reason: term()
     }}

Fetch component_verify_ticket of WeChat component application.

Link to this callback

fetch_secret_key(appid, args)

View Source

Specs

fetch_secret_key(appid :: String.t(), args :: term()) :: {:ok, String.t()} | nil

Get secret_key of WeChat component application.

Link to this callback

fetch_ticket(appid, authorizer_appid, type, args)

View Source

Specs

fetch_ticket(
  appid :: String.t(),
  authorizer_appid :: String.t(),
  type :: String.t(),
  args :: term()
) ::
  {:ok,
   %WeChat.Ticket{
     expires_in: term(),
     timestamp: term(),
     type: term(),
     value: term()
   }}
  | {:error,
     %WeChat.Error{
       __exception__: term(),
       errcode: term(),
       http_status: term(),
       message: term(),
       reason: term()
     }}

Fetch authorizer account's ticket in WeChat component application.

Link to this callback

refresh_access_token(appid, authorizer_appid, access_token, args)

View Source

Specs

refresh_access_token(
  appid :: String.t(),
  authorizer_appid :: String.t(),
  access_token :: String.t(),
  args :: term()
) ::
  {:ok,
   %WeChat.Token{
     access_token: term(),
     expires_in: term(),
     refresh_token: term(),
     timestamp: term()
   }}
  | {:error,
     %WeChat.Error{
       __exception__: term(),
       errcode: term(),
       http_status: term(),
       message: term(),
       reason: term()
     }}

Refresh authorizer's access_token for WeChat component application.

Link to this callback

refresh_component_access_token(appid, component_access_token, args)

View Source

Specs

refresh_component_access_token(
  appid :: String.t(),
  component_access_token :: String.t(),
  args :: term()
) ::
  {:ok,
   %WeChat.Token{
     access_token: term(),
     expires_in: term(),
     refresh_token: term(),
     timestamp: term()
   }}
  | {:error,
     %WeChat.Error{
       __exception__: term(),
       errcode: term(),
       http_status: term(),
       message: term(),
       reason: term()
     }}

Refresh access_token of WeChat component application.

Link to this callback

save_access_token(appid, authorizer_appid, access_token, refresh_token, args)

View Source

Specs

save_access_token(
  appid :: String.t(),
  authorizer_appid :: String.t(),
  access_token :: String.t(),
  refresh_token :: String.t(),
  args :: term()
) :: term()

Save authorizer's access_token for WeChat component application.

Link to this callback

save_component_access_token(appid, component_access_token, args)

View Source

Specs

save_component_access_token(
  appid :: String.t(),
  component_access_token :: String.t(),
  args :: term()
) ::
  {:ok,
   %WeChat.Token{
     access_token: term(),
     expires_in: term(),
     refresh_token: term(),
     timestamp: term()
   }}

Save access_token of WeChat component application.

Link to this callback

save_component_verify_ticket(appid, component_verify_ticket, args)

View Source

Specs

save_component_verify_ticket(
  appid :: String.t(),
  component_verify_ticket :: String.t(),
  args :: term()
) :: term()

Save component_verify_ticket of WeChat component application.

Link to this callback

save_ticket(appid, authorizer_appid, ticket, type, args)

View Source

Specs

save_ticket(
  appid :: String.t(),
  authorizer_appid :: String.t(),
  ticket :: String.t(),
  type :: String.t(),
  args :: term()
) ::
  {:ok,
   %WeChat.Ticket{
     expires_in: term(),
     timestamp: term(),
     type: term(),
     value: term()
   }}

Save authorizer account's ticket in WeChat component application.