-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding CBOR request endoing. Adding Http Connection as a connection o…
…ption
- Loading branch information
Showing
22 changed files
with
545 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E= | ||
github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ= | ||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= | ||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= | ||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= | ||
|
@@ -18,6 +20,8 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR | |
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= | ||
This comment has been minimized.
Sorry, something went wrong.
ElecTwix
Contributor
|
||
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package connection | ||
|
||
type Connection interface { | ||
Connect(url string) (Connection, error) | ||
Close() error | ||
Send(method string, params []interface{}) (interface{}, error) | ||
} | ||
|
||
type LiveHandler interface { | ||
LiveNotifications(id string) (chan Notification, error) | ||
} | ||
|
||
type Encoder func(value interface{}) ([]byte, error) | ||
|
||
type Decoder func(encoded []byte, value interface{}) error | ||
|
||
type BaseConnection struct { | ||
encode Encoder | ||
decode Decoder | ||
baseURL string | ||
} | ||
|
||
type NewConnectionParams struct { | ||
Encoder Encoder | ||
Decoder Decoder | ||
BaseURL string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package connection | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/surrealdb/surrealdb.go/internal/rand" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type Http struct { | ||
BaseConnection | ||
|
||
httpClient *http.Client | ||
|
||
namespace string | ||
database string | ||
} | ||
|
||
func NewHttp(p NewConnectionParams) Connection { | ||
con := Http{ | ||
BaseConnection: BaseConnection{ | ||
encode: p.Encoder, | ||
decode: p.Decoder, | ||
}, | ||
} | ||
|
||
if con.httpClient == nil { | ||
con.httpClient = &http.Client{ | ||
Timeout: 10 * time.Second, // Set a default timeout to avoid hanging requests | ||
} | ||
} | ||
|
||
return &con | ||
} | ||
|
||
func (h *Http) Connect(url string) (Connection, error) { | ||
// TODO: EXTRACT BASE url and set | ||
h.baseURL = url | ||
|
||
_, err := h.MakeRequest(http.MethodGet, "/health", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return h, nil | ||
} | ||
|
||
func (h *Http) Close() error { | ||
return nil | ||
} | ||
|
||
func (h *Http) SetTimeout(timeout time.Duration) *Http { | ||
h.httpClient.Timeout = timeout | ||
return h | ||
} | ||
|
||
func (h *Http) SetHttpClient(client *http.Client) *Http { | ||
h.httpClient = client | ||
return h | ||
} | ||
|
||
func (h *Http) Send(method string, params []interface{}) (interface{}, error) { | ||
if h.baseURL == "" { | ||
return nil, fmt.Errorf("connection host not set") | ||
} | ||
|
||
rpcReq := &RPCRequest{ | ||
ID: rand.String(RequestIDLength), | ||
Method: method, | ||
Params: params, | ||
} | ||
|
||
reqBody, err := h.encode(rpcReq) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
httpReq, err := http.NewRequest(method, h.baseURL+"rpc", bytes.NewBuffer(reqBody)) | ||
httpReq.Header.Set("Accept", "application/cbor") | ||
httpReq.Header.Set("Content-Type", "application/cbor") | ||
|
||
resp, err := h.MakeRequest(http.MethodPost, "/rpc", reqBody) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var rpcResponse RPCResponse | ||
err = h.decode(resp, &rpcResponse) | ||
|
||
return &rpcResponse, nil | ||
} | ||
|
||
func (h *Http) MakeRequest(method string, url string, body []byte) ([]byte, error) { | ||
req, err := http.NewRequest(method, url, bytes.NewBuffer(body)) | ||
if err != nil { | ||
log.Fatalf("Error creating request: %v", err) | ||
} | ||
|
||
resp, err := h.httpClient.Do(req) | ||
if err != nil { | ||
log.Fatalf("Error making HTTP request: %v", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
return nil, fmt.Errorf("request failed with status code %d", resp.StatusCode) | ||
} | ||
|
||
return ioutil.ReadAll(resp.Body) | ||
} |
Oops, something went wrong.
Why move to
logger
fromslog
. Slog is better for DB like systems.https://go.dev/blog/slog