NewApiClientFromConnection creates ApiClient based on given connection.
( ctx gocontext.Context, br context.BasicRes, connection plugin.ApiConnection, )
| 71 | |
| 72 | // NewApiClientFromConnection creates ApiClient based on given connection. |
| 73 | func NewApiClientFromConnection( |
| 74 | ctx gocontext.Context, |
| 75 | br context.BasicRes, |
| 76 | connection plugin.ApiConnection, |
| 77 | ) (*ApiClient, errors.Error) { |
| 78 | if reflect.ValueOf(connection).Kind() != reflect.Ptr { |
| 79 | panic(fmt.Errorf("connection is not a pointer")) |
| 80 | } |
| 81 | apiClient, err := NewApiClient(ctx, connection.GetEndpoint(), nil, 0, connection.GetProxy(), br) |
| 82 | if err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | |
| 86 | // if connection needs to prepare the ApiClient, i.e. fetch token for future requests |
| 87 | if prepareApiClient, ok := connection.(plugin.PrepareApiClient); ok { |
| 88 | err = prepareApiClient.PrepareApiClient(apiClient) |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // if connection requires authorization |
| 95 | if authenticator, ok := connection.(plugin.ApiAuthenticator); ok { |
| 96 | apiClient.SetAuthFunction(func(req *http.Request) errors.Error { |
| 97 | return authenticator.SetupAuthentication(req) |
| 98 | }) |
| 99 | } |
| 100 | |
| 101 | return apiClient, nil |
| 102 | } |
| 103 | |
| 104 | // NewApiClient creates a new synchronize ApiClient |
| 105 | func NewApiClient( |
no test coverage detected