Skip to content

Commit

Permalink
ChannelAPIClient shouldn't require channelToken
Browse files Browse the repository at this point in the history
  • Loading branch information
tokuhirom committed Nov 17, 2023
1 parent d920a8d commit cdff9ce
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{# @pebvariable name="generatorClass" type="java.lang.String" -#}
{# @pebvariable name="classname" type="java.lang.String" -#}
{# @pebvariable name="authenticated" type="java.lang.Boolean" -#}
{# @pebvariable name="authMethods" type="java.util.ArrayList<org.openapitools.codegen.CodegenSecurity>" -#}
{% include "./licenseInfo.pebble" %}
//go:generate python3 ../../generate-code.py
{# @pebvariable name="packageName" type="java.lang.String" #}
Expand All @@ -27,21 +28,28 @@ import (
type {{classname}} struct {
httpClient *http.Client
endpoint *url.URL
{% if authMethods != null -%}
channelToken string
{% endif -%}
ctx context.Context
}

// {{ classname }}Option type
type {{ classname }}Option func (* {{ classname }}) error

// New returns a new bot client instance.
func New{{ classname }}(channelToken string, options ...{{classname}}Option) (*{{ classname }}, error) {
func New{{ classname }}({% if authMethods != null %}channelToken string, {% endif %}options ...{{classname}}Option) (*{{ classname }}, error) {
{% if authMethods != null -%}
if channelToken == "" {
return nil, errors.New("missing channel access token")
}

{% endif -%}

c := &{{ classname }}{
{% if authMethods != null -%}
channelToken: channelToken,
{% endif -%}
httpClient: http.DefaultClient,
}

Expand All @@ -67,9 +75,11 @@ func (call *{{ classname }}) WithContext(ctx context.Context) *{{ classname }} {
}

func (client *{{ classname }}) Do(req *http.Request) (*http.Response, error) {
{% if authMethods != null -%}
if client.channelToken != "" {
req.Header.Set("Authorization", "Bearer "+client.channelToken)
}
{% endif -%}
req.Header.Set("User-Agent", "LINE-BotSDK-Go/"+linebot.GetVersion())
if client.ctx != nil {
req = req.WithContext(client.ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{# @pebvariable name="operations" type="org.openapitools.codegen.model.OperationMap" -#}
{# @pebvariable name="packageName" type="String" -#}
{# @pebvariable name="classname" type="String" -#}
{# @pebvariable name="authMethods" type="java.util.ArrayList<org.openapitools.codegen.CodegenSecurity>" -#}
package tests

import (
Expand Down Expand Up @@ -47,7 +48,7 @@ func Test{{ op.operationId }}(t *testing.T) {
)

client, err := {{ packageName }}.New{{ classname }}(
"MY_CHANNEL_TOKEN",
{% if authMethods != null %}"MY_CHANNEL_TOKEN",{% endif %}
{{ packageName }}.WithEndpoint(server.URL),
)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions linebot/channel_access_token/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ model_issue_channel_access_token_response.go
model_issue_short_lived_channel_access_token_response.go
model_issue_stateless_channel_access_token_response.go
model_verify_channel_access_token_response.go
tests/api/api_channel_access_token_test_test.go
20 changes: 5 additions & 15 deletions linebot/channel_access_token/api_channel_access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
Expand All @@ -36,24 +35,18 @@ import (
)

type ChannelAccessTokenAPI struct {
httpClient *http.Client
endpoint *url.URL
channelToken string
ctx context.Context
httpClient *http.Client
endpoint *url.URL
ctx context.Context
}

// ChannelAccessTokenAPIOption type
type ChannelAccessTokenAPIOption func(*ChannelAccessTokenAPI) error

// New returns a new bot client instance.
func NewChannelAccessTokenAPI(channelToken string, options ...ChannelAccessTokenAPIOption) (*ChannelAccessTokenAPI, error) {
if channelToken == "" {
return nil, errors.New("missing channel access token")
}

func NewChannelAccessTokenAPI(options ...ChannelAccessTokenAPIOption) (*ChannelAccessTokenAPI, error) {
c := &ChannelAccessTokenAPI{
channelToken: channelToken,
httpClient: http.DefaultClient,
httpClient: http.DefaultClient,
}

u, err := url.ParseRequestURI("https://api.line.me")
Expand All @@ -78,9 +71,6 @@ func (call *ChannelAccessTokenAPI) WithContext(ctx context.Context) *ChannelAcce
}

func (client *ChannelAccessTokenAPI) Do(req *http.Request) (*http.Response, error) {
if client.channelToken != "" {
req.Header.Set("Authorization", "Bearer "+client.channelToken)
}
req.Header.Set("User-Agent", "LINE-BotSDK-Go/"+linebot.GetVersion())
if client.ctx != nil {
req = req.WithContext(client.ctx)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,262 @@
package tests

import (
"log"
"net/http"
"net/http/httptest"
"testing"

"github.com/line/line-bot-sdk-go/v7/linebot/channel_access_token"
)

func TestGetsAllValidChannelAccessTokenKeyIds(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
}),
)

client, err := channel_access_token.NewChannelAccessTokenAPI(

channel_access_token.WithEndpoint(server.URL),
)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
resp, err := client.GetsAllValidChannelAccessTokenKeyIds(
"hello",

"hello",
)
if err != nil {
t.Fatalf("Failed to call API: %v", err)
}
log.Printf("Got response: %v", resp)
}

func TestIssueChannelToken(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
t.Fatalf("Invalid content-type: %s", r.Header.Get("Content-Type"))
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
}),
)

client, err := channel_access_token.NewChannelAccessTokenAPI(

channel_access_token.WithEndpoint(server.URL),
)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
resp, err := client.IssueChannelToken(
"hello",

"hello",

"hello",
)
if err != nil {
t.Fatalf("Failed to call API: %v", err)
}
log.Printf("Got response: %v", resp)
}

func TestIssueChannelTokenByJWT(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
t.Fatalf("Invalid content-type: %s", r.Header.Get("Content-Type"))
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
}),
)

client, err := channel_access_token.NewChannelAccessTokenAPI(

channel_access_token.WithEndpoint(server.URL),
)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
resp, err := client.IssueChannelTokenByJWT(
"hello",

"hello",

"hello",
)
if err != nil {
t.Fatalf("Failed to call API: %v", err)
}
log.Printf("Got response: %v", resp)
}

func TestIssueStatelessChannelToken(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
t.Fatalf("Invalid content-type: %s", r.Header.Get("Content-Type"))
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
}),
)

client, err := channel_access_token.NewChannelAccessTokenAPI(

channel_access_token.WithEndpoint(server.URL),
)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
resp, err := client.IssueStatelessChannelToken(
"hello",

"hello",

"hello",

"hello",

"hello",
)
if err != nil {
t.Fatalf("Failed to call API: %v", err)
}
log.Printf("Got response: %v", resp)
}

func TestRevokeChannelToken(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
t.Fatalf("Invalid content-type: %s", r.Header.Get("Content-Type"))
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
}),
)

client, err := channel_access_token.NewChannelAccessTokenAPI(

channel_access_token.WithEndpoint(server.URL),
)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
resp, err := client.RevokeChannelToken(
"hello",
)
if err != nil {
t.Fatalf("Failed to call API: %v", err)
}
log.Printf("Got response: %v", resp)
}

func TestRevokeChannelTokenByJWT(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
t.Fatalf("Invalid content-type: %s", r.Header.Get("Content-Type"))
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
}),
)

client, err := channel_access_token.NewChannelAccessTokenAPI(

channel_access_token.WithEndpoint(server.URL),
)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
resp, err := client.RevokeChannelTokenByJWT(
"hello",

"hello",

"hello",
)
if err != nil {
t.Fatalf("Failed to call API: %v", err)
}
log.Printf("Got response: %v", resp)
}

func TestVerifyChannelToken(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
t.Fatalf("Invalid content-type: %s", r.Header.Get("Content-Type"))
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
}),
)

client, err := channel_access_token.NewChannelAccessTokenAPI(

channel_access_token.WithEndpoint(server.URL),
)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
resp, err := client.VerifyChannelToken(
"hello",
)
if err != nil {
t.Fatalf("Failed to call API: %v", err)
}
log.Printf("Got response: %v", resp)
}

func TestVerifyChannelTokenByJWT(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
}),
)

client, err := channel_access_token.NewChannelAccessTokenAPI(

channel_access_token.WithEndpoint(server.URL),
)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
resp, err := client.VerifyChannelTokenByJWT(
"hello",
)
if err != nil {
t.Fatalf("Failed to call API: %v", err)
}
log.Printf("Got response: %v", resp)
}

0 comments on commit cdff9ce

Please sign in to comment.