This repository has been archived by the owner on Aug 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash2db_connector.go
71 lines (58 loc) · 1.64 KB
/
flash2db_connector.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
package gode
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
//By convention, one-method interfaces are named by the method name plus an -er suffix ...
//https://golang.org/doc/effective_go.html#interface-names
type Connector interface {
Connect(function string, parameters ...interface{}) (json.RawMessage, error)
}
type Flash2dbConnector struct {
url string
}
func NewFlash2dbConnector(host string) *Flash2dbConnector {
const prefixPath = `/amfphp/json.php`
return &Flash2dbConnector{
url: host + prefixPath,
}
}
func (f *Flash2dbConnector) Connect(function string, parameters ...interface{}) (json.RawMessage, error) {
msg, err := f.get(f.generateURL(function, parameters...))
if err != nil {
return nil, err
}
return msg, nil
}
func (f *Flash2dbConnector) get(url string) (json.RawMessage, error) {
response, err := http.Get(url)
if err != nil {
log.Fatal("http Get Error", err)
return nil, err
}
//todo: should we do anything?
//noinspection GoUnhandledErrorResult
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("flash2db response code not OK, got %d", response.StatusCode)
}
bytes, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Print("ioutil ReadAll error : ", err)
return nil, err
}
fmt.Printf("---\nrequest:%s\nresponse:%v\n", url, string(bytes))
return bytes, nil
}
func (f *Flash2dbConnector) generateURL(phpFunctionName string, param ...interface{}) string {
b := strings.Builder{}
b.WriteString(fmt.Sprintf("%s/%s", f.url, phpFunctionName))
for _, p := range param {
b.WriteString(fmt.Sprintf("/%v", p))
}
return b.String()
}