From 029568f2a880f3f4e8f25acce027958288c3edc0 Mon Sep 17 00:00:00 2001 From: Joseph Little Date: Thu, 20 Jun 2024 15:58:32 +0100 Subject: [PATCH 1/3] from file beginning --- httpclient/builders.go | 51 ++++++++++++++++++++++++++++++++++++++++++ httpclient/client.go | 16 ++++++------- 2 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 httpclient/builders.go diff --git a/httpclient/builders.go b/httpclient/builders.go new file mode 100644 index 0000000..30188bf --- /dev/null +++ b/httpclient/builders.go @@ -0,0 +1,51 @@ +package httpclient + +import ( + "encoding/json" + "fmt" + "io" + "os" +) + +type cookieWithJson struct{} + +type configProcessedVals struct { + CustomCookies []cookieWithJson `json:"custom_cookies>cookie"` + CustomTimeoutSeconds int `json:"custom_timeout_seconds"` + TokenRefreshBufferPeriodSeconds int `json:"token_refresh_buffer_period_seconds"` + TotalRetryDurationSeconds int `json:"total_retry_duration_seconds"` +} + +func BuildConfigFromJsonFile(filepath string) (ClientConfig, error) { + var clientConfig ClientConfig + var processorContainer *configProcessedVals + + // Load file to base struct + loadConfigFromJSONFile(filepath, clientConfig) + + // Load file to processor struct + loadConfigFromJSONFile(filepath, processorContainer) + + return clientConfig, nil +} + +// loadCombinedConfig loads the combined configuration from a JSON file +func loadConfigFromJSONFile(configFilePath string, home any) error { + file, err := os.Open(configFilePath) + if err != nil { + return fmt.Errorf("could not open file: %v", err) + } + defer file.Close() + + byteValue, err := io.ReadAll(file) + if err != nil { + return fmt.Errorf("could not read file: %v", err) + } + + err = json.Unmarshal(byteValue, &home) + if err != nil { + return fmt.Errorf("could not unmarshal JSON: %v", err) + } + + return nil +} diff --git a/httpclient/client.go b/httpclient/client.go index 1ace4dc..e509027 100644 --- a/httpclient/client.go +++ b/httpclient/client.go @@ -36,17 +36,17 @@ type Client struct { // Options/Variables for Client type ClientConfig struct { Integration APIIntegration - HideSensitiveData bool + HideSensitiveData bool `json:"hide_sensitive_data"` CustomCookies []*http.Cookie - MaxRetryAttempts int - MaxConcurrentRequests int - EnableDynamicRateLimiting bool + MaxRetryAttempts int `json:"max_retry_attempts"` + MaxConcurrentRequests int `json:"max_concurrent_requests"` + EnableDynamicRateLimiting bool `json:"enable_dynamic_rate_limiting"` CustomTimeout time.Duration TokenRefreshBufferPeriod time.Duration - TotalRetryDuration time.Duration // TODO do we need this now it's in the integration? - FollowRedirects bool - MaxRedirects int - EnableConcurrencyManagement bool + TotalRetryDuration time.Duration + FollowRedirects bool `json:"follow_redirects"` + MaxRedirects int `json:"max_redirects"` + EnableConcurrencyManagement bool `json:"enable_concurrency_management"` } // BuildClient creates a new HTTP client with the provided configuration. From c5b99b60ea5c6f63324e521b84d74f15b3fe5960 Mon Sep 17 00:00:00 2001 From: ShocOne <62835948+ShocOne@users.noreply.github.com> Date: Fri, 21 Jun 2024 11:50:53 +0100 Subject: [PATCH 2/3] moved functionality to sdk layer --- httpclient/builders.go | 51 ------------------------------------------ 1 file changed, 51 deletions(-) delete mode 100644 httpclient/builders.go diff --git a/httpclient/builders.go b/httpclient/builders.go deleted file mode 100644 index 30188bf..0000000 --- a/httpclient/builders.go +++ /dev/null @@ -1,51 +0,0 @@ -package httpclient - -import ( - "encoding/json" - "fmt" - "io" - "os" -) - -type cookieWithJson struct{} - -type configProcessedVals struct { - CustomCookies []cookieWithJson `json:"custom_cookies>cookie"` - CustomTimeoutSeconds int `json:"custom_timeout_seconds"` - TokenRefreshBufferPeriodSeconds int `json:"token_refresh_buffer_period_seconds"` - TotalRetryDurationSeconds int `json:"total_retry_duration_seconds"` -} - -func BuildConfigFromJsonFile(filepath string) (ClientConfig, error) { - var clientConfig ClientConfig - var processorContainer *configProcessedVals - - // Load file to base struct - loadConfigFromJSONFile(filepath, clientConfig) - - // Load file to processor struct - loadConfigFromJSONFile(filepath, processorContainer) - - return clientConfig, nil -} - -// loadCombinedConfig loads the combined configuration from a JSON file -func loadConfigFromJSONFile(configFilePath string, home any) error { - file, err := os.Open(configFilePath) - if err != nil { - return fmt.Errorf("could not open file: %v", err) - } - defer file.Close() - - byteValue, err := io.ReadAll(file) - if err != nil { - return fmt.Errorf("could not read file: %v", err) - } - - err = json.Unmarshal(byteValue, &home) - if err != nil { - return fmt.Errorf("could not unmarshal JSON: %v", err) - } - - return nil -} From a3b62009f2cad2d207a29074e3a6513f479d32f0 Mon Sep 17 00:00:00 2001 From: ShocOne <62835948+ShocOne@users.noreply.github.com> Date: Fri, 21 Jun 2024 13:36:27 +0100 Subject: [PATCH 3/3] chore: Updated readme --- README.md | 55 ++++++++++++++----------------------------------------- 1 file changed, 14 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 41411b6..a6fbacd 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ This Go module offers a sophisticated HTTP client designed for seamless API interactions, with a strong emphasis on concurrency management, robust error handling, extensive logging, and adaptive rate limiting. It's particularly suitable for applications requiring high-throughput API interactions with complex authentication and operational resilience. -This client leverages API-specific SDKs to provide a comprehensive and consistent interface for interacting with various APIs, including Microsoft Graph, Jamf Pro, and others. It is designed to be easily extensible to support additional APIs and to be highly configurable to meet specific API requirements. It achieves this through using a modular design, with a core HTTP client and API-specific handlers that encapsulate the unique requirements of each API supported. +This client is designed to be used with targetted SDK's and terraform providers only. As such the http client cannot be used without a supporting SDK and associated api integration plugin [go-api-http-client-integrations](https://github.com/deploymenttheory/go-api-http-client-integrations). -This HTTP client is intended to be used with targetted SDK's and terraform providers only. As such the http client cannot be used without a supporting SDK. +The plugin is required to provide the necessary API-specific handlers and configuration to the HTTP client. The plugin approach is designed to provide a consistent interface for interacting with various APIs, including Microsoft Graph, Jamf Pro, and others. It is easily extensible to support additional APIs and highly configurable to meet specific API requirements. It achieves this through using a modular design, with a core HTTP client and API-specific handlers that encapsulate the unique requirements of each API supported. Conseqently the client provides core common HTTP client functionality, such as rate limiting, logging, concurrency and responses while the plugin provides the API-specific logic, such as encoding and decoding requests, managing authentication endpoints, and handling API-specific logic. -## Features +## HTTP Client Features - **Comprehensive Authentication Support**: Robust support for various authentication schemes, including OAuth and Bearer Token, with built-in token management and validation. - **Advanced Concurrency Management**: An intelligent Concurrency Manager dynamically adjusts concurrent request limits to optimize throughput and adhere to API rate limits. @@ -20,42 +20,19 @@ This HTTP client is intended to be used with targetted SDK's and terraform provi - **API Handler Interface**: Provides a flexible and extensible way to interact with different APIs, including encoding and decoding requests and responses, managing authentication endpoints, and handling API-specific logic. - **Configuration via JSON or Environment Variables**: The Go API HTTP Client supports configuration via JSON files or environment variables, providing flexibility in defining authentication credentials, API endpoints, logging settings, and other parameters. -TBC - ## Getting Started -' -{ - "Integration": { - "FQDN": "https://api.example.com", - "AuthMethodDescriptor": "Bearer" - // other necessary fields for integration - }, - "HideSensitiveData": true, - "CustomCookies": [ - { - "Name": "sessionid", - "Value": "abc123", - "Path": "/", - "Domain": "example.com", - "Expires": "2025-01-02T15:04:05Z", - "MaxAge": 86400, - "Secure": true, - "HttpOnly": true, - "SameSite": 1 - } - ], - "MaxRetryAttempts": 5, - "MaxConcurrentRequests": 10, - "EnableDynamicRateLimiting": true, - "CustomTimeout": "10s", - "TokenRefreshBufferPeriod": "2m", - "TotalRetryDuration": "10m", - "FollowRedirects": true, - "MaxRedirects": 3, - "ConcurrencyManagementEnabled": true -} -' +This SDK requires an existing Go environment to build and run. If you do not have Go installed, you can download and install it from the [official Go website](https://golang.org/doc/install). + + + +## SDK Support + +This http client is used in conjuction with the following SDKs: + +- [go-api-sdk-m365](https://github.com/deploymenttheory/go-api-sdk-m365) + +- [go-api-sdk-jamfpro](https://github.com/deploymenttheory/go-api-sdk-jamfpro) ## Reporting Issues and Feedback @@ -75,10 +52,6 @@ If you would like to become an active contributor to this repository or project, ## Learn More -* [GitHub Documentation][GitHubDocs] -* [Azure DevOps Documentation][AzureDevOpsDocs] -* [Microsoft Azure Documentation][MicrosoftAzureDocs] -