-
Notifications
You must be signed in to change notification settings - Fork 4
/
http.go
127 lines (114 loc) · 3.85 KB
/
http.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
package kroki
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strings"
)
// PostRequestContext executes a POST request on Kroki using a context
// The payload is a string representing the diagram in text format
func (c *Client) PostRequestContext(ctx context.Context, payload string, diagramType DiagramType, imageFormat ImageFormat) (string, error) {
// construct the url
u, err := url.Parse(c.Config.URL)
if err != nil {
return "", (fmt.Errorf("fail to create the URL from %s: %w", c.Config.URL, err))
}
u.Path = path.Join(u.Path, string(diagramType), string(imageFormat))
// construct the request
req, err := http.NewRequest("POST", u.String(), strings.NewReader(payload))
if err != nil {
return "", fmt.Errorf("fail to create the request: %w", err)
}
timeoutCtx, cancel := context.WithTimeout(ctx, c.Config.Timeout)
defer cancel()
req.Header = http.Header{
"Content-Type": {"text/plain"},
"User-Agent": {fmt.Sprintf("kroki-go %s", Version)},
}
req = req.WithContext(timeoutCtx)
// execute the request
client := http.DefaultClient
response, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("fail to generate the image: %w", err)
}
// read the result
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
body, err := io.ReadAll(response.Body)
var message string
if err != nil {
message = ""
} else {
message = string(body)
}
return "", fmt.Errorf(
"fail to generate the image {status: %d, body: %s}",
response.StatusCode, message)
}
body, err := io.ReadAll(response.Body)
if err != nil {
return "", fmt.Errorf("fail to read the response body: %w", err)
}
return string(body), nil
}
// GetRequest executes a POST request on Kroki
// The payload is a string representing the diagram in text format
func (c *Client) PostRequest(payload string, diagramType DiagramType, imageFormat ImageFormat) (string, error) {
return c.PostRequestContext(context.Background(), payload, diagramType, imageFormat)
}
// GetRequestContext executes a GET request on Kroki using a context
// The payload is a string representing the diagram in deflate + base64 format
func (c *Client) GetRequestContext(ctx context.Context, payload string, diagramType DiagramType, imageFormat ImageFormat) (string, error) {
// construct the url
u, err := url.Parse(c.Config.URL)
if err != nil {
return "", fmt.Errorf("fail to create the URL from %s: %w", c.Config.URL, err)
}
u.Path = path.Join(u.Path, string(diagramType), string(imageFormat), payload)
// construct the request
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return "", fmt.Errorf("fail to create the request: %w", err)
}
timeoutCtx, cancel := context.WithTimeout(ctx, c.Config.Timeout)
defer cancel()
req.Header = http.Header{
"Accept": {"text/plain"},
"User-Agent": {fmt.Sprintf("kroki-go %s", Version)},
}
req = req.WithContext(timeoutCtx)
// execute the request
client := http.DefaultClient
response, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("fail to generate the image: %w", err)
}
// read the result
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
body, err := io.ReadAll(response.Body)
var message string
if err != nil {
message = ""
} else {
message = string(body)
}
return "", fmt.Errorf(
"fail to generate the image {status: %d, body: %s}",
response.StatusCode, message)
}
body, err := io.ReadAll(response.Body)
if err != nil {
return "", fmt.Errorf("fail to read the response body: %w", err)
}
return string(body), nil
}
// GetRequest executes a GET request on Kroki
// The payload is a string representing the diagram in deflate + base64 format
func (c *Client) GetRequest(payload string, diagramType DiagramType, imageFormat ImageFormat) (string, error) {
return c.GetRequestContext(context.Background(), payload, diagramType, imageFormat)
}