github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/obs/client_base.go (about)

     1  // Copyright 2019 Huawei Technologies Co.,Ltd.
     2  // Licensed under the Apache License, Version 2.0 (the "License"); you may not use
     3  // this file except in compliance with the License.  You may obtain a copy of the
     4  // License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software distributed
     9  // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    10  // CONDITIONS OF ANY KIND, either express or implied.  See the License for the
    11  // specific language governing permissions and limitations under the License.
    12  
    13  package obs
    14  
    15  import (
    16  	"fmt"
    17  	"net/http"
    18  	"strings"
    19  )
    20  
    21  // ObsClient defines OBS client.
    22  type ObsClient struct {
    23  	conf       *config
    24  	httpClient *http.Client
    25  }
    26  
    27  // New creates a new ObsClient instance.
    28  func New(ak, sk, endpoint string, configurers ...configurer) (*ObsClient, error) {
    29  	conf := &config{endpoint: endpoint}
    30  	conf.securityProviders = make([]securityProvider, 0, 3)
    31  	conf.securityProviders = append(conf.securityProviders, NewBasicSecurityProvider(ak, sk, ""))
    32  
    33  	conf.maxRetryCount = -1
    34  	conf.maxRedirectCount = -1
    35  	for _, configurer := range configurers {
    36  		configurer(conf)
    37  	}
    38  
    39  	if err := conf.initConfigWithDefault(); err != nil {
    40  		return nil, err
    41  	}
    42  	err := conf.getTransport()
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	if isWarnLogEnabled() {
    48  		info := make([]string, 3)
    49  		info[0] = fmt.Sprintf("[OBS SDK Version=%s", OBS_SDK_VERSION)
    50  		info[1] = fmt.Sprintf("Endpoint=%s", conf.endpoint)
    51  		accessMode := "Virtual Hosting"
    52  		if conf.pathStyle {
    53  			accessMode = "Path"
    54  		}
    55  		info[2] = fmt.Sprintf("Access Mode=%s]", accessMode)
    56  		doLog(LEVEL_WARN, strings.Join(info, "];["))
    57  	}
    58  
    59  	if conf.httpClient != nil {
    60  		doLog(LEVEL_DEBUG, "Create obsclient with config:\n%s\n", conf)
    61  		obsClient := &ObsClient{conf: conf, httpClient: conf.httpClient}
    62  		return obsClient, nil
    63  	}
    64  
    65  	doLog(LEVEL_DEBUG, "Create obsclient with config:\n%s\n", conf)
    66  	obsClient := &ObsClient{conf: conf, httpClient: &http.Client{Transport: conf.transport, CheckRedirect: checkRedirectFunc}}
    67  	return obsClient, nil
    68  }