pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/sofyan48/cumi

="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-71a44d5be3f782c5.css" /> GitHub - sofyan48/cumi
Skip to content

sofyan48/cumi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cumi Http Requester

A simple HTTP client library for Go

Installation

go get github.com/sofyan48/cumi

Quick Start

Basic GET Request

package main

import (
    "fmt"
    "log"
    "github.com/sofyan48/cumi"
)

func main() {
    client := cumi.NewClient()
    resp, err := client.Http().Get("https://httpbin.org/get")
    if err != nil {
        log.Fatal(err)
    }
    
    fmt.Println("Status:", resp.Status)
    fmt.Println("Response:", resp.String())
}

SetSuccessResult - Auto JSON Parsing

Compared to standard net/http which requires manual steps:

// Traditional way with net/http
resp, err := http.Get("https://api.example.com/users/1")
if err != nil {
    return err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
    return err
}

var user User
err = json.Unmarshal(body, &user)
if err != nil {
    return err
}

With requester SetSuccessResult, it becomes much simpler:

// With requester - auto parsing!
var user User
client := cumi.NewClient()
resp, err := client.Http().
    SetSuccessResult(&user).
    Get("https://api.example.com/users/1")

if err != nil {
    return err
}

if resp.IsSuccess() {
    // user is already populated with data!
    fmt.Printf("User: %+v\n", user)
}

Features

  • SetSuccessResult/SetErrorResult - Automatic response parsing
  • Zero external dependencies - Only uses Go standard library
  • Built-in retry mechanism with configurable backoff
  • Authentication support - Basic Auth, Bearer Token, API Key
  • Request/Response middleware for logging and preprocessing
  • File upload/download with progress callbacks
  • Debug mode for request/response logging
  • TLS configuration for custom certificates
  • Context support for timeout and cancellation
  • Form data and JSON request bodies
  • Query parameters and path parameters
  • Cookie management
  • Custom headers per request or globally
  • Tracing support with OpenTelemetry integration

Examples

GET Request with Auto JSON Parsing

package main

import (
    "fmt"
    "log"
    "github.com/sofyan48/requester"
)

type User struct {
    ID   int    `json:"id"`
    Name string `json:"name"`
    Email string `json:"email"`
}

func main() {
    client := cumi.NewClient()
    var user User
    resp, err := client.Http().
        SetSuccessResult(&user).
        Get("https://jsonplaceholder.typicode.com/users/1")
    
    if err != nil {
        log.Fatal(err)
    }
    
    if resp.IsSuccess() {
        fmt.Printf("User: %+v\n", user)
    }
}

POST with JSON

func main() {
    client := cumi.NewClient()
    user := User{Name: "John", Email: "john@example.com"}
    
    resp, err := client.Http().
        SetBodyJSON(user).
        Post("https://httpbin.org/post")
    if err != nil {
        panic(err)
    }
    
    fmt.Println("Status:", resp.Status)
    fmt.Println("Response:", resp.String())
}

POST with Auto Result Binding

func main() {
    client := cumi.NewClient()
    user := User{Name: "John", Email: "john@example.com"}
    
    // Auto result binding with SetSuccessResult
    var result map[string]interface{}
    resp, err := client.Http().
        SetBodyJSON(user).
        SetSuccessResult(&result).
        Post("https://httpbin.org/post")
    
    if err != nil {
        panic(err)
    }
    
    if resp.IsSuccess() {
        fmt.Printf("Received JSON: %+v\n", result["json"])
    }
}

Error Handling with SetErrorResult

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

func main() {
    client := cumi.NewClient()
    var user User
    var apiError APIError
    
    resp, err := client.Http().
        SetSuccessResult(&user).
        SetErrorResult(&apiError).
        Get("https://api.example.com/users/999")
    
    if err != nil {
        log.Fatal(err)
    }
    
    if resp.IsSuccess() {
        fmt.Printf("User: %+v\n", user)
    } else {
        fmt.Printf("API Error: %+v\n", apiError)
    }
}

Client Configuration

client := cumi.NewClient().
    SetBaseURL("https://api.example.com").
    SetTimeout(30*time.Second).
    SetCommonHeader("Authorization", "Bearer your-token").
    SetRetryCount(3)

var users []User
resp, err := client.Http().
    SetSuccessResult(&users).
    Get("/users")

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy