Skip to content

Commit

Permalink
Merge pull request #35 from nathanielvarona/improvement/dry-unmarshal…
Browse files Browse the repository at this point in the history
…-handler-for-organization

DRY Unmarshal Handler for Organization
  • Loading branch information
nathanielvarona authored Apr 21, 2024
2 parents 2839fa9 + 63efda7 commit 7ab79d7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,5 @@ Check the [_examples](./_examples) folder for code examples demonstrating how to

## Alternative API Clients
* Python - [Pritunl API Client for Python](https://github.com/nathanielvarona/pritunl-api-python) by [@nathanielvarona](https://github.com/nathanielvarona)
- _(fork from [Pritunl API client for Python 3](https://github.com/ijat/pritunl-api-python) by [@ijat](https://github.com/ijat))_.
- _(fork from [Pritunl API client for Python 3](https://github.com/ijat/pritunl-api-python) by [@ijat](https://github.com/ijat))_
* Ruby - [Pritunl API Client](https://github.com/eterry1388/pritunl_api_client) by [@eterry1388](https://github.com/eterry1388)
35 changes: 20 additions & 15 deletions organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)

// OrganizationRequest represents the structure of the organization request
type OrganizationRequest struct {
Name string `json:"name"`
AuthApi bool `json:"auth_api"`
AuthToken bool `json:"auth_token"` // Addition for Put Method
AuthSecret bool `json:"auth_secret"` // Addition for Put Method
}

// OrganizationResponse represents the structure of the organization response
type OrganizationResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Expand All @@ -24,10 +25,6 @@ type OrganizationResponse struct {
UserCount int `json:"user_count"`
}

func handleUnmarshalOrganizations(body io.Reader, organizations *[]OrganizationResponse) error {
return handleUnmarshal(body, organizations)
}

// OrganizationGet retrieves a organization or organizations on the server
func (c *Client) OrganizationGet(ctx context.Context, orgId ...string) ([]OrganizationResponse, error) {
// The API path for the organization
Expand All @@ -38,28 +35,30 @@ func (c *Client) OrganizationGet(ctx context.Context, orgId ...string) ([]Organi
path = fmt.Sprintf("%s/%s", path, orgId[0]) // Use the first element if orgId is provided
}

// Send an authenticated HTTP GET request to the API
response, err := c.AuthRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}

// Handle the HTTP response
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()

// Unmarshal the JSON data using the helper function
// Unmarshal the JSON data into a slice of OrganizationResponse
var organizations []OrganizationResponse
if err := handleUnmarshalOrganizations(body, &organizations); err != nil {
if err := handleUnmarshal(body, &organizations); err != nil {
return nil, err
}

// Return the slice of organizations
return organizations, nil
}

// OrganizationCreate create a new organization on the server
// OrganizationCreate creates a new organization on the server
func (c *Client) OrganizationCreate(ctx context.Context, newOrganization OrganizationRequest) ([]OrganizationResponse, error) {
// Marshal the OrganizationRequest struct into JSON data
orgData, err := json.Marshal(newOrganization)
Expand All @@ -70,28 +69,30 @@ func (c *Client) OrganizationCreate(ctx context.Context, newOrganization Organiz
// The API path for the organization
path := "/organization"

// Send an authenticated HTTP POST request to the API with the organization data
response, err := c.AuthRequest(ctx, http.MethodPost, path, orgData)
if err != nil {
return nil, err
}

// Handle the HTTP response
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()

// Unmarshal the JSON data using the helper function
// Unmarshal the JSON data into a slice of OrganizationResponse
var organizations []OrganizationResponse
if err := handleUnmarshalOrganizations(body, &organizations); err != nil {
if err := handleUnmarshal(body, &organizations); err != nil {
return nil, err
}

// Return the slice of organizations
return organizations, nil
}

// OrganizationUpdate update an existing organization on the server
// OrganizationUpdate updates an existing organization on the server
func (c *Client) OrganizationUpdate(ctx context.Context, orgId string, updateOrganization OrganizationRequest) ([]OrganizationResponse, error) {
// Marshal the OrganizationRequest struct into JSON data
orgData, err := json.Marshal(updateOrganization)
Expand All @@ -102,37 +103,41 @@ func (c *Client) OrganizationUpdate(ctx context.Context, orgId string, updateOrg
// Construct the API path for the organization
path := fmt.Sprintf("/organization/%s", orgId)

// Send an authenticated HTTP PUT request to the API with the organization data
response, err := c.AuthRequest(ctx, http.MethodPut, path, orgData)
if err != nil {
return nil, err
}

// Handle the HTTP response
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()

// Unmarshal the JSON data using the helper function
// Unmarshal the JSON data into a slice of OrganizationResponse
var organizations []OrganizationResponse
if err := handleUnmarshalOrganizations(body, &organizations); err != nil {
if err := handleUnmarshal(body, &organizations); err != nil {
return nil, err
}

// Return the slice of organizations
return organizations, nil
}

// OrganizationDelete delete an existing organization on the server
// OrganizationDelete deletes an existing organization on the server
func (c *Client) OrganizationDelete(ctx context.Context, orgId string) ([]OrganizationResponse, error) {
// The API path for the organization
path := fmt.Sprintf("/organization/%s", orgId)

// Send an authenticated HTTP DELETE request to the API
response, err := c.AuthRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}

// Handle the HTTP response
body, err := handleResponse(response)
if err != nil {
return nil, err
Expand All @@ -141,7 +146,7 @@ func (c *Client) OrganizationDelete(ctx context.Context, orgId string) ([]Organi

// Unmarshal the JSON data using the helper function
var organizations []OrganizationResponse
if err := handleUnmarshalOrganizations(body, &organizations); err != nil {
if err := handleUnmarshal(body, &organizations); err != nil {
return nil, err
}

Expand Down

0 comments on commit 7ab79d7

Please sign in to comment.