-
Notifications
You must be signed in to change notification settings - Fork 0
/
newton.go
43 lines (37 loc) · 990 Bytes
/
newton.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
package newton
import (
"encoding/json"
"net/http"
"strings"
)
//Data wraps the response from the newton API.
//The first 2 properties are provided by the user. The last property is provided by the API.
type Data struct {
Operation, Expression, Result string
}
func buildURL(operation, expression string) string {
var url strings.Builder
url.WriteString("https://newton.now.sh/")
url.WriteString(operation)
url.WriteString("/")
url.WriteString(expression)
return url.String()
}
var client = &http.Client{}
func getResult(url string, target interface{}) error {
r, err := client.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
return json.NewDecoder(r.Body).Decode(target)
}
//Calc sends the expression and operation off to Newton then returns the resulting Data struct
func Calc(requestData Data) Data {
result := Data{}
err := getResult(buildURL(requestData.Operation, requestData.Expression), &result)
if err != nil {
panic(err)
}
return result
}