retrieve

package module
v0.0.0-...-2fd6822 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2025 License: MIT Imports: 13 Imported by: 0

README

Retrieve

Test

A lightweight and efficient Golang package for downloading files from the web with minimal code

Install

go get -u github.com/ciathefed/retrieve

Examples

package main

import (
    "log"
    "github.com/ciathefed/retrieve"
)

func main() {
    err := retrieve.New("https://example.com").
        SetOutput("filename.ext").
        Exec()
    if err != nil {
        log.Fatalf("failed to download file: %v", err)
    }
}

You can find all the examples here

Contributing

Contributions are welcome! Please follow these steps to contribute:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/YourFeature).
  3. Make your changes and commit them (git commit -m 'Add some feature').
  4. Push to the branch (git push origin feature/YourFeature).
  5. Open a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

func New

func New(url string) *Builder

New initializes a new Builder instance with the specified URL.

func (*Builder) BuildURL

func (b *Builder) BuildURL() (string, error)

BuildURL constructs and returns the final URL with all query parameters applied.

func (*Builder) Exec

func (b *Builder) Exec() error

Exec executes the HTTP request and downloads the file.

func (*Builder) GetBody

func (b *Builder) GetBody() (string, error)

GetBody returns the request body as a string, if set.

func (*Builder) GetContext

func (b *Builder) GetContext() context.Context

GetContext returns the context associated with the request.

func (*Builder) GetHeaders

func (b *Builder) GetHeaders() map[string]string

GetHeaders returns the headers set for the request.

func (*Builder) GetMethod

func (b *Builder) GetMethod() string

GetMethod returns the HTTP method set for the request.

func (*Builder) GetOutput

func (b *Builder) GetOutput() string

GetOutput returns the output path set for the response.

func (*Builder) GetTimeout

func (b *Builder) GetTimeout() time.Duration

GetTimeout returns the timeout duration set for the request.

func (*Builder) GetUrl

func (b *Builder) GetUrl() string

GetUrl returns the current URL.

func (*Builder) IgnoreStatusCode

func (b *Builder) IgnoreStatusCode() *Builder

IgnoreStatusCode allows responses with non-2xx status codes to be processed without error.

func (*Builder) IsIgnoreStatusCode

func (b *Builder) IsIgnoreStatusCode() bool

IsIgnoreStatusCode returns whether the request should ignore non-2xx status codes.

func (*Builder) SetBody

func (b *Builder) SetBody(body any) *Builder

SetBody sets the request body.

If the input is a string, it's used as-is. If the input is a byte slice, it's wrapped in a reader. If the input is any other type, it's serialized to JSON.

Automatically sets the "Content-Type" header to "application/json" if JSON encoding is used.

func (*Builder) SetContext

func (b *Builder) SetContext(ctx context.Context) *Builder

SetContext allows setting a custom context for the request.

This is useful for handling request cancellation and deadlines.

func (*Builder) SetHeader

func (b *Builder) SetHeader(key string, value string) *Builder

SetHeader adds or updates an HTTP header in the request.

func (*Builder) SetHeaders

func (b *Builder) SetHeaders(headers map[string]string) *Builder

SetHeaders adds or updates multiple HTTP headers in the request.

func (*Builder) SetMethod

func (b *Builder) SetMethod(method string) *Builder

SetMethod specifies the HTTP method for the request.

Supported methods: "GET", "POST", "PUT", "PATCH".

func (*Builder) SetOutput

func (b *Builder) SetOutput(output string) *Builder

SetOutput defines the file path or directory where the downloaded content will be saved.

func (*Builder) SetQueryParam

func (b *Builder) SetQueryParam(key, value string) *Builder

SetQueryParam adds a single query parameter to the URL.

func (*Builder) SetQueryParams

func (b *Builder) SetQueryParams(params map[string]string) *Builder

SetQueryParams adds multiple query parameters to the URL.

func (*Builder) SetTimeout

func (b *Builder) SetTimeout(duration time.Duration) *Builder

SetTimeout configures the request timeout duration.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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