Skip to content

Commit

Permalink
Timeouts (#75)
Browse files Browse the repository at this point in the history
* configurable client timeout

* syntax fixes

* timeout checks and errs

* configurable timeout
  • Loading branch information
Jmfwolf committed Jul 31, 2023
1 parent cd301ae commit 5a96162
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
21 changes: 16 additions & 5 deletions pkg/onelogin/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/http"
"net/url"
"os"
"strconv"
"time"

"github.com/onelogin/onelogin-go-sdk/v4/pkg/onelogin/authentication"
olerror "github.com/onelogin/onelogin-go-sdk/v4/pkg/onelogin/error"
Expand All @@ -21,6 +23,7 @@ type Client struct {
HttpClient HTTPClient // HTTPClient interface for making HTTP requests
Auth *authentication.Authenticator // Authenticator for managing authentication
OLdomain string // OneLogin domain
Timeout time.Duration
}

// HTTPClient is an interface that defines the Do method for making HTTP requests.
Expand All @@ -39,15 +42,23 @@ func NewClient() (*Client, error) {
subdomain := os.Getenv("ONELOGIN_SUBDOMAIN")
old := fmt.Sprintf("https://%s.onelogin.com", subdomain)
authenticator := authentication.NewAuthenticator(subdomain)
err := authenticator.GenerateToken()
timeoutStr := os.Getenv("ONELOGIN_TIMEOUT")
timeout, err := strconv.Atoi(timeoutStr)
if err != nil || timeout <= 0 {
timeout = 10
}
timeoutDuration := time.Second * time.Duration(timeout)

err = authenticator.GenerateToken()
if err != nil {
return nil, err
}

return &Client{
HttpClient: http.DefaultClient,
Auth: authenticator,
OLdomain: old,
HttpClient: &http.Client{
Timeout: timeoutDuration,
},
Auth: authenticator,
OLdomain: old,
}, nil
}

Expand Down
54 changes: 29 additions & 25 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ This is the Onelogin SDK, a Go package that provides a convenient interface for
- **Data Models**: Represent Onelogin entities and resources.
- **Utilities**: Provide utility functions and helper methods.


## Supported APIs

- [API Authorization](https://developers.onelogin.com/api-docs/2/api-authorization/overview)
- [Apps](https://developers.onelogin.com/api-docs/2/apps)
- [App Rules](https://developers.onelogin.com/api-docs/2/app-rules)
Expand All @@ -26,6 +26,7 @@ This is the Onelogin SDK, a Go package that provides a convenient interface for
- [User Mappings](https://developers.onelogin.com/api-docs/2/user-mappings)

## Partially Support APIs

- [MFA](https://developers.onelogin.com/api-docs/2/multi-factor-authentication)

## Installation
Expand All @@ -43,6 +44,7 @@ The SDK expects three environment variables to be set for authentication:
- `ONELOGIN_CLIENT_ID`
- `ONELOGIN_CLIENT_SECRET`
- `ONELOGIN_SUBDOMAIN`
- `ONELOGIN_TIMEOUT`

These variables are used by the Authenticator for authentication with the OneLogin API. The Authenticator retrieves an access token using these credentials, which is then used for API requests. You can set these variables directly in your shell or in the environment of the program that will be using this SDK.

Expand All @@ -52,6 +54,7 @@ In a Unix/Linux shell, you can use the `export` command to set these variables:
export ONELOGIN_CLIENT_ID=your_client_id
export ONELOGIN_CLIENT_SECRET=your_client_secret
export ONELOGIN_SUBDOMAIN=your_subdomain
export ONELOGIN_TIMEOUT=15
```

In a Go program, you can use the `os` package to set these variables:
Expand All @@ -60,6 +63,7 @@ In a Go program, you can use the `os` package to set these variables:
os.Setenv("ONELOGIN_CLIENT_ID", "your_client_id")
os.Setenv("ONELOGIN_CLIENT_SECRET", "your_client_secret")
os.Setenv("ONELOGIN_SUBDOMAIN", "your_subdomain")
os.Setenv("ONELOGIN_TIMEOUT", 15)
```

Please ensure these variables are set before attempting to use the SDK to make API requests.
Expand All @@ -72,33 +76,33 @@ Here's an example demonstrating how to use the Onelogin SDK:
package main

import (
"fmt"
"fmt"

"github.com/onelogin/onelogin-go-sdk/v4/pkg/onelogin/models"
"github.com/onelogin/onelogin-go-sdk/v4/pkg/onelogin"
"github.com/onelogin/onelogin-go-sdk/v4/pkg/onelogin/models"
"github.com/onelogin/onelogin-go-sdk/v4/pkg/onelogin"
)

func main() {
ol, err := onelogin.NewOneloginSDK()
if err != nil {
fmt.Println("Unable to initialize client:", err)
return
}
userQuery := models.UserQuery{}
userList, err := ol.GetUsers(&userQuery)
if err != nil {
fmt.Println("Failed to get user:", err)
return
}
fmt.Println(userList)

appQuery := models.AppQuery{}
appList, err := ol.GetApps(&appQuery)
if err != nil {
fmt.Println("Failed to get app list:", err)
return
}
fmt.Println("App List:", appList)
ol, err := onelogin.NewOneloginSDK()
if err != nil {
fmt.Println("Unable to initialize client:", err)
return
}
userQuery := models.UserQuery{}
userList, err := ol.GetUsers(&userQuery)
if err != nil {
fmt.Println("Failed to get user:", err)
return
}
fmt.Println(userList)

appQuery := models.AppQuery{}
appList, err := ol.GetApps(&appQuery)
if err != nil {
fmt.Println("Failed to get app list:", err)
return
}
fmt.Println("App List:", appList)
}
```

Expand All @@ -115,4 +119,4 @@ func main() {

## Contributing

Contributions to the Onelogin SDK are welcome! If you encounter any issues or have suggestions for improvement, please open an issue or submit a pull request. We appreciate your feedback and contributions.
Contributions to the Onelogin SDK are welcome! If you encounter any issues or have suggestions for improvement, please open an issue or submit a pull request. We appreciate your feedback and contributions.

0 comments on commit 5a96162

Please sign in to comment.