Go library for performing different image operations.
// gumlet/gumlet_url_builder.go
package gumlet
import (
"fmt"
"net/url"
"strings"
)
type GumletURLBuilder struct {
scheme string
host string
port int
path string
queryParams map[string]string
}
func NewGumletURLBuilder() *GumletURLBuilder {
return &GumletURLBuilder{
port: -1,
queryParams: make(map[string]string),
}
}
func (b *GumletURLBuilder) SetScheme(scheme string) *GumletURLBuilder {
b.scheme = scheme
return b
}
func (b *GumletURLBuilder) SetHost(host string) *GumletURLBuilder {
b.host = host
return b
}
func (b *GumletURLBuilder) SetPort(port int) *GumletURLBuilder {
b.port = port
return b
}
func (b *GumletURLBuilder) SetPath(path string) *GumletURLBuilder {
b.path = path
return b
}
func (b *GumletURLBuilder) AddQueryParameter(name, value string) *GumletURLBuilder {
b.queryParams[name] = value
return b
}
func (b *GumletURLBuilder) Build() (string, error) {
u := url.URL{}
if b.scheme != "" {
u.Scheme = b.scheme
}
if b.host != "" {
u.Host = b.host
}
if b.port != -1 {
u.Host = fmt.Sprintf("%s:%d", b.host, b.port)
}
if b.path != "" {
if !strings.HasPrefix(b.path, "/") {
b.path = "/" + b.path
}
u.Path = b.path
}
query := url.Values{}
for k, v := range b.queryParams {
query.Set(k, v)
}
u.RawQuery = query.Encode()
return u.String(), nil
}
A sample code:
// main.go
package main
import (
"fmt"
"gumlet"
)
func main() {
builder := NewGumletURLBuilder().
SetScheme("https").
SetHost("demo.gumlet.io").
SetPath("/flower.jpeg").
AddQueryParameter("width", "250").
AddQueryParameter("dpr", "2.0").
AddQueryParameter("overlay", "https://demo.gumlet.io/logo.png")
url, err := builder.Build()
if err != nil {
fmt.Println("Error building URL:", err)
} else {
fmt.Println("Built URL:", url) //output - Built URL: https://demo.gumlet.io/flower.jpeg?overlay=https%3A%2F%2Fdemo.gumlet.io%2Flogo.png&dpr=2.0&width=250
}
}