-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
156 lines (132 loc) · 3.66 KB
/
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
package pengine
import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/ichiban/prolog"
)
// Client is a Pengines endpoint.
type Client struct {
// URL of the pengines server, required.
URL string
// Application name, optional. (example: "pengines_sandbox")
Application string
// Chunk is the number of query results to accumulate in one response. 1 by default.
Chunk int
// SourceText is Prolog source code to load (optional).
SourceText string
// SourceURL specifies a URL of Prolog source for the pengine to load (optional).
SourceURL string
// HTTP is the HTTP client used to make API requests.
// If nil, http.DefaultClient is used.
HTTP *http.Client
// Interpreter is the Prolog interpreter used for decoding Prolog-format responses.
// This is useful for handling custom operators.
// If nil, a default interpreter will be used.
Interpreter *prolog.Interpreter
// If true, prints debug logs.
Debug bool
}
// Create creates a new pengine. Call Engine's Ask method to query it.
// If destroy is true, the pengine will be automatically destroyed when a query completes.
// If destroy is false, it is the caller's responsibility to destroy the pengine with Engine.Close.
func (c Client) Create(ctx context.Context, destroy bool) (*Engine, error) {
eng, answer, err := c.create(ctx, "", destroy)
if err != nil {
return nil, err
}
err = eng.handle(answer)
return eng, err
}
// Ask creates a new engine with the given initial query and executes it, returning the answers iterator.
func (c Client) Ask(ctx context.Context, query string) (Answers[Solution], error) {
eng, answer, err := c.create(ctx, query, true)
if err != nil {
return nil, err
}
return newIterator[Solution](eng, answer)
}
func (c Client) create(ctx context.Context, query string, destroy bool) (*Engine, answer, error) {
if c.URL == "" {
return nil, answer{}, fmt.Errorf("pengine: Server URL not set")
}
eng := &Engine{
client: c,
destroy: destroy,
debug: c.Debug,
}
opts := c.options("json")
if query != "" {
opts.Ask = query
}
opts.Destroy = destroy
evt, err := eng.post(ctx, "create", opts)
if err != nil {
return nil, evt, fmt.Errorf("pengine create error: %w", err)
}
return eng, evt, nil
}
func (c Client) client() *http.Client {
if c.HTTP != nil {
return c.HTTP
}
return http.DefaultClient
}
type options struct {
Format string `json:"format"`
Destroy bool `json:"destroy"`
Application string `json:"application,omitempty"`
Chunk int `json:"chunk,omitempty"`
Ask string `json:"ask,omitempty"`
Template string `json:"template,omitempty"`
SourceText string `json:"src_text,omitempty"`
SourceURL string `json:"src_url,omitempty"`
}
func (c Client) options(format string) options {
return options{
Format: format,
Application: c.Application,
Chunk: c.Chunk,
SourceText: c.SourceText,
SourceURL: c.SourceURL,
}
}
func (opts options) String() string {
var sb strings.Builder
first := true
write := func(strs ...string) {
if !first {
sb.WriteRune(',')
}
for _, str := range strs {
sb.WriteString(str)
}
first = false
}
sb.WriteRune('[')
if !opts.Destroy {
write("destroy(false)")
}
if opts.Application != "" {
write("application(", escapeAtom(opts.Application), ")")
}
if opts.Chunk > 0 {
write("chunk(", strconv.Itoa(opts.Chunk), ")")
}
if opts.Ask != "" {
write("ask(", opts.Ask, ")")
}
if opts.Template != "" {
write("template(", opts.Template, ")")
}
if opts.SourceText != "" {
write("src_text(", escapeAtom(opts.SourceText), ")")
}
if opts.SourceURL != "" {
write("src_url(", escapeAtom(opts.SourceURL), ")")
}
sb.WriteRune(']')
return sb.String()
}