@@ -41,8 +41,8 @@ type StateResponse struct {
4141}
4242
4343var (
44- errTooManyRequests = errors .New ("server responses 429 too many request" )
45- errUnauthorized = errors .New ("server responses 401 unauthorized request" )
44+ ErrTooManyRequests = errors .New ("server responses 429 too many request" )
45+ ErrUnauthorized = errors .New ("server responses 401 unauthorized request" )
4646)
4747
4848// client exposes the functions allowed to talk with the Timehook API
@@ -52,29 +52,19 @@ type client struct {
5252}
5353
5454// RegisterAndPoll starts a long running process for a single webhook and
55- // returns three channels: first one for regular messages, second one for
56- // errors and third one indicates in the overall process was successful.
55+ // returns RegisterAnPollProcess which can be query to know the process.
5756//
5857// The process consists in first registers the webhook to be execute on URL
5958// with the body given with a delay in seconds.
6059// Second it polls the state until it the webhook finishes or until encounter
6160// an irrecoverable error.
62- func (c * client ) RegisterAndPoll (URL , body string , delay int , interval time.Duration ) (<- chan string , <- chan string , <- chan bool ) {
63- out := make (chan string )
64- errc := make (chan string )
65- succ := make (chan bool )
66-
61+ func (c * client ) RegisterAndPoll (URL , body string , sec int , interval time.Duration ) * RegisterAnPollProcess {
62+ proc := NewRegisterAnPollProcess ()
6763 go func () {
68- defer close (out )
69- defer close (errc )
70- defer close (succ )
71- stdout := NewOutput ()
72-
73- out <- stdout .Connecting ()
74- rr , err := c .register (URL , body , delay )
64+ proc .Connect ()
65+ rr , err := c .register (URL , body , sec )
7566 if err != nil {
76- errc <- stdout .Error (err )
77- succ <- false
67+ proc .Error (err )
7868 return
7969 }
8070
@@ -83,21 +73,18 @@ func (c *client) RegisterAndPoll(URL, body string, delay int, interval time.Dura
8373 for range ticker .C {
8474 sr , err := c .state (rr .ID )
8575 if err != nil {
86- errc <- stdout .Error (err )
76+ proc .Error (err )
8777 } else {
88- for _ , v := range stdout .State (sr ) {
89- out <- v
90- }
78+ proc .State (sr )
9179 }
9280
93- if isFinal (sr , err ) {
94- succ <- isSuccess (sr , err )
81+ if proc .IsFinished () {
9582 break
9683 }
9784 }
9885 }()
9986
100- return out , errc , succ
87+ return proc
10188}
10289
10390// isFinal returns if StateResponse or err is a final state
@@ -106,7 +93,7 @@ func isFinal(state *StateResponse, err error) bool {
10693 return state .Status == "failed" || state .Status == "succeeded" || state .Status == "timeout"
10794 }
10895
109- if err == errUnauthorized {
96+ if err == ErrUnauthorized {
11097 return true
11198 }
11299
@@ -180,9 +167,9 @@ func (c *client) execute(req *http.Request, codeWanted int) ([]byte, error) {
180167
181168 switch c := resp .StatusCode ; {
182169 case c == 401 :
183- return nil , errUnauthorized
170+ return nil , ErrUnauthorized
184171 case c == 429 :
185- return nil , errTooManyRequests
172+ return nil , ErrTooManyRequests
186173 case c != codeWanted :
187174 return nil , fmt .Errorf ("wrong response registering webhook: %s, %s\n " , resp .Status , b )
188175 }
0 commit comments