/
interface.go
77 lines (64 loc) · 2.22 KB
/
interface.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
// Copyright 2014, Hǎiliàng Wáng. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package getgo
import (
"io"
"net/http"
"github.com/hailiang/html-query"
)
// Task is an HTTP crawler task.
// It must provide an HTTP request and a method to handle an HTTP response.
type Task interface {
Requester
Handle(resp *http.Response) error
}
// Runner runs Tasks.
// A Runner gets an HTTP request from a Task, get the HTTP response and pass the
// response to the Task's Handle method. When a runner failed to get a response
// object, a nil response must still be passed to the Handle method to notify
// that a transaction must be rolled back if any.
type Runner interface {
Run(task Task) error // Run runs a task
Close() // Close closes the runner
}
// StorableTask is a task that should be able to store data with a Storer passed to the Handle method.
type StorableTask interface {
Requester
Handle(resp *http.Response, s Storer) error
}
// TextTask is a task that only retrieves a Response's body.
type TextTask interface {
Requester
Handle(r io.Reader, s Storer) error
}
// HTMLTask is an HTML task should be able to Parse an HTML node tree to a slice of objects.
type HTMLTask interface {
Requester
Handle(root *query.Node, s Storer) error
}
// Requester is the interface that returns an HTTP request by Request method.
// The Request method must be implemented to allow repeated calls.
type Requester interface {
Request() *http.Request
}
// Storer provides the Store method to store an object parsed from an HTTP response.
type Storer interface {
Store(v interface{}) error
}
// Tx is a transaction interface that provides methods for storing objects,
// commit or rollback changes. Notice that there is no Delete method defined.
// Tx's implementation must allow concurrent use.
type Tx interface {
Storer
Commit() error
Rollback() error
}
// Doer processes an HTTP request and returns an HTTP response.
type Doer interface {
Do(req *http.Request) (resp *http.Response, err error)
}
// ErrorHandler is used to call back an external error handler when a task fails.
type ErrorHandler interface {
HandleError(request *http.Request, err error) error
}