refactor: remove utils pkg
BREAKING CHANGE: utils package has been removed in favor of specific new packages (http, crypto, strings)
This commit is contained in:
parent
251c476e17
commit
0ab5ea5a57
40 changed files with 131 additions and 126 deletions
|
@ -1,107 +0,0 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultHTTPClient = &http.Client{
|
||||
Timeout: time.Duration(30 * time.Second),
|
||||
}
|
||||
)
|
||||
|
||||
type Decoder interface {
|
||||
Decode(dst interface{}, src map[string][]string) error
|
||||
}
|
||||
type Encoder interface {
|
||||
Encode(src interface{}, dst map[string][]string) error
|
||||
}
|
||||
|
||||
type FormAuthorization func(url.Values)
|
||||
type RequestAuthorization func(*http.Request)
|
||||
|
||||
func AuthorizeBasic(user, password string) RequestAuthorization {
|
||||
return func(req *http.Request) {
|
||||
req.SetBasicAuth(url.QueryEscape(user), url.QueryEscape(password))
|
||||
}
|
||||
}
|
||||
|
||||
func FormRequest(endpoint string, request interface{}, encoder Encoder, authFn interface{}) (*http.Request, error) {
|
||||
form := url.Values{}
|
||||
if err := encoder.Encode(request, form); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if fn, ok := authFn.(FormAuthorization); ok {
|
||||
fn(form)
|
||||
}
|
||||
body := strings.NewReader(form.Encode())
|
||||
req, err := http.NewRequest("POST", endpoint, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if fn, ok := authFn.(RequestAuthorization); ok {
|
||||
fn(req)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func HttpRequest(client *http.Client, req *http.Request, response interface{}) error {
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to read response body: %v", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("http status not ok: %s %s", resp.Status, body)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, response)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to unmarshal response: %v %s", err, body)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func URLEncodeResponse(resp interface{}, encoder Encoder) (string, error) {
|
||||
values := make(map[string][]string)
|
||||
err := encoder.Encode(resp, values)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
v := url.Values(values)
|
||||
return v.Encode(), nil
|
||||
}
|
||||
|
||||
func StartServer(ctx context.Context, port string) {
|
||||
server := &http.Server{Addr: port}
|
||||
go func() {
|
||||
if err := server.ListenAndServe(); err != http.ErrServerClosed {
|
||||
log.Fatalf("ListenAndServe(): %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
ctxShutdown, cancelShutdown := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancelShutdown()
|
||||
err := server.Shutdown(ctxShutdown)
|
||||
if err != nil {
|
||||
log.Fatalf("Shutdown(): %v", err)
|
||||
}
|
||||
}()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue