Skip to content

Commit

Permalink
updated documents
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmfwolf committed Jun 20, 2023
1 parent f1774e0 commit 51da23a
Show file tree
Hide file tree
Showing 17 changed files with 125 additions and 81 deletions.
9 changes: 0 additions & 9 deletions docs/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,3 @@ func (a *Authenticator) GetToken() (string, error) {
}
```

## Error Handling

Errors in the Authentication module are represented using the `olError` package, and they can be of the following types:

- `AuthenticationError`: Represents an error that occurred during the authentication process, e.g., missing environment variables, failed authentication request, failed token generation or revocation.
- `RequestError`: Represents an error that occurred while creating or sending an HTTP request.
- `SerializationError`: Represents an error that occurred while marshalling or unmarshalling JSON data.

Each error type is associated with a specific error message that provides more details about the error.
33 changes: 33 additions & 0 deletions docs/error_handling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Error Handling

1. AuthenticationError:
- Purpose: Represents an error related to authentication.
- Fields:
- Message: Provides additional information about the error.

2. APIError:
- Purpose: Represents an error related to API calls.
- Fields:
- Message: Provides additional information about the error.
- Code: Specifies the error code associated with the API error.

3. SerializationError:
- Purpose: Represents an error related to serialization.
- Fields:
- Message: Provides additional information about the error.

4. SDKError:
- Purpose: Represents a general SDK error.
- Fields:
- Message: Provides additional information about the error.

5. RequestError:
- Purpose: Represents an error related to making an authentication request.
- Fields:
- Message: Provides additional information about the error.

Each error type has an associated Error() method that returns a formatted error message based on the error type and the provided error message. Additionally, there are corresponding New<ErrorType> functions that create and return an error instance with the specified error message.

To use these error types, you can import the `error` package and utilize the respective New<ErrorType> functions to create specific error instances when necessary.

Please note that it's important to handle and propagate errors appropriately in your code to ensure proper error handling and debugging.
1 change: 0 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ This documentation provides detailed information on how to use the SDK to intera

The SDK follows a modular structure with the following key components:

- `cmd/main.go`: Entry point of the SDK application.
- `internal/api`: Contains the implementation of the API client, request handling, and response parsing.
- `internal/authentication`: Provides authentication mechanisms and related functionality.
- `internal/error`: Defines various error types used by the SDK.
Expand Down
4 changes: 2 additions & 2 deletions docs/models.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SDK Models

The SDK provides several models that are used for data representation and interaction with the API. These models define the structure of the data that can be sent or received when using the SDK methods.
The SDK provides several models that define the structure of data used for data representation and interaction with the OneLogin API. These models are utilized in the SDK's client implementation to handle requests to the OneLogin API. The `Client` struct provides methods such as `Get`, `Delete`,`Post`, and `Put` that internally use the specified models to serialize data into JSON and send HTTP requests to the API. These methods handle authentication, path construction, query parameters, and request body serialization based on the provided models.

The following is a list of models available in the SDK:

Expand All @@ -10,7 +10,7 @@ The `App` model represents an application within the OneLogin platform. It conta

```go
type App struct {
ID int64 `json:"id"`
ID int64 `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
// ...
Expand Down
52 changes: 52 additions & 0 deletions docs/usage_examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
```go
package main

import (
"fmt"

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

func main() {
var testSSO = models.SSOOpenId{ClientID: "1234567890"}
var testConfig = models.ConfigurationOpenId{
RedirectURI: "https://example.com",
LoginURL: "https://example.com/login",
OidcApplicationType: 1,
TokenEndpointAuthMethod: 1,
AccessTokenExpirationMinutes: 60,
RefreshTokenExpirationMinutes: 60,
}

var (
connetorid int32 = 108419
name string = "Bow wow wow yippy yo yippy yay"
descr string = "Dog app"
)

client, err := onelogin.NewOneloginSDK()
if err != nil {
fmt.Println(err)
}
appQ := models.AppQuery{}
applist, err := client.GetApps(&appQ)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%+v\n", applist)

appT, err := client.CreateApp(models.App{
ConnectorID: &connetorid,
Name: &name,
Description: &descr,
SSO: testSSO,
Configuration: testConfig,
})
if err != nil {
fmt.Println(err)
}
fmt.Printf("%+v\n", appT)
}

```
15 changes: 5 additions & 10 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,13 @@ func (c *Client) DeleteWithBody(path *string, body interface{}) (*http.Response,

// Post sends a POST request to the specified path with the given query parameters and request body.
func (c *Client) Post(path *string, body interface{}) (*http.Response, error) {
var bodyReader io.Reader

if body != nil {
// Convert request body to JSON
jsonBody, err := json.Marshal(body)
if err != nil {
return nil, err
}
bodyReader = bytes.NewReader(jsonBody)
// Convert request body to JSON
jsonBody, err := json.Marshal(body)
if err != nil {
return nil, err
}

req, err := c.newRequest(http.MethodPost, path, nil, bodyReader)
req, err := c.newRequest(http.MethodPost, path, nil, bytes.NewReader(jsonBody))
if err != nil {
return nil, err
}
Expand Down
6 changes: 0 additions & 6 deletions internal/error/network_error.go

This file was deleted.

6 changes: 0 additions & 6 deletions internal/error/validation_error.go

This file was deleted.

14 changes: 9 additions & 5 deletions internal/models/app.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package models

type App struct {
ID int32 `json:"id,omitempty"`
ConnectorID int32 `json:"connector_id"`
Name string `json:"name"`
ID *int32 `json:"id,omitempty"`
ConnectorID *int32 `json:"connector_id"`
Name *string `json:"name"`
Description *string `json:"description,omitempty"`
Notes *string `json:"notes,omitempty"`
PolicyID *int `json:"policy_id,omitempty"`
Expand All @@ -17,8 +17,8 @@ type App struct {
RoleIDs *[]int `json:"role_ids,omitempty"`
AllowAssumedSignin *bool `json:"allow_assumed_signin,omitempty"`
Provisioning *Provisioning `json:"provisioning,omitempty"`
SSO *interface{} `json:"sso,omitempty"`
Configuration *interface{} `json:"configuration,omitempty"`
SSO interface{} `json:"sso,omitempty"`
Configuration interface{} `json:"configuration,omitempty"`
Parameters *map[string]Parameter `json:"parameters,omitempty"`
EnforcementPoint *EnforcementPoint `json:"enforcement_point,omitempty"`
}
Expand All @@ -27,6 +27,10 @@ type Provisioning struct {
Enabled bool `json:"enabled"`
}

type SSO interface {
ValidateSSO() error
}

type SSOOpenId struct {
ClientID string `json:"client_id"`
}
Expand Down
6 changes: 3 additions & 3 deletions internal/models/app_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ type AppRule struct {
}

type AppRuleQuery struct {
Limit string
Page string
Cursor string
Limit string `json:"limit,omitempty"`
Page string `json:"page,omitempty"`
Cursor string `json:"cursor,omitempty"`
Enabled bool `json:"enabled,omitempty"`
HasCondition *string `json:"has_condition,omitempty"`
HasConditionType *string `json:"has_condition_type,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions internal/models/privilege.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package models

// PrivilegeQuery represents available query parameters
type PrivilegeQuery struct {
Limit string
Page string
Cursor string
Limit string `json:"limit,omitempty"`
Page string `json:"page,omitempty"`
Cursor string `json:"cursor,omitempty"`
}

// Privilege represents the Role resource in OneLogin
Expand Down
6 changes: 3 additions & 3 deletions internal/models/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package models

// RoleQuery represents available query parameters
type RoleQuery struct {
Limit string
Page string
Cursor string
Limit string `json:"limit,omitempty"`
Page string `json:"page,omitempty"`
Cursor string `json:"cursor,omitempty"`
}

// Role represents the Role resource in OneLogin
Expand Down
8 changes: 4 additions & 4 deletions internal/models/smart_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ const (

// SmartHookQuery represents available query parameters
type SmartHookQuery struct {
Limit string
Page string
Cursor string
Type string
Limit string `json:"limit,omitempty"`
Page string `json:"page,omitempty"`
Cursor string `json:"cursor,omitempty"`
Type string `json:"type,omitempty"`
}

// SmartHook represents a OneLogin SmartHook with associated resource data
Expand Down
4 changes: 2 additions & 2 deletions pkg/onelogin/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const (
AppPath string = "api/2/apps"
)

func (sdk *OneloginSDK) CreateApp(app *mod.App) (interface{}, error) {
func (sdk *OneloginSDK) CreateApp(app mod.App) (interface{}, error) {
p, err := utl.BuildAPIPath(AppPath)
if err != nil {
return nil, err
Expand Down Expand Up @@ -74,7 +74,7 @@ func (sdk *OneloginSDK) DeleteApp(id int) (interface{}, error) {

}

func (sdk *OneloginSDK) CreateAppRule(id int, appRule *mod.AppRule) (interface{}, error) {
func (sdk *OneloginSDK) CreateAppRule(id int, appRule mod.AppRule) (interface{}, error) {
p, err := utl.BuildAPIPath(AppPath, id)
if err != nil {
return nil, err
Expand Down
5 changes: 4 additions & 1 deletion pkg/onelogin/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ var (
)

func (sdk *OneloginSDK) CreateRole(role *mod.Role) (interface{}, error) {
p := RolePath
p, err := utl.BuildAPIPath(RolePath)
if err != nil {
return nil, err
}
resp, err := sdk.Client.Post(&p, role)
if err != nil {
return nil, err
Expand Down
6 changes: 5 additions & 1 deletion pkg/onelogin/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ const (

// Users V2
func (sdk *OneloginSDK) CreateUser(user mod.User) (interface{}, error) {
p := UserPathV2
p, err := utl.BuildAPIPath(UserPathV2)
if err != nil {
return nil, err
}

resp, err := sdk.Client.Post(&p, user)
if err != nil {
return nil, err
Expand Down
25 changes: 0 additions & 25 deletions tests/error_handling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,6 @@ import (
"github.com/onelogin/onelogin-go-sdk/internal/error"
)

func TestValidationError(t *testing.T) {
expectedMessage := "mock message"
expectedCode := "mock code"
validationError := error.ValidationError{
Message: expectedMessage,
Code: expectedCode,
}

t.Run("when Message is accessed", func(t *testing.T) {
message := validationError.Message

if message != expectedMessage {
t.Errorf("unexpected message: got %q, want %q", message, expectedMessage)
}
})

t.Run("when Code is accessed", func(t *testing.T) {
code := validationError.Code

if code != expectedCode {
t.Errorf("unexpected code: got %q, want %q", code, expectedCode)
}
})
}

func TestNewSerializationError(t *testing.T) {
expectedMessage := "mock message"
err := error.NewSerializationError(expectedMessage)
Expand Down

0 comments on commit 51da23a

Please sign in to comment.