separated httpcli from replaycli. Added config methods

nats
Paulo Simão 2021-10-11 11:53:44 -03:00
parent 7d8e31240e
commit 4e142e5300
9 changed files with 77 additions and 207 deletions

View File

@ -5,12 +5,14 @@ import (
)
type Cli struct {
cli *api.Cli
*api.ApiCli
//cli *api.Cli
}
func (c *Cli) HttpCli() *api.Cli {
return c.cli
}
//func (c *Cli) HttpCli() *api.Cli {
//
// return c.HttpCli()
//}
type Req struct {
Site string `json:"site"`
@ -27,7 +29,7 @@ func (c *Cli) Recaptchav2(site string, data string) (string, error) {
Img: nil,
To: 300,
}
err := c.cli.HttpJsonPost("/ipc/anticaptcha/recaptchav2", req, &ret)
err := c.HttpCli().JsonPost("/ipc/anticaptcha/recaptchav2", req, &ret)
return ret, err
}
@ -38,11 +40,11 @@ func (c *Cli) Image2text(site string, data []byte) (string, error) {
Img: data,
To: 300,
}
err := c.cli.HttpJsonPost("/ipc/anticaptcha/image2text", req, &ret)
err := c.HttpCli().JsonPost("/ipc/anticaptcha/image2text", req, &ret)
return ret, err
}
func NewCli() *Cli {
ret := &Cli{cli: api.NewCli()}
ret := &Cli{ApiCli: api.NewApiCli()}
return ret
}

View File

@ -7,44 +7,44 @@ import (
)
type Cli struct {
cli *api.Cli
*api.ApiCli
}
func (c *Cli) HttpCli() *api.Cli {
return c.cli
}
//func (c *Cli) HttpCli() *httpcli.Cli {
// return c.ApiCli.HttpCli()
//}
func (c *Cli) Start(to int) error {
err := c.cli.HttpJsonGet(fmt.Sprintf("/ipc/chrome/start?to=%d", to), nil)
err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/chrome/start?to=%d", to), nil)
return err
}
func (c *Cli) StartHeadless() error {
err := c.cli.HttpJsonGet("/ipc/chrome/startHeadless", nil)
err := c.HttpCli().JsonGet("/ipc/chrome/startHeadless", nil)
return err
}
func (c *Cli) Stop() error {
err := c.cli.HttpJsonGet("/ipc/chrome/stop", nil)
err := c.HttpCli().JsonGet("/ipc/chrome/stop", nil)
return err
}
func (c *Cli) New(url string) (string, error) {
ret := ""
err := c.cli.HttpJsonGet(fmt.Sprintf("/ipc/chrome/new?url=%s", url), &ret)
err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/chrome/new?url=%s", url), &ret)
return ret, err
}
func (c *Cli) Close(id string) error {
err := c.cli.HttpJsonGet("/ipc/chrome/close/"+id, nil)
err := c.HttpCli().JsonGet("/ipc/chrome/close/"+id, nil)
return err
}
func (c *Cli) Eval(id string, s string) (map[string]interface{}, error) {
ret := make(map[string]interface{})
bs, err := c.cli.HttpRawPost("/ipc/chrome/eval/"+id, []byte(s))
bs, err := c.HttpCli().RawPost("/ipc/chrome/eval/"+id, []byte(s))
json.Unmarshal(bs, &ret)
return ret, err
}
func (c *Cli) Wait(id string, s string, to int) (string, error) {
ret := ""
bs, err := c.cli.HttpRawPost(fmt.Sprintf("/ipc/chrome/wait/%s?to=%d", id, to), []byte(s))
bs, err := c.HttpCli().RawPost(fmt.Sprintf("/ipc/chrome/wait/%s?to=%d", id, to), []byte(s))
json.Unmarshal(bs, &ret)
return ret, err
}
@ -55,11 +55,11 @@ func (c *Cli) Send(id string, m string, ps map[string]interface{}) (string, erro
"method": m,
"params": ps,
}
err := c.cli.HttpJsonPost("/ipc/chrome/eval/"+id, in, &ret)
err := c.HttpCli().JsonPost("/ipc/chrome/eval/"+id, in, &ret)
return ret, err
}
func NewCli() *Cli {
ret := &Cli{cli: api.NewCli()}
ret := &Cli{ApiCli: api.NewApiCli()}
return ret
}

View File

@ -1,145 +1,23 @@
package api
import (
"bytes"
"encoding/json"
"io"
"net/http"
"go.digitalcircle.com.br/open/httpcli"
"go.digitalcircle.com.br/open/replaycli-go/util"
"os"
"strings"
)
type Cli struct {
cli *http.Client
headers map[string]string
type ApiCli struct {
cli *httpcli.Cli
}
func (c *Cli) Addr() string {
ret := os.Getenv("REPLAY_ADDR")
if ret == "" {
ret = "https://localhost:8443"
} else if strings.HasPrefix(ret, ":") {
ret = "https://localhost" + ret
}
return ret
}
func (c *Cli) AddHeader(k string, v string) {
c.headers[k] = v
}
func (c *Cli) DelHeader(k string) {
delete(c.headers, k)
}
func (c *Cli) HttpDo(method string, strurl string, body []byte) (*http.Response, error) {
if !strings.HasPrefix(strurl, "http") {
addr := c.Addr()
if strings.HasSuffix(addr, "/") {
addr = addr[:len(addr)-1]
}
if !strings.HasPrefix(strurl, "") {
strurl = "/" + strurl
}
strurl = addr + strurl
}
var req *http.Request
var err error
if body != nil && len(body) > 0 {
req, err = http.NewRequest(method, strurl, io.NopCloser(bytes.NewReader(body)))
} else {
req, err = http.NewRequest(method, strurl, nil)
}
if err != nil {
return nil, err
}
if c.headers != nil {
for k, v := range c.headers {
req.Header.Set(k, v)
}
}
req.Header.Set("X-API-KEY", apikey)
req.Header.Set("Content-Type", "application/json")
return c.cli.Do(req)
}
func (c *Cli) HttpDoJson(method string, strurl string, i interface{}, o interface{}) (err error) {
bs, err := json.Marshal(i)
if err != nil {
return err
}
res, err := c.HttpDo(method, strurl, bs)
if err != nil {
return
}
defer res.Body.Close()
if o != nil {
err = json.NewDecoder(res.Body).Decode(o)
}
return
}
func (c *Cli) HttpJsonGet(strurl string, o interface{}) error {
return c.HttpDoJson(http.MethodGet, strurl, nil, o)
}
func (c *Cli) HttpJsonDelete(strurl string, o interface{}) error {
return c.HttpDoJson(http.MethodDelete, strurl, nil, o)
}
func (c *Cli) HttpJsonHead(strurl string, o interface{}) error {
return c.HttpDoJson(http.MethodHead, strurl, nil, o)
}
func (c *Cli) HttpJsonPost(strurl string, i interface{}, o interface{}) error {
return c.HttpDoJson(http.MethodPost, strurl, i, o)
}
func (c *Cli) HttpJsonPut(strurl string, i interface{}, o interface{}) error {
return c.HttpDoJson(http.MethodPut, strurl, i, o)
}
func (c *Cli) HttpJsonPatch(strurl string, i interface{}, o interface{}) error {
return c.HttpDoJson(http.MethodPatch, strurl, i, o)
}
func (c *Cli) HttpRawGet(strurl string) ([]byte, error) {
res, err := c.HttpDo(http.MethodGet, strurl, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
bs, _ := io.ReadAll(res.Body)
return bs, nil
}
func (c *Cli) HttpRawDelete(strurl string) ([]byte, error) {
res, err := c.HttpDo(http.MethodDelete, strurl, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
bs, _ := io.ReadAll(res.Body)
return bs, nil
}
func (c *Cli) HttpRawPost(strurl string, i []byte) ([]byte, error) {
res, err := c.HttpDo(http.MethodPost, strurl, i)
if err != nil {
return nil, err
}
defer res.Body.Close()
bs, _ := io.ReadAll(res.Body)
return bs, nil
}
func (c *Cli) HttpRawPut(strurl string, i []byte) ([]byte, error) {
res, err := c.HttpDo(http.MethodPut, strurl, i)
if err != nil {
return nil, err
}
defer res.Body.Close()
bs, _ := io.ReadAll(res.Body)
return bs, nil
func (a *ApiCli) HttpCli() *httpcli.Cli {
return a.cli
}
func NewCli() *Cli {
func NewApiCli() *ApiCli {
ret := &ApiCli{cli: httpcli.C()}
ret.cli.SetBasePath(util.Addr())
apikey = os.Getenv("REPLAY_APIKEY")
ret := &Cli{
cli: &http.Client{},
headers: make(map[string]string),
}
ret.AddHeader("X-API-KEY", apikey)
ret.cli.AddHeader("X-API-KEY", apikey)
return ret
}

View File

@ -5,19 +5,16 @@ import (
)
type Cli struct {
cli *api.Cli
*api.ApiCli
}
func (c *Cli) HttpCli() *api.Cli {
return c.cli
}
func (c *Cli) New(i interface{}) (string, error) {
res := ""
err := c.cli.HttpJsonPost("/ipc/datasyncreportmgr/new", i, &res)
err := c.HttpCli().JsonPost("/ipc/datasyncreportmgr/new", i, &res)
return res, err
}
func NewCli() *Cli {
ret := &Cli{cli: api.NewCli()}
ret := &Cli{ApiCli: api.NewApiCli()}
return ret
}

View File

@ -11,11 +11,7 @@ const (
)
type Cli struct {
cli *api.Cli
}
func (c *Cli) HttpCli() *api.Cli {
return c.cli
*api.ApiCli
}
type Req struct {
@ -32,7 +28,7 @@ func (c *Cli) Read(fname string, sheet string) ([][]string, error) {
Sheet: sheet,
}
res := make([][]string, 0)
err := c.cli.HttpJsonPost("/ipc/excel/read", req, &res)
err := c.HttpCli().JsonPost("/ipc/excel/read", req, &res)
return res, err
}
func (c *Cli) Write(fname string, sheet string, cell string, val string, celtype string) error {
@ -44,7 +40,7 @@ func (c *Cli) Write(fname string, sheet string, cell string, val string, celtype
Celtype: celtype,
}
res := ""
err := c.cli.HttpJsonPost("/ipc/excel/write", req, &res)
err := c.HttpCli().JsonPost("/ipc/excel/write", req, &res)
return err
}
@ -54,11 +50,11 @@ func (c *Cli) New(fname string, sheet string) (string, error) {
Sheet: sheet,
}
res := ""
err := c.cli.HttpJsonPost("/ipc/excel/new", req, &res)
err := c.HttpCli().JsonPost("/ipc/excel/new", req, &res)
return res, err
}
func NewCli() *Cli {
ret := &Cli{cli: api.NewCli()}
ret := &Cli{ApiCli: api.NewApiCli()}
return ret
}

View File

@ -10,11 +10,7 @@ import (
)
type Cli struct {
cli *api.Cli
}
func (c *Cli) HttpCli() *api.Cli {
return c.cli
*api.ApiCli
}
func (c *Cli) ReplayEnvQueueId() string {
@ -55,56 +51,56 @@ func (c *Cli) ReplayEnvApiKey() string {
}
func (c *Cli) OpenApp(id uint) error {
err := c.cli.HttpJsonGet(fmt.Sprintf("/api/v1/app/run/%d", id), nil)
err := c.HttpCli().JsonGet(fmt.Sprintf("/api/v1/app/run/%d", id), nil)
return err
}
func (c *Cli) ConfigGet(k string) (ret string, err error) {
err = c.cli.HttpJsonGet("/api/v1/config/byrepo/"+util.Repo()+"/"+k, &ret)
err = c.HttpCli().JsonGet("/api/v1/config/byrepo/"+util.Repo()+"/"+k, &ret)
return
}
func (c *Cli) ConfigGetAll() (ret []types.Config, err error) {
ret = make([]types.Config, 0)
err = c.cli.HttpJsonGet("/api/v1/config/byrepo/"+util.Repo(), &ret)
err = c.HttpCli().JsonGet("/api/v1/config/byrepo/"+util.Repo(), &ret)
return
}
func (c *Cli) CronReload() error {
return c.cli.HttpJsonGet("/ipc/cron/reload", nil)
return c.HttpCli().JsonGet("/ipc/cron/reload", nil)
}
func (c *Cli) Exit() error {
err := c.cli.HttpJsonGet("/api/v1/exit", nil)
err := c.HttpCli().JsonGet("/api/v1/exit", nil)
return err
}
func (c *Cli) MenuGetAllEnabled() (ret []types.Menu, err error) {
ret = make([]types.Menu, 0)
err = c.cli.HttpJsonGet("/api/v1/menu/enabled", nil)
err = c.HttpCli().JsonGet("/api/v1/menu/enabled", nil)
return ret, err
}
func (c *Cli) QueueAdd(job string, bs []byte) error {
_, err := c.cli.HttpRawPost("/api/v1/queue/add/"+job, bs)
_, err := c.HttpCli().RawPost("/api/v1/queue/add/"+job, bs)
return err
}
func (c *Cli) QueueGetData(id string) (ret []byte, err error) {
ret, err = c.cli.HttpRawGet("/api/v1/queue/getrequest/" + id)
ret, err = c.HttpCli().RawGet("/api/v1/queue/getrequest/" + id)
return
}
func (c *Cli) QueueGetMyData() ([]byte, error) {
return c.QueueGetData(os.Getenv("REPLAY_QUEUEID"))
}
func (c *Cli) QueueAbort() error {
return c.cli.HttpJsonGet("/api/v1/queue/abort", nil)
return c.HttpCli().JsonGet("/api/v1/queue/abort", nil)
}
func (c *Cli) QueueEnqueue(id uint) error {
return c.cli.HttpJsonGet(fmt.Sprintf("/api/v1/robots/op/enqueue/%d", id), nil)
return c.HttpCli().JsonGet(fmt.Sprintf("/api/v1/robots/op/enqueue/%d", id), nil)
}
func (c *Cli) ServiceStopAll() error {
return c.cli.HttpJsonGet("/api/v1/service/op/stopall", nil)
return c.HttpCli().JsonGet("/api/v1/service/op/stopall", nil)
}
type SQLReturn struct {
@ -117,11 +113,11 @@ func (c *Cli) SQL(s string) (*SQLReturn, error) {
Sql string `json:"sql"`
}{s}
out := &SQLReturn{}
err := c.cli.HttpJsonPost("/api/v1/sql", in, &out)
err := c.HttpCli().JsonPost("/api/v1/sql", in, &out)
return out, err
}
func NewCli() *Cli {
ret := &Cli{cli: api.NewCli()}
ret := &Cli{ApiCli: api.NewApiCli()}
return ret
}

View File

@ -7,55 +7,55 @@ import (
)
type Cli struct {
cli *api.Cli
*api.ApiCli
}
func (c *Cli) HttpCli() *api.Cli {
return c.cli
}
//func (c *Cli) HttpCli() *api.Cli {
// return c.cli
//}
func (c *Cli) ClipRead() (string, error) {
ret := ""
err := c.cli.HttpJsonGet(fmt.Sprintf("/ipc/wingui/clip/read"), &ret)
err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/clip/read"), &ret)
return ret, err
}
func (c *Cli) ClipWrite(site string) error {
err := c.cli.HttpJsonGet(fmt.Sprintf("/ipc/wingui/clip/write?str=%s", site), nil)
err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/clip/write?str=%s", site), nil)
return err
}
func (c *Cli) MouseClick() error {
err := c.cli.HttpJsonGet(fmt.Sprintf("/ipc/wingui/mouse/click"), nil)
err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/mouse/click"), nil)
return err
}
func (c *Cli) MouseMove(x, y int) error {
err := c.cli.HttpJsonGet(fmt.Sprintf("/ipc/wingui/mouse/move?x=%d&y=%d", x, y), nil)
err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/mouse/move?x=%d&y=%d", x, y), nil)
return err
}
func (c *Cli) MouseMoveRelative(x, y int) error {
err := c.cli.HttpJsonGet(fmt.Sprintf("/ipc/wingui/mouse/moverelative?x=%d&y=%d", x, y), nil)
err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/mouse/moverelative?x=%d&y=%d", x, y), nil)
return err
}
func (c *Cli) MouseClickRelative(x, y int) error {
err := c.cli.HttpJsonGet(fmt.Sprintf("/ipc/wingui/mouse/clickatrelative?x=%d&y=%d", x, y), nil)
err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/mouse/clickatrelative?x=%d&y=%d", x, y), nil)
return err
}
func (c *Cli) MouseClickAt(x, y int) error {
err := c.cli.HttpJsonGet(fmt.Sprintf("/ipc/wingui/mouse/clickat?x=%d&y=%d", x, y), nil)
err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/mouse/clickat?x=%d&y=%d", x, y), nil)
return err
}
func (c *Cli) ScreenClick(f string) error {
err := c.cli.HttpJsonGet(fmt.Sprintf("/ipc/wingui/screen/click?f=%s", f), nil)
err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/screen/click?f=%s", f), nil)
return err
}
func (c *Cli) ScreenClickCenter(f string) error {
err := c.cli.HttpJsonGet(fmt.Sprintf("/ipc/wingui/screen/clickcenter?f=%s", f), nil)
err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/screen/clickcenter?f=%s", f), nil)
return err
}
func (c *Cli) ScreenFind(f string) (ret map[string]interface{}, err error) {
ret = make(map[string]interface{})
err = c.cli.HttpJsonGet(fmt.Sprintf("/ipc/wingui/screen/find?f=%s", f), &ret)
err = c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/screen/find?f=%s", f), &ret)
return
}
@ -154,7 +154,7 @@ func (c *Cli) KBTap(t string) (map[string]interface{}, error) {
*/
ret := make(map[string]interface{})
err := c.cli.HttpJsonGet(fmt.Sprintf("/ipc/wingui/kb/tap?str=%s", t), &ret)
err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/kb/tap?str=%s", t), &ret)
return ret, err
}
@ -164,27 +164,27 @@ func (c *Cli) KBType(t string) (map[string]interface{}, error) {
por exemplo. Ao chegar no robo passrão pelo decoding análogo. */
ret := make(map[string]interface{})
err := c.cli.HttpJsonGet(fmt.Sprintf("/ipc/wingui/kb/type?str=%s", t), &ret)
err := c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/kb/type?str=%s", t), &ret)
return ret, err
}
func (c *Cli) ScreenWait(f string, m int) (ret map[string]interface{}, err error) {
ret = make(map[string]interface{})
err = c.cli.HttpJsonGet(fmt.Sprintf("/ipc/wingui/screen/wait?f=%s&m=%d", f, m), &ret)
err = c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/screen/wait?f=%s&m=%d", f, m), &ret)
return
}
func (c *Cli) ScreenWaitClick(f string, m int) (ret map[string]interface{}, err error) {
ret = make(map[string]interface{})
err = c.cli.HttpJsonGet(fmt.Sprintf("/ipc/wingui/screen/waitclick?f=%s&m=%d", f, m), &ret)
err = c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/screen/waitclick?f=%s&m=%d", f, m), &ret)
return
}
func (c *Cli) ScreenWaitClickCenter(f string, m int) (ret map[string]interface{}, err error) {
ret = make(map[string]interface{})
err = c.cli.HttpJsonGet(fmt.Sprintf("/ipc/wingui/screen/waitclickcenter?f=%s&m=%d", f, m), &ret)
err = c.HttpCli().JsonGet(fmt.Sprintf("/ipc/wingui/screen/waitclickcenter?f=%s&m=%d", f, m), &ret)
return
}
func NewCli() *Cli {
ret := &Cli{cli: api.NewCli()}
ret := &Cli{ApiCli: api.NewApiCli()}
return ret
}

1
go.mod
View File

@ -6,5 +6,6 @@ require (
github.com/Microsoft/go-winio v0.5.0
github.com/gorilla/websocket v1.4.2
github.com/mitchellh/go-ps v1.0.0
go.digitalcircle.com.br/open/httpcli v0.0.0-20211010235458-74d201454158
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
)

4
go.sum
View File

@ -9,9 +9,9 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
go.digitalcircle.com.br/open/httpcli v0.0.0-20211010235458-74d201454158 h1:vFm0G1b4hBDJ7y/s2NqRuX99lwuI8/sxxH3XLUOdi0o=
go.digitalcircle.com.br/open/httpcli v0.0.0-20211010235458-74d201454158/go.mod h1:e/6vw7F4vgfs+ThQoLeBbYCYLTH1b5thzFwkFixi+oA=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210921065528-437939a70204 h1:JJhkWtBuTQKyz2bd5WG9H8iUsJRU3En/KRfN8B2RnDs=
golang.org/x/sys v0.0.0-20210921065528-437939a70204/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=