https://github.com/juniorvieira99/jr_httpcodes
A component for a large logging system that deal with https codes and methods.
https://github.com/juniorvieira99/jr_httpcodes
code-https http http-client http-requests requests
Last synced: 6 days ago
JSON representation
A component for a large logging system that deal with https codes and methods.
- Host: GitHub
- URL: https://github.com/juniorvieira99/jr_httpcodes
- Owner: JuniorVieira99
- License: mit
- Created: 2025-03-24T16:19:21.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-24T19:11:05.000Z (over 1 year ago)
- Last Synced: 2025-04-02T14:16:32.610Z (about 1 year ago)
- Topics: code-https, http, http-client, http-requests, requests
- Language: Go
- Homepage:
- Size: 30.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# HTTP Codes Library for Go

[](https://godoc.org/github.com/JuniorVieira99/jr_httpcodes)
[](https://goreportcard.com/report/github.com/JuniorVieira99/jr_httpcodes)
[](https://github.com/JuniorVieira99/jr_httpcodes/actions/workflows/tests_workflow.yaml)

A lightweight, type-safe Go library for working with HTTP status codes and HTTP methods. This library provides constants, validation utilities, and human-readable descriptions for all standard HTTP status codes and methods.
## Index
- [Installation](#installation)
- [Key Features](#key-features)
- [Quick Start](#quick-start)
- [Usage Examples](#usage-examples)
- [Working with Status Codes](#working-with-status-codes)
- [Working with HTTP Methods](#working-with-http-methods)
- [Documentation](#documentation)
- [API Reference](#api-reference)
- [Status Code Functions](#status-code-functions)
- [Method Functions](#method-functions)
- [Available Constants](#available-constants)
- [Thread Safety](#thread-safety)
- [Overall Use](#overall-use)
- [License](#license)
## Installation
```bash
go get github.com/JuniorVieira99/jr_httpcodes
```
## Key Features
- Type-safe HTTP status codes and methods
- Human-readable descriptions for all standard HTTP status codes
- Validation utilities for status codes and methods
- Thread-safe registration of custom codes and methods
- Utility functions for debugging and logging
- **NOTE**: Check the docs folder for detailed information.
## Quick Start
```go
import (
"fmt"
"github.com/JuniorVieira99/jr_httpcodes"
)
func main() {
codes.Ok.Print() // Output: "OK -> Request succeeded and response contains requested data"
}
```
## Usage Examples
### Working with Status Codes
```go
import (
"fmt"
"github.com/JuniorVieira99/jr_httpcodes"
)
func main() {
// Call a status code
code := codes.OK
// Check status code category
if codes.IsSuccess(codes.OK) {
fmt.Println("This is a success status code")
}
// Get description
fmt.Println(codes.GetStatusInfo(codes.NotFound))
// Output: "Requested resource could not be found"
// Validate a status code
ok := codes.IsValidStatusCode(codes.StatusCode(999))
if !ok {
fmt.Println("Invalid status code")
}
// String representation
myCode:=codes.InternalServerError.String()
// Direct print
codes.InternalServerError.Print()
// Output: "500 -> Server encountered unexpected condition"
// Register a custom status code
customCode := codes.StatusCode(599)
customDesc := codes.Description("My Custom Error")
codes.RegisterStatusCode(customCode, customDesc)
// Delete custom status code
codes.DeleteStatusCode(customCode)
}
```
### Working with HTTP Methods
```go
import (
"fmt"
"github.com/JuniorVieira99/jr_httpcodes"
)
func main() {
// Call a method
method := codes.POST
// Get method description
fmt.Println(codes.GetMethodDescription(method))
// Output: "Send data to server for processing"
// String representation
methodStr:= codes.GET.String()
// Direct print
codes.GET.Print()
// Output: "GET -> Retrieve data from server"
// Validate a method
err := codes.ValidateMethod(codes.Method("INVALID"))
if err != nil {
fmt.Println("Invalid method")
}
// Register a custom method
customMethod := codes.Method("CUSTOM")
customDesc := codes.Description("My Custom Method")
codes.RegisterMethod(customMethod, customDesc)
// Delete custom method
codes.DeleteMethod(customMethod)
}
```
## Documentation
For local documentation check the `docs`folder.
For online documentation check the [GoDoc](https://pkg.go.dev/github.com/JuniorVieira99/jr_httpcodes).
## API Reference
### Status Code Functions
| Function | Description |
|----------|-------------|
| `IsValidStatusCode(code StatusCode) bool` | Checks if a status code is valid (100-599) |
| `IsInformational(code StatusCode) bool` | Checks if a code is informational (1xx) |
| `IsSuccess(code StatusCode) bool` | Checks if a code indicates success (2xx) |
| `IsRedirection(code StatusCode) bool` | Checks if a code indicates redirection (3xx) |
| `IsClientError(code StatusCode) bool` | Checks if a code indicates client error (4xx) |
| `IsServerError(code StatusCode) bool` | Checks if a code indicates server error (5xx) |
| `ValidateStatusCode(code StatusCode) error` | Returns error for invalid status codes |
| `GetStatusInfo(code StatusCode) string` | Returns human-readable description |
| `RegisterStatusCode(code StatusCode, desc Description)` | Registers a custom status code |
| `DeleteStatusCode(code StatusCode)` | Deletes a custom status code |
| `String() string` | Returns human-readable representation |
| `Print() string` | Prints the status code to the console |
| `CallMap() map[StatusCode]Description` | A map of status codes to status descriptions |
| `StringStatusCodeMap() string` | Returns a string representation of the status code map |
| `PrintStatusCodeMap()` | Prints the status code map to the console |
### Method Functions
| Function | Description |
|----------|-------------|
| `ValidateMethod(method Method) error` | Returns error for invalid methods |
| `GetMethodDescription(method Method) string` | Returns human-readable description |
| `RegisterMethod(method Method, desc Description)` | Registers a custom method |
| `DeleteMethod(method Method)` | Deletes a custom method |
| `String() string` | Returns human-readable representation |
| `Print() string` | Prints the method to the console |
| `CallMap() map[Method]Description` | A map of method names to method functions |
| `StringMethodMap() string` | Returns a string representation of the method map |
| `PrintMethodMap()` | Prints the method map to the console |
## Available Constants
The library includes constants for all standard HTTP status codes (100-511) and methods (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, CONNECT, TRACE).
**Note**: Check docs for detail information.
## Thread Safety
All registration functions are thread-safe and can be called from multiple goroutines.
## Tests
To run tests, use the following command:
```bash
go test ./tests
```
## Overall Use
```go
import (
"fmt"
"github.com/JuniorVieira99/jr_httpcodes"
)
func main()
{
// STATUS CODE
// ----------------
// Check status code category
if codes.IsSuccess(codes.OK) {
fmt.Println("This is a success status code")
}
// Get description
fmt.Println(codes.GetStatusInfo(codes.NotFound))
// Output: "Requested resource could not be found"
// Validate a status code
err := codes.ValidateStatusCode(codes.StatusCode(999))
if err != nil {
fmt.Println("Invalid status code")
}
// String representation
fmt.Println(codes.InternalServerError.String())
// Output: "500 -> Server encountered unexpected condition"
// Print status code
codes.InternalServerError.Print()
// Output: "500 -> Server encountered unexpected condition"
// Register a custom status code
codes.RegisterStatusCode(codes.StatusCode(599), codes.Description("My Custom Error"))
// Delete custom status code
codes.DeleteStatusCode(codes.StatusCode(599))
// METHOD
// ----------------
// Get method description
fmt.Println(codes.GetMethodDescription(codes.POST))
// Output: "Send data to server for processing"
// Validate a method
err := codes.ValidateMethod(codes.Method("INVALID"))
if err != nil {
fmt.Println("Invalid method")
}
// String representation
fmt.Println(codes.GET.String())
// Output: "GET -> Retrieve data from server"
// Register a custom method
codes.RegisterMethod(codes.Method("CUSTOM"), codes.Description("My Custom Method"))
// Delete custom method
codes.DeleteMethod(codes.Method("CUSTOM"))
}
```
## License
MIT License