-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
134 lines (110 loc) · 2.91 KB
/
Copy pathapp.go
File metadata and controls
134 lines (110 loc) · 2.91 KB
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
package main
import (
"bytes"
"context"
"io"
"log"
"net/http"
"net/http/httputil"
"strings"
)
// App struct
type App struct {
ctx context.Context
}
type Req struct {
Method string `json:"method"`
Url string `json:"url"`
Body string `json:"body"`
Headers []Header `json:"headers"`
ContentType string `json:"contentType"`
}
type Resp struct {
Body string `json:"body"`
Cookies []*http.Cookie `json:"cookies"`
Log []string `json:"log"`
Status string `json:"status"`
ContentType string `json:"contentType"`
Headers []Header `json:"headers"`
}
type Header struct {
Key string `json:"key"`
Value string `json:"value"`
}
var FileName = "kanapi-pref.json"
var pref *Pref
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
// read Preferences from pref.go
}
func (a *App) GetPref() (*Pref, error) {
var err error
pref, err = readPreference(FileName)
if err != nil {
log.Fatal(err)
return nil, err
}
return pref, nil
}
func (a *App) SetPref(pref Pref) (*Pref, error) {
return nil, updatePreference(FileName, pref)
}
// CallApi is the main function that is called by the runtime
func (a *App) CallApi(req *Req) (*Resp, error) {
var res Resp
log.Println(req.Method + " call initiated for: " + req.Url)
// replace with https://github.com/monaco-io/request or equivalent
doreq, err := http.NewRequest(req.Method, req.Url, bytes.NewReader([]byte(req.Body)))
if err != nil {
log.Fatal("Failed to create request. ", err, '\n')
return nil, err
}
doreq.Header.Set("User-Agent", "kanapi")
doreq.Header.Set("Accept", "*/*")
doreq.Header.Set("Connection", "keep-alive")
doreq.Header.Set("Content-Type", req.ContentType)
for _, header := range req.Headers {
if header.Key != "" {
doreq.Header.Set(header.Key, header.Value)
}
}
dores, err := http.DefaultClient.Do(doreq)
if err != nil {
log.Fatal("Failed to make http request: ", err, '\n')
return nil, err
}
defer dores.Body.Close()
log.Println(req.Method, " call completed for: ", req.Url)
log.Println("> Status: ", dores.Status)
res.Status = dores.Status
res.ContentType = dores.Header.Get("Content-Type")
res.Headers = parseHeaders(&dores.Header)
buf, err := io.ReadAll(dores.Body)
if err != nil {
log.Fatal("Failed to process response body. ", err, '\n')
return nil, err
}
res.Body = string(buf)
dump, err := httputil.DumpRequestOut(doreq, true)
if err != nil {
log.Fatal(err)
}
res.Log = append(res.Log, string(dump))
return &res, nil
}
func parseHeaders(headers *http.Header) []Header {
var headarr []Header
for k, v := range *headers {
h := Header{}
h.Key = k
h.Value = strings.Join(v, "")
headarr = append(headarr, h)
}
return headarr
}