Skip to content

Commit e49dd06

Browse files
authored
feat(refactor): for pr #826 (#848)
1 parent 9097b96 commit e49dd06

File tree

1 file changed

+25
-42
lines changed

1 file changed

+25
-42
lines changed

client.go

+25-42
Original file line numberDiff line numberDiff line change
@@ -884,34 +884,15 @@ func (c *Client) SetRootCertificate(pemFilePath string) *Client {
884884
c.log.Errorf("%v", err)
885885
return c
886886
}
887-
888-
config, err := c.tlsConfig()
889-
if err != nil {
890-
c.log.Errorf("%v", err)
891-
return c
892-
}
893-
if config.RootCAs == nil {
894-
config.RootCAs = x509.NewCertPool()
895-
}
896-
897-
config.RootCAs.AppendCertsFromPEM(rootPemData)
887+
c.handleCAs("root", rootPemData)
898888
return c
899889
}
900890

901891
// SetRootCertificateFromString method helps to add one or more root certificates into Resty client
902892
//
903-
// client.SetRootCertificateFromString("pem file content")
904-
func (c *Client) SetRootCertificateFromString(pemContent string) *Client {
905-
config, err := c.tlsConfig()
906-
if err != nil {
907-
c.log.Errorf("%v", err)
908-
return c
909-
}
910-
if config.RootCAs == nil {
911-
config.RootCAs = x509.NewCertPool()
912-
}
913-
914-
config.RootCAs.AppendCertsFromPEM([]byte(pemContent))
893+
// client.SetRootCertificateFromString("pem certs content")
894+
func (c *Client) SetRootCertificateFromString(pemCerts string) *Client {
895+
c.handleCAs("root", []byte(pemCerts))
915896
return c
916897
}
917898

@@ -924,35 +905,37 @@ func (c *Client) SetClientRootCertificate(pemFilePath string) *Client {
924905
c.log.Errorf("%v", err)
925906
return c
926907
}
927-
928-
config, err := c.tlsConfig()
929-
if err != nil {
930-
c.log.Errorf("%v", err)
931-
return c
932-
}
933-
if config.ClientCAs == nil {
934-
config.ClientCAs = x509.NewCertPool()
935-
}
936-
937-
config.ClientCAs.AppendCertsFromPEM(rootPemData)
908+
c.handleCAs("client", rootPemData)
938909
return c
939910
}
940911

941912
// SetClientRootCertificateFromString method helps to add one or more client's root certificates into Resty client
942913
//
943-
// client.SetClientRootCertificateFromString("pem file content")
944-
func (c *Client) SetClientRootCertificateFromString(pemContent string) *Client {
914+
// client.SetClientRootCertificateFromString("pem certs content")
915+
func (c *Client) SetClientRootCertificateFromString(pemCerts string) *Client {
916+
c.handleCAs("client", []byte(pemCerts))
917+
return c
918+
}
919+
920+
func (c *Client) handleCAs(scope string, permCerts []byte) {
945921
config, err := c.tlsConfig()
946922
if err != nil {
947923
c.log.Errorf("%v", err)
948-
return c
949-
}
950-
if config.ClientCAs == nil {
951-
config.ClientCAs = x509.NewCertPool()
924+
return
952925
}
953926

954-
config.ClientCAs.AppendCertsFromPEM([]byte(pemContent))
955-
return c
927+
switch scope {
928+
case "root":
929+
if config.RootCAs == nil {
930+
config.RootCAs = x509.NewCertPool()
931+
}
932+
config.RootCAs.AppendCertsFromPEM(permCerts)
933+
case "client":
934+
if config.ClientCAs == nil {
935+
config.ClientCAs = x509.NewCertPool()
936+
}
937+
config.ClientCAs.AppendCertsFromPEM(permCerts)
938+
}
956939
}
957940

958941
// SetOutputDirectory method sets output directory for saving HTTP response into file.

0 commit comments

Comments
 (0)