-
Notifications
You must be signed in to change notification settings - Fork 1
/
tika.go
72 lines (58 loc) · 1.36 KB
/
tika.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
package tika
import (
"errors"
"net/http"
)
// TikaResource represents the structure of our resource
type TikaResource struct {
client *Client
endpoint string
}
// Tika is the entry point for interacting with the Tika resource
func (c *Client) Tika() *TikaResource {
return &TikaResource{client: c,
endpoint: c.Url + "/tika",
}
}
// Hello returns a greeting
func (c *Client) Hello() (string, error) {
req, err := c.NewRequest(http.MethodGet, c.Tika().endpoint, nil)
if err != nil {
return "", err
}
return c.text(req)
}
// Html returns extracted text as HTML
func (tr *TikaResource) Html() (string, error) {
req, err := tr.newRequest()
if err != nil {
return "", err
}
return tr.client.html(req)
}
// Raw returns extracted text as bytes
func (tr *TikaResource) Raw() ([]byte, error) {
req, err := tr.newRequest()
if err != nil {
return nil, err
}
return tr.client.raw(req)
}
// Text returns extracted text as plain text
func (tr *TikaResource) Text() (string, error) {
req, err := tr.newRequest()
if err != nil {
return "", err
}
return tr.client.text(req)
}
func (tr *TikaResource) newRequest() (*http.Request, error) {
if tr.client.Document == nil {
return nil, errors.New("need a document")
}
req, err := tr.client.NewRequest(http.MethodPut, tr.endpoint, tr.client.Document)
if err != nil {
return nil, err
}
return req, nil
}