Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChannelAPIClient shouldn't require channelToken #392

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}