jsonapi

package module
v0.0.0-...-d467397 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2023 License: MIT Imports: 5 Imported by: 0

README

JSON API Parser for Go

A lightweight and versatile JSON API parser library for the Go programming language. Simplify interaction with JSON-based APIs by effortlessly fetching and decoding responses into Go structs. Handle errors gracefully and set customizable timeouts for HTTP requests. This library is designed to streamline API consumption and response handling while adhering to best practices in error handling and maintainable code.

Features:

  • Fetch JSON data from APIs with ease using a clean and concise API.
  • Decode JSON responses directly into Go structs, eliminating manual parsing.
  • Smart error handling and descriptive error messages for effective debugging.
  • Set configurable timeouts for HTTP requests to ensure responsiveness.
  • Thoughtfully designed for maintainability and extensibility in your projects.

Well-documented codebase with clear examples to get you started quickly. Whether you're building web applications, microservices, or any project that involves interacting with JSON APIs, this library empowers you with a reliable foundation for efficient JSON data handling in your Go applications.

Installing
go get github.com/gobeybot/json-api-parser
Example Usage
package main

import (
	"fmt"
	"time"

	"github.com/gobeybot/json-api-parser"
)

func main() {
	baseURL := "https://api.example.com"
	timeout := 10 * time.Second // Example timeout setting

	client := jsonapi.NewJSONAPIClient(baseURL, timeout)

	// Example for a GET request

	var getResponse map[string]interface{}
	if err := client.Get("/data", &getResponse); err != nil {
		// Check if the error is a JSONAPIError
		if apiErr, ok := err.(*jsonapi.JSONAPIError); ok {
			fmt.Printf("API Error - Code: %d, Message: %s\n", apiErr.Code, apiErr.Message)
		} else {
			fmt.Printf("Unexpected Error: %v\n", err)
		}
		return
	}
	// Handle the successful response.
	fmt.Println("GET response:", getResponse)

	// Example for a POST request
	requestData := map[string]interface{}{
		"key": "value",
	}

	var postResponse map[string]interface{}
	if err := client.Post("/endpoint", requestData, &postResponse); err != nil {
		// Check if the error is a JSONAPIError
		if apiErr, ok := err.(*jsonapi.JSONAPIError); ok {
			fmt.Printf("API Error - Code: %d, Message: %s\n", apiErr.Code, apiErr.Message)
		} else {
			fmt.Printf("Unexpected Error: %v\n", err)
		}
		return
	}
	// Handle the successful response.
	fmt.Println("POST response:", postResponse)

	// You can use other HTTP methods similarly
}

Explore the possibilities of simplified JSON API interactions in your Go projects with the JSON API Parser library. Contributions and feedback are always welcome!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSONAPIClient

type JSONAPIClient struct {
	BaseURL string
	Client  *http.Client
}

JSONAPIClient defines the basic structure for a JSON API client.

func NewJSONAPIClient

func NewJSONAPIClient(baseURL string, timeout time.Duration) *JSONAPIClient

NewJSONAPIClient creates a new JSON API client.

func (*JSONAPIClient) Delete

func (c *JSONAPIClient) Delete(endpoint string) error

Delete performs an API request using the DELETE HTTP method.

func (*JSONAPIClient) Get

func (c *JSONAPIClient) Get(endpoint string, result interface{}) error

Get performs an API request using the GET HTTP method.

func (*JSONAPIClient) Head

func (c *JSONAPIClient) Head(endpoint string) error

Head performs an API request using the HEAD HTTP method.

func (*JSONAPIClient) Options

func (c *JSONAPIClient) Options(endpoint string) error

Options performs an API request using the OPTIONS HTTP method.

func (*JSONAPIClient) Patch

func (c *JSONAPIClient) Patch(endpoint string, requestData, result interface{}) error

Patch performs an API request using the PATCH HTTP method.

func (*JSONAPIClient) Post

func (c *JSONAPIClient) Post(endpoint string, requestData, result interface{}) error

Post performs an API request using the POST HTTP method.

func (*JSONAPIClient) Put

func (c *JSONAPIClient) Put(endpoint string, requestData, result interface{}) error

Put performs an API request using the PUT HTTP method.

type JSONAPIError

type JSONAPIError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

JSONAPIError is a custom error struct for JSON API errors.

func NewJSONAPIError

func NewJSONAPIError(code int, message string) *JSONAPIError

NewJSONAPIError creates a new JSONAPIError instance.

func (*JSONAPIError) Error

func (e *JSONAPIError) Error() string

Error returns the error message for the JSONAPIError.

Jump to

Keyboard shortcuts

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