github.com/grokify/go-ringcentral-client@v0.3.31/office/v1/util/clientutil.go (about)

     1  package clientutil
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/grokify/goauth"
    11  	"github.com/grokify/mogo/net/urlutil"
    12  	"github.com/joho/godotenv"
    13  
    14  	rc "github.com/grokify/go-ringcentral-client/office/v1/client"
    15  	rs "github.com/grokify/go-scim-client"
    16  	ro "github.com/grokify/goauth/ringcentral"
    17  )
    18  
    19  func LoadEnv() error {
    20  	envPaths := []string{}
    21  	if len(os.Getenv("ENV_PATH")) > 0 {
    22  		envPaths = append(envPaths, os.Getenv("ENV_PATH"))
    23  	}
    24  	return godotenv.Load(envPaths...)
    25  }
    26  
    27  func NewApiClientHttpClientBaseURL(httpClient *http.Client, serverUrl string) (*rc.APIClient, error) {
    28  	if len(strings.TrimSpace(serverUrl)) == 0 {
    29  		return nil, fmt.Errorf("No RingCentral API Server URL provided")
    30  	}
    31  	apiConfig := rc.NewConfiguration()
    32  	apiConfig.BasePath = strings.TrimSpace(serverUrl)
    33  	apiConfig.HTTPClient = httpClient
    34  	apiClient := rc.NewAPIClient(apiConfig)
    35  	return apiClient, nil
    36  }
    37  
    38  func NewApiClientCredentials(creds goauth.Credentials) (*rc.APIClient, error) {
    39  	httpClient, err := creds.NewClient(context.Background())
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	return NewApiClientHttpClientBaseURL(httpClient, creds.OAuth2.ServerURL)
    44  }
    45  
    46  func NewApiClientPassword(oc goauth.CredentialsOAuth2) (*rc.APIClient, error) {
    47  	httpClient, err := ro.NewClientPassword(oc)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return NewApiClientHttpClientBaseURL(httpClient, oc.ServerURL)
    52  }
    53  
    54  func NewApiClientPasswordSimple(oc goauth.CredentialsOAuth2) (*rc.APIClient, error) {
    55  	httpClient, err := ro.NewClientPasswordSimple(oc)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	return NewApiClientHttpClientBaseURL(httpClient, oc.ServerURL)
    60  }
    61  
    62  func NewApiClientPasswordEnv(envPrefix string) (*rc.APIClient, error) {
    63  	return NewApiClientPassword(goauth.NewCredentialsOAuth2Env(envPrefix))
    64  }
    65  
    66  func NewScimApiClient(oc goauth.CredentialsOAuth2) (*rs.APIClient, error) {
    67  	httpClient, err := ro.NewClientPassword(oc)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	apiConfig := rs.NewConfiguration()
    72  	apiConfig.BasePath = urlutil.JoinAbsolute(oc.ServerURL, "/scim/v2")
    73  	apiConfig.HTTPClient = httpClient
    74  	apiClient := rs.NewAPIClient(apiConfig)
    75  	return apiClient, nil
    76  }
    77  
    78  func ApiResponseErrorBody(err error) []byte {
    79  	openAPIErr := err.(rc.GenericOpenAPIError)
    80  	return openAPIErr.Body()
    81  }