AFAIRE
sdk
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.4 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### sdk ####### Copyright (c) 2021 losyme ###################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package client
import (
"crypto/tls"
"forge.chapril.org/losyme/errors"
)
type Client struct {
endpoints []*endpoint
}
func buildTLSCertificate(cfg *Config) (*tls.Certificate, error) {
if cfg.Cert == "" || cfg.Key == "" {
return nil, nil
}
cert, err := tls.LoadX509KeyPair(cfg.Cert, cfg.Key)
if err != nil {
return nil, err
}
return &cert, nil
}
func New(cfg *Config) (*Client, error) {
if err := cfg.validate(); err != nil {
return nil, errors.WithMessage(err, "sdk.client") //////////////////////////////////////////////////////////////
}
endpoints := make([]*endpoint, 0, len(cfg.Endpoints))
tlsCert, err := buildTLSCertificate(cfg)
if err != nil {
return nil, err
}
for _, ce := range cfg.Endpoints {
ep, err := newEndpoint(cfg, ce, tlsCert)
if err != nil {
return nil, err
}
endpoints = append(endpoints, ep)
}
c := &Client{
endpoints: endpoints,
}
return c, nil
}
/*
######################################################################################################## @(°_°)@ #######
*/