feat: add tap offset of point

This commit is contained in:
xucong.053
2022-10-25 14:52:51 +08:00
committed by xucong.053
parent c467b55c72
commit ef9eaa7e38

View File

@@ -4,12 +4,14 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"time"
@@ -24,6 +26,16 @@ type Driver struct {
lastLaunchedPackageName string
}
// HTTPClient is the default client to use to communicate with the WebDriver server.
var HTTPClient = http.DefaultClient
type RawResponse []byte
var uia2Header = map[string]string{
"Content-Type": "application/json;charset=UTF-8",
"accept": "application/json",
}
func (wd *Driver) concatURL(u *url.URL, elem ...string) string {
var tmp *url.URL
if u == nil {
@@ -52,6 +64,24 @@ func (wd *Driver) httpDELETE(pathElem ...string) (rawResp rawResponse, err error
return wd.httpRequest(http.MethodDelete, wd.concatURL(nil, pathElem...), nil)
}
func (wd *Driver) tempHttpGET(pathElem ...string) (rawResp rawResponse, err error) {
return wd.tempHttpRequest(http.MethodGet, wd.concatURL(nil, pathElem...), nil)
}
func (wd *Driver) tempHttpPOST(data interface{}, pathElem ...string) (rawResp rawResponse, err error) {
var bsJSON []byte = nil
if data != nil {
if bsJSON, err = json.Marshal(data); err != nil {
return nil, err
}
}
return wd.tempHttpRequest(http.MethodPost, wd.concatURL(nil, pathElem...), bsJSON)
}
func (wd *Driver) tempHttpDELETE(pathElem ...string) (rawResp rawResponse, err error) {
return wd.tempHttpRequest(http.MethodDelete, wd.concatURL(nil, pathElem...), nil)
}
func (wd *Driver) httpRequest(method string, rawURL string, rawBody []byte) (rawResp rawResponse, err error) {
log.Debug().Str("method", method).Str("url", rawURL).Str("body", string(rawBody)).Msg("request driver agent")
@@ -94,6 +124,76 @@ func (wd *Driver) httpRequest(method string, rawURL string, rawBody []byte) (raw
return
}
func (wd *Driver) tempHttpRequest(method string, rawURL string, rawBody []byte) (rawResp rawResponse, err error) {
var localPort int
{
tmpURL, _ := url.Parse(rawURL)
hostname := tmpURL.Hostname()
if strings.HasPrefix(hostname, forwardToPrefix) {
localPort, _ = strconv.Atoi(strings.TrimPrefix(hostname, forwardToPrefix))
rawURL = strings.Replace(rawURL, hostname, "localhost", 1)
}
}
var req *http.Request
if req, err = http.NewRequest(method, rawURL, bytes.NewBuffer(rawBody)); err != nil {
return
}
for k, v := range uia2Header {
req.Header.Set(k, v)
}
tmpHTTPClient := HTTPClient
if localPort != 0 {
var conn net.Conn
if conn, err = net.Dial("tcp", fmt.Sprintf(":%d", localPort)); err != nil {
return nil, fmt.Errorf("adb forward: %w", err)
}
tmpHTTPClient.Transport = &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return conn, nil
},
}
defer func() { _ = conn.Close() }()
}
var resp *http.Response
if resp, err = tmpHTTPClient.Do(req); err != nil {
return nil, err
}
defer func() {
_ = resp.Body.Close()
}()
rawResp, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var reply = new(struct {
Value struct {
Err string `json:"error"`
Message string `json:"message"`
Stacktrace string `json:"stacktrace"`
}
})
if err = json.Unmarshal(rawResp, reply); err != nil {
if resp.StatusCode == http.StatusOK {
// 如果遇到 value 直接是 字符串,则报错,但是 http 状态是 200
// {"sessionId":"b4f2745a-be74-4cb3-8f4c-881cde817a8d","value":"YWJjZDEyMw==\n"}
return rawResp, nil
}
return nil, err
}
if reply.Value.Err != "" {
return nil, fmt.Errorf("%s: %s", reply.Value.Err, reply.Value.Message)
}
return
}
func convertToHTTPClient(conn net.Conn) *http.Client {
return &http.Client{
Transport: &http.Transport{