go-api-http-client

module
v0.0.72 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2024 License: MIT

README

Go API HTTP Client

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.

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.
  • Structured Error Handling: Clear and actionable error reporting facilitates troubleshooting and improves reliability.
  • Performance Monitoring: Detailed performance metrics tracking provides insights into API interaction efficiency and optimization opportunities.
  • Configurable Logging: Extensive logging capabilities with customizable levels and formats aid in debugging and operational monitoring.
  • Adaptive Rate Limiting: Dynamic rate limiting automatically adjusts request rates in response to API server feedback.
  • Flexible Configuration: Extensive customization of HTTP client behavior to meet specific API requirements, including custom timeouts, retry strategies, header management, and more.
  • Header Management: Easy and efficient management of HTTP request headers, ensuring compliance with API requirements.
  • Enhanced Logging with Zap: Utilizes Uber's zap library for structured, high-performance logging, offering levels from Debug to Fatal, including structured context and dynamic adjustment based on the environment.
  • 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.

API Handler

The APIHandler interface abstracts the functionality needed to interact with various APIs, making the HTTP client adaptable to different API implementations. It includes methods for constructing resource and authentication endpoints, marshaling requests, handling responses, and managing API-specific headers.

Implementations

Currently, the HTTP client supports the following API handlers:

  • Jamf Pro: Tailored for interacting with Jamf Pro's API, providing specialized methods for device management and configuration.
  • Microsoft Graph: Designed for Microsoft Graph API, enabling access to various Microsoft 365 services.

Getting Started

Installation

To use this HTTP client in your project, add the package to your Go module dependencies:

go get github.com/yourusername/go-api-http-client
Usage

Example usage with a configuration file:

package main

import (
	"fmt"
	"log"

	"github.com/deploymenttheory/go-api-http-client/httpclient"
	"github.com/deploymenttheory/go-api-sdk-jamfpro/sdk/jamfpro"
)

func main() {
	configFilePath := "/path/to/clientconfig.json"
	loadedConfig, err := jamfpro.LoadClientConfig(configFilePath)
	if err != nil {
		log.Fatalf("Failed to load client OAuth configuration: %v", err)
	}

	config := httpclient.ClientConfig{
		Auth: httpclient.AuthConfig{
			ClientID:     loadedConfig.Auth.ClientID,
			ClientSecret: loadedConfig.Auth.ClientSecret,
		},
		Environment: httpclient.EnvironmentConfig{
			APIType:      loadedConfig.Environment.APIType,
			InstanceName: loadedConfig.Environment.InstanceName,
		},
		ClientOptions: httpclient.ClientOptions{
			LogLevel:          loadedConfig.ClientOptions.LogLevel,
			HideSensitiveData: loadedConfig.ClientOptions.HideSensitiveData,
			LogOutputFormat:   loadedConfig.ClientOptions.LogOutputFormat,
		},
	}

	client, err := jamfpro.BuildClient(config)
	if err != nil {
		log.Fatalf("Failed to create client: %v", err)
	}
}

Example configuration file (clientconfig.json):

{
   "Auth": {
    "ClientID": "client-id",
    "ClientSecret": "client-secret",
    "Username": "username",
    "Password": "password"
  },
  "Environment": {
    "InstanceName": "yourinstance",
    "OverrideBaseDomain": "",
    "APIType": "" // "jamfpro" / "graph"
  },
  "ClientOptions": {
    "LogLevel": "LogLevelDebug", // "LogLevelDebug" / "LogLevelInfo" / "LogLevelWarn" / "LogLevelError" / "LogLevelFatal" / "LogLevelPanic"
    "LogOutputFormat": "console", // "console" / "json"
    "LogConsoleSeparator": " ", // " " / "\t" / "," / etc.
    "HideSensitiveData": true,  // true / false
  }
}

Status

Super Linter

Sample Workflow

Reporting Issues and Feedback

Issues and Bugs

If you find any bugs, please file an issue in the GitHub Issues page. Please fill out the provided template with the appropriate information.

If you are taking the time to mention a problem, even a seemingly minor one, it is greatly appreciated, and a totally valid contribution to this project. Thank you!

Feedback

Contributions are welcome to make this HTTP client even better! Feel free to fork the repository, make your improvements, and submit a pull request. For major changes or new features, please file an issue or feature request in the GitHub Issues page to discuss what you would like to change.

Contribution

If you would like to become an active contributor to this repository or project, please follow the instructions provided in CONTRIBUTING.md.

Learn More

Directories

Path Synopsis
apihandlers
github
github_api_handler.go
github_api_handler.go
graph
graph_api_error_messages.go
graph_api_error_messages.go
jamfpro
jamfpro_api_error_messages.go
jamfpro_api_error_messages.go
http_error_handling.go This package provides utility functions and structures for handling and categorizing HTTP error responses.
http_error_handling.go This package provides utility functions and structures for handling and categorizing HTTP error responses.
httpclient_api_handler.go
httpclient_api_handler.go
logger.go
logger.go
version.go
version.go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL