-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
d32f88e
commit 494d982
Showing
28 changed files
with
753 additions
and
664 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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 | ||
} |
Oops, something went wrong.