-
Notifications
You must be signed in to change notification settings - Fork 10
/
ocr_http_client.go
161 lines (128 loc) · 3.8 KB
/
ocr_http_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
package ocrclient
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/textproto"
"github.com/couchbaselabs/logg"
)
type HttpClient struct {
ApiEndpointUrl string // the url of the server, eg, http://api.openocr.net
}
func NewHttpClient(apiEndpointUrl string) *HttpClient {
return &HttpClient{
ApiEndpointUrl: apiEndpointUrl,
}
}
// Send a multipart/related POST request to OpenOCR API endpoint to decode image data
func (c HttpClient) DecodeImageReader(imageReader io.Reader, ocrRequest OcrRequest) (string, error) {
// create JSON for POST reqeust
jsonBytes, err := json.Marshal(ocrRequest)
if err != nil {
return "", err
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
mimeHeader := textproto.MIMEHeader{}
mimeHeader.Set("Content-Type", "application/json")
part, err := writer.CreatePart(mimeHeader)
if err != nil {
logg.LogTo("OCRCLIENT", "Unable to create json multipart part")
return "", err
}
_, err = part.Write(jsonBytes)
if err != nil {
logg.LogTo("OCRCLIENT", "Unable to write json multipart part")
return "", err
}
partHeaders := textproto.MIMEHeader{}
// TODO: pass these vals in instead of hardcoding
partHeaders.Set("Content-Type", "image/png")
partHeaders.Set("Content-Disposition", "attachment; filename=\"attachment.txt\".")
partAttach, err := writer.CreatePart(partHeaders)
if err != nil {
logg.LogTo("OCRCLIENT", "Unable to create image multipart part")
return "", err
}
_, err = io.Copy(partAttach, imageReader)
if err != nil {
logg.LogTo("OCRCLIENT", "Unable to write image multipart part")
return "", err
}
err = writer.Close()
if err != nil {
logg.LogTo("OCRCLIENT", "Error closing writer")
return "", err
}
// create a client
client := &http.Client{}
// create POST request
apiUrl := c.OcrApiFileUploadEndpointUrl()
req, err := http.NewRequest("POST", apiUrl, bytes.NewReader(body.Bytes()))
if err != nil {
logg.LogTo("OCRCLIENT", "Error creating POST request")
return "", err
}
// set content type
contentType := fmt.Sprintf("multipart/related; boundary=%q", writer.Boundary())
req.Header.Set("Content-Type", contentType)
// TODO: code below is all duplicated with DecodeImageUrl()
// send POST request
resp, err := client.Do(req)
if err != nil {
logg.LogTo("OCRCLIENT", "Error sending POST request")
return "", err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return "", fmt.Errorf("Got error status response: %d", resp.StatusCode)
}
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
logg.LogTo("OCRCLIENT", "Error reading response")
return "", err
}
return string(respBytes), nil
}
// Send a JSON POST request to OpenOCR API endpoint to decode image url
func (c HttpClient) DecodeImageUrl(ocrRequest OcrRequest) (string, error) {
// create JSON for POST reqeust
jsonBytes, err := json.Marshal(ocrRequest)
if err != nil {
return "", err
}
// create a client
client := &http.Client{}
// create POST request
apiUrl := c.OcrApiEndpointUrl()
req, err := http.NewRequest("POST", apiUrl, bytes.NewReader(jsonBytes))
if err != nil {
return "", err
}
// send POST request
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return "", fmt.Errorf("Got error status response: %d", resp.StatusCode)
}
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(respBytes), nil
}
// Get the url of the OCR API endpoint, eg, http://api.openocr.net/ocr
func (c HttpClient) OcrApiEndpointUrl() string {
return fmt.Sprintf("%v/%v", c.ApiEndpointUrl, "ocr")
}
// Get the url of the OCR API endpoint that supports file upload
func (c HttpClient) OcrApiFileUploadEndpointUrl() string {
return fmt.Sprintf("%v/%v", c.ApiEndpointUrl, "ocr-file-upload")
}