8000 code style refactoring. by streamdp · Pull Request #10 · streamdp/ccd · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

code style refactoring. #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 19, 2025
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ model/mysql_data/
model/postgres_data/

dist/

*.http
6 changes: 4 additions & 2 deletions clients/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package clients

import (
"context"

"github.com/streamdp/ccd/domain"
)

Expand All @@ -11,7 +13,7 @@ type RestClient interface {

// WsClient interface makes it possible to expand the list of wss data providers
type WsClient interface {
Subscribe(from string, to string) error
Unsubscribe(from string, to string) error
Subscribe(ctx context.Context, from string, to string) error
Unsubscribe(ctx context.Context, from string, to string) error
ListSubscriptions() domain.Subscriptions
}
82 changes: 41 additions & 41 deletions clients/cryptocompare/rest_client.go
< 6D47 tr data-hunk="41aac03dc74905e5b277b0dcb472a02728deeec90fc2795e1f5f7ae88b3f52d0" class="show-top-border">
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package cryptocompare
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"

"github.com/streamdp/ccd/clients"
"github.com/streamdp/ccd/config"
"github.com/streamdp/ccd/domain"
)
Expand All @@ -28,56 +27,68 @@ type cryptoCompareRest struct {
client *http.Client
}

var (
errApiKeyNotDefined = errors.New("you should specify \"CCDC_APIKEY\" in you OS environment")
errWrongStatusCode = errors.New("wrong status code")
)

// Init apiKey, apiUrl, wsURL variables with environment values and return CryptoCompareData structure
func Init() (rc clients.RestClient, err error) {
var apiKey string
if apiKey, err = getApiKey(); err != nil {
return
func Init(cfg *config.App) (*cryptoCompareRest, error) {
if cfg.ApiKey == "" {
return nil, errApiKeyNotDefined
}

return &cryptoCompareRest{
apiKey: apiKey,
apiKey: cfg.ApiKey,
client: &http.Client{
Timeout: time.Duration(config.HttpClientTimeout) * time.Millisecond,
Timeout: cfg.Http.ClientTimeout(),
},
}, nil
}

func getApiKey() (apiKey string, err error) {
if apiKey = config.GetEnv("CCDC_APIKEY"); apiKey == "" {
return "", errors.New("you should specify \"CCDC_APIKEY\" in you OS environment")
}
return
}

// Get filled CryptoCompareData structure for the selected pair currencies over http/https
func (cc *cryptoCompareRest) Get(fSym string, tSym string) (ds *domain.Data, err error) {
func (cc *cryptoCompareRest) Get(fSym string, tSym string) (*domain.Data, error) {
var (
u *url.URL
response *http.Response
body []byte
)
if u, err = cc.buildURL(fSym, tSym); err != nil {
return nil, err
u, err := cc.buildURL(fSym, tSym)
if err != nil {
return nil, fmt.Errorf("failed to build url: %w", err)
}
if response, err = cc.client.Get(u.String()); err != nil {
return nil, err
response, err = cc.client.Get(u.String())
if err != nil {
return nil, fmt.Errorf("failed to fetch data: %w", err)
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(response.Body)
if body, err = io.ReadAll(response.Body); err != nil {
return
body, err = io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response: %w", err)
}
if response.StatusCode != 200 {
return
if response.StatusCode != http.StatusOK {
return nil, errWrongStatusCode
}
rawData := &cryptoCompareData{}
if err = json.Unmarshal(body, rawData); err != nil {
return
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}

return convertToDomain(fSym, tSym, rawData), nil
}

func (cc *cryptoCompareRest) buildURL(fSym string, tSym string) (*url.URL, error) {
u, err := url.Parse(apiUrl + multipleSymbolsFullData)
if err != nil {
return nil, fmt.Errorf("failed to build url: %w", err)
}
query := u.Query()
query.Set("fsyms", fSym)
query.Set("tsyms", tSym)
query.Set("api_key", cc.apiKey)
u.RawQuery = query.Encode()

return u, nil
}

func convertToDomain(from, to string, d *cryptoCompareData) *domain.Data {
if d == nil || d.Raw == nil || d.Raw[from] == nil {
return nil
Expand All @@ -95,6 +106,7 @@ func convertToDomain(from, to string, d *cryptoCompareData) *domain.Data {
Supply: r.Supply,
MktCap: r.MktCap,
})

return &domain.Data{
FromSymbol: from,
ToSymbol: to,
Expand All @@ -111,15 +123,3 @@ func convertToDomain(from, to string, d *cryptoCompareData) *domain.Data {
DisplayDataRaw: string(b),
}
}

func (cc *cryptoCompareRest) buildURL(fSym string, tSym string) (u *url.URL, err error) {
if u, err = url.Parse(apiUrl + multipleSymbolsFullData); err != nil {
return nil, err
}
query := u.Query()
query.Set("fsyms", fSym)
query.Set("tsyms", tSym)
query.Set("api_key", cc.apiKey)
u.RawQuery = query.Encode()
return u, nil
}
2 changes: 2 additions & 0 deletions clients/cryptocompare/rest_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func Test_cryptoCompareRest_buildURL(t *testing.T) {
"https://min-api.cryptocompare.com/data/pricemultifull?" +
"api_key=jHQuBvBisp3UFKzqvmWpH4elAqNv%2BJQT&fsyms=BTC&tsyms=USTD",
)

return u
}(),
wantErr: false,
Expand All @@ -149,6 +150,7 @@ func Test_cryptoCompareRest_buildURL(t *testing.T) {
gotU, err := cc.buildURL(tt.args.fSym, tt.args.tSym)
if (err != nil) != tt.wantErr {
t.Errorf("buildURL() error = %v, wantErr %v", err, tt.wantErr)

return
}
if !reflect.DeepEqual(gotU, tt.wantU) {
Expand Down
Loading
Loading
0