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

Support issue link token api #94

Merged
merged 2 commits into from
Jul 17, 2018
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
56 changes: 56 additions & 0 deletions linebot/account_link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2016 LINE Corporation
//
// LINE Corporation licenses this file to you under the Apache License,
// version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package linebot

import (
"context"
"fmt"
)

// IssueLinkToken method
// https://developers.line.me/en/reference/messaging-api/#issue-link-token
func (client *Client) IssueLinkToken(userID string) *IssueLinkTokenCall {
return &IssueLinkTokenCall{
c: client,
userID: userID,
}
}

// IssueLinkTokenCall type
type IssueLinkTokenCall struct {
c *Client
ctx context.Context

userID string
}

// WithContext method
func (call *IssueLinkTokenCall) WithContext(ctx context.Context) *IssueLinkTokenCall {
call.ctx = ctx
return call
}

// Do method
func (call *IssueLinkTokenCall) Do() (*LinkTokenResponse, error) {
endpoint := fmt.Sprintf(APIEndpointLinkToken, call.userID)
res, err := call.c.post(call.ctx, endpoint, nil)
if res != nil && res.Body != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
return decodeToLinkTokenResponse(res)
}
93 changes: 93 additions & 0 deletions linebot/account_link_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2016 LINE Corporation
//
// LINE Corporation licenses this file to you under the Apache License,
// version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package linebot

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)

func TestIssueLinkToken(t *testing.T) {
type want struct {
RequestBody []byte
Response *LinkTokenResponse
Error error
}
var testCases = []struct {
UserID string
Response []byte
ResponseCode int
Want want
}{
{
UserID: "u206d25c2ea6bd87c17655609a1c37cb8",
ResponseCode: 200,
Response: []byte(`{"linkToken":"NMZTNuVrPTqlr2IF8Bnymkb7rXfYv5EY"}`),
Want: want{
RequestBody: []byte(""),
Response: &LinkTokenResponse{LinkToken: "NMZTNuVrPTqlr2IF8Bnymkb7rXfYv5EY"},
},
},
}

var currentTestIdx int
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
tc := testCases[currentTestIdx]
if r.Method != http.MethodPost {
t.Errorf("Method %s; want %s", r.Method, http.MethodPost)
}
endpoint := fmt.Sprintf(APIEndpointLinkToken, tc.UserID)
if r.URL.Path != endpoint {
t.Errorf("URLPath %s; want %s", r.URL.Path, endpoint)
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(body, tc.Want.RequestBody) {
t.Errorf("RequestBody\n %s; want\n %s", body, tc.Want.RequestBody)
}
w.WriteHeader(tc.ResponseCode)
w.Write(tc.Response)
}))
defer server.Close()
client, err := mockClient(server)
if err != nil {
t.Fatal(err)
}
for i, tc := range testCases {
currentTestIdx = i
res, err := client.IssueLinkToken(tc.UserID).Do()
if tc.Want.Error != nil {
if !reflect.DeepEqual(err, tc.Want.Error) {
t.Errorf("Error %d %q; want %q", i, err, tc.Want.Error)
}
} else {
if err != nil {
t.Error(err)
}
}
if tc.Want.Response != nil {
if !reflect.DeepEqual(res, tc.Want.Response) {
t.Errorf("Response %d %q; want %q", i, res, tc.Want.Response)
}
}
}
}
2 changes: 2 additions & 0 deletions linebot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const (
APIEndpointAddLIFFAPP = "/liff/v1/apps"
APIEndpointUpdateLIFFAPP = "/liff/v1/apps/%s/view"
APIEndpointDeleteLIFFAPP = "/liff/v1/apps/%s"

APIEndpointLinkToken = "/v2/bot/user/%s/linkToken"
)

// Client type
Expand Down
17 changes: 17 additions & 0 deletions linebot/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ type LIFFResponse struct {
Apps []LIFFAPP `json:"apps"`
}

// LinkTokenResponse type
type LinkTokenResponse struct {
LinkToken string `json:"linkToken"`
}

func checkResponse(res *http.Response) error {
if res.StatusCode != http.StatusOK {
decoder := json.NewDecoder(res.Body)
Expand Down Expand Up @@ -205,3 +210,15 @@ func decodeToLIFFIDResponse(res *http.Response) (*LIFFIDResponse, error) {
}
return &result, nil
}

func decodeToLinkTokenResponse(res *http.Response) (*LinkTokenResponse, error) {
if err := checkResponse(res); err != nil {
return nil, err
}
decoder := json.NewDecoder(res.Body)
result := LinkTokenResponse{}
if err := decoder.Decode(&result); err != nil {
return nil, err
}
return &result, nil
}