View Source WeChat.Storage.Hub behaviour (elixir_wechat v0.4.6)

The storage adapter specification for WeChat common application.

Since we need to temporarily storage some key data(e.g. 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 common application.

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

writing-custom-storage-adapter

Writing custom storage adapter

Example for WeChat Official Account Platform application

defmodule MyAppStorageHub do
  @behaviour WeChat.Storage.Hub

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

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

  @impl true
  def fetch_ticket(appid, type, args) do
    ticket = "Get ticket by appid from your persistence..."
    {:ok, ticket}
  end

  @impl true
  def refresh_access_token(appid, access_token, args) do
    access_token = "Refresh access_token by appid from your persistence..."
    {:ok, %WeChat.Token{access_token: access_token}}
  end

  @impl true
  def save_access_token(appid, access_token, args) do
    # Save access_token by appid to your persistence
  end

  @impl true
  def save_ticket(appid, ticket, type, args) do
    # Save ticket by appid to your persistence
  end
end

Use MyAppStorageHub

Global configure MyAppStorageHub

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

defmodule Client do
  use WeChat,
    adapter_storage: MyAppStorageHub
end

Dynamically set MyAppStorageHub when call WeChat.request/2

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

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

Fetch access_token of WeChat common application.

Fetch secret_key of WeChat common application.

Fetch ticket of WeChat common application, the option of type parameter is "wx_card" or "jsapi"(refer WeChat Official document).

Refresh access_token of WeChat common application.

Save access_token of WeChat common application.

Save ticket of WeChat common application, the option of type parameter is "wx_card" or "jsapi"(refer WeChat Official document).

Link to this section Callbacks

Link to this callback

fetch_access_token(appid, args)

View Source
@callback fetch_access_token(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()
     }}

Fetch access_token of WeChat common application.

Link to this callback

fetch_secret_key(appid, args)

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

Fetch secret_key of WeChat common application.

Link to this callback

fetch_ticket(appid, type, args)

View Source
@callback fetch_ticket(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 ticket of WeChat common application, the option of type parameter is "wx_card" or "jsapi"(refer WeChat Official document).

Link to this callback

refresh_access_token(appid, access_token, args)

View Source
@callback refresh_access_token(
  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 access_token of WeChat common application.

Link to this callback

save_access_token(appid, access_token, args)

View Source
@callback save_access_token(
  appid :: String.t(),
  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 common application.

Link to this callback

save_ticket(appid, ticket, type, args)

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

Save ticket of WeChat common application, the option of type parameter is "wx_card" or "jsapi"(refer WeChat Official document).