Skip to content

Commit

Permalink
feat: refactor architecture (#5)
Browse files Browse the repository at this point in the history
* feat: remove old implementation

Signed-off-by: Luca Georges Francois <[email protected]>

* feat: add relay service implementation

Signed-off-by: Luca Georges Francois <[email protected]>

* feat: add relay data sdk implementation

Signed-off-by: Luca Georges Francois <[email protected]>

---------

Signed-off-by: Luca Georges Francois <[email protected]>
  • Loading branch information
0xpanoramix committed Dec 3, 2023
1 parent d32f88e commit 494d982
Show file tree
Hide file tree
Showing 28 changed files with 753 additions and 664 deletions.
49 changes: 0 additions & 49 deletions dto/builder_blocks_received.go

This file was deleted.

64 changes: 0 additions & 64 deletions dto/builder_blocks_received_test.go

This file was deleted.

75 changes: 0 additions & 75 deletions dto/proposer_payload_delivered.go

This file was deleted.

71 changes: 0 additions & 71 deletions dto/proposer_payload_delivered_test.go

This file was deleted.

99 changes: 99 additions & 0 deletions relay/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package relay

import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
)

// Client is the Service client which performs API requests.
//
// This client should be passed in the `NewApi` functions whenever an API instance is created.
type Client struct {
apiURL string
requester Requester
}

// NewClient instantiate a new Client object.
//
// Zero or more ClientOption object can be passed as a parameter.
// These options will then be applied to the client.
func NewClient(opts ...ClientOption) (*Client, error) {
conf := newClientConfiguration()

// Apply the options to the configuration.
conf.apply(append(defaultClientOptions(), opts...))

// Validate the applied options.
if err := conf.validate(); err != nil {
return nil, NewClientConfigurationValidationError(err)
}

return &Client{
apiURL: conf.apiURL,
requester: conf.requester,
}, nil
}

// Do performs HTTP request based on the Request object.
func (c *Client) Do(ctx context.Context, req *Request, res any) error {
return c.do(ctx, req, res)
}

//nolint:cyclop
func (c *Client) do(ctx context.Context, req *Request, res any) (err error) {
if req == nil {
return ErrEmptyClientRequest
}

// Acquiring target URL.
reqURL, err := req.getURL(c.apiURL)
if err != nil {
return NewClientRequestInvalidTarget(err)
}

// Instantiating HTTP request.
httpRequest, err := http.NewRequest(req.Method, reqURL.String(), req.Body)
if err != nil {
return NewClientRequestCreationError(err)
}

httpRequest = httpRequest.WithContext(ctx)

// Performing HTTP request.
httpResponse, err := c.requester.Do(httpRequest)
if err != nil {
return NewClientRequestExecutionError(err)
}

// Reading response data.
data, err := io.ReadAll(httpResponse.Body)
if err != nil {
return NewClientResponseReadError(err)
}

// Closing response data.
defer func() {
closeErr := httpResponse.Body.Close()

if closeErr != nil {
err = errors.Join(err, NewClientResponseCloseError(closeErr))
}
}()

// Handling response data errors.
if httpResponse.StatusCode < 200 || httpResponse.StatusCode >= 300 {
return NewClientResponseError(httpResponse.Status, data)
}

// Deserializing response data.
if res != nil {
if err := json.Unmarshal(data, res); err != nil {
return NewClientResponseDeserializationError(err)
}
}

return nil
}
Loading

0 comments on commit 494d982

Please sign in to comment.