-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
180 lines (162 loc) · 4.07 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package bugyoclient
import (
"errors"
"github.com/PuerkitoBio/goquery"
"log"
"net/http"
"net/http/cookiejar"
"net/http/httputil"
"net/url"
"strings"
)
const userAgent = "Bugyo-Client-Go/1.0.0"
const (
urlLoginPage = "https://id.obc.jp/%s"
urlCheckAuthenticationMethod = "https://id.obc.jp/%s/login/CheckAuthenticationMethod"
urlAuthenticate = "https://id.obc.jp/%s/login/login/?Length=5"
urlUserCode = "https://id.obc.jp/%s/omredirect/redirect/"
urlPunchmarkPage = "https://hromssp.obc.jp/%s/%s/timeclock/punchmark/"
urlInsertReadDateTime = "https://hromssp.obc.jp/%s/%s/TimeClock/InsertReadDateTime/"
)
type BugyoClient interface {
Login() error
IsLoggedIn() bool
Punchmark(clockType ClockType) error
}
type bugyoClient struct {
client *http.Client
config *BugyoConfig
token string
userCode string
lastReq *url.URL
debug bool
}
type BugyoConfig struct {
TenantCode string
OBCiD string
Password string
}
type Options func(*options)
type options struct {
debug bool
}
func WithDebug() Options {
return func(ops *options) {
ops.debug = true
}
}
func NewClient(config *BugyoConfig, opts ...Options) (BugyoClient, error) {
var opt options
for _, o := range opts {
o(&opt)
}
jar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
client := &http.Client{
Jar: jar,
}
if config == nil {
return nil, errors.New("invalid argument: config required")
}
if config.TenantCode == "" {
return nil, errors.New("invalid argument: TenantCode required")
}
if config.OBCiD == "" {
return nil, errors.New("invalid argument: OBCiD required")
}
if config.Password == "" {
return nil, errors.New("invalid argument: Password required")
}
return &bugyoClient{client: client, config: config, debug: opt.debug}, nil
}
func (b *bugyoClient) get(uri string) (*goquery.Document, error) {
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", userAgent)
defer b.setLastReq(req.URL)
if b.debug {
reqDump, err := httputil.DumpRequest(req, false)
if err != nil {
return nil, err
}
log.Printf("request=%q\n", reqDump)
}
res, err := b.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if b.debug {
resDump, err := httputil.DumpResponse(res, true)
if err != nil {
return nil, err
}
log.Printf("response=%q\n", resDump)
}
if res.StatusCode != http.StatusOK {
log.Printf("status code error: %d %s", res.StatusCode, res.Status)
return nil, errors.New(res.Status)
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, err
}
return doc, nil
}
// nolint:unparam
func (b *bugyoClient) post(uri string, body url.Values) (*goquery.Document, error) {
req, err := http.NewRequest(http.MethodPost, uri, strings.NewReader(body.Encode()))
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", userAgent)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("__RequestVerificationToken", b.token)
req.Header.Add("X-Requested-With", "XMLHttpRequest")
if ref := b.refererForURL(b.lastReq); ref != "" {
req.Header.Set("Referer", ref)
}
defer b.setLastReq(req.URL)
if b.debug {
reqDump, err := httputil.DumpRequest(req, true)
if err != nil {
return nil, err
}
log.Printf("request=%q\n", reqDump)
}
res, err := b.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if b.debug {
resDump, err := httputil.DumpResponse(res, true)
if err != nil {
return nil, err
}
log.Printf("response=%q\n", resDump)
}
if res.StatusCode != http.StatusOK {
log.Printf("status code error: %d %s", res.StatusCode, res.Status)
return nil, errors.New(res.Status)
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, err
}
b.lastReq = req.URL
return doc, nil
}
func (b *bugyoClient) refererForURL(lastReq *url.URL) string {
if lastReq == nil {
return ""
}
return lastReq.String()
}
func (b *bugyoClient) setLastReq(lastReq *url.URL) {
b.lastReq = lastReq
}