Skip to content
Snippets Groups Projects
Select Git revision
  • 9f1116b2b16b6ba2a29e2a9129db5e662ceb8b96
  • master default protected
  • v1.23.2
  • v1.23.1
  • v1.23.0
  • v1.22.0
  • v1.21.1
  • v1.21.0
  • v1.20.3
  • v1.20.2
  • v1.20.1
  • v1.20.0
  • v1.19.4
  • v1.19.3
  • v1.19.2
  • v1.19.1
  • v1.19.0
  • v1.18.2
  • v1.18.1
  • v1.18.0
  • v1.17.0
  • v1.16.1
22 results

runnable-http.go

Blame
  • runnable-http.go 1023 B
    package jobqueue
    
    import (
    	"bytes"
    	"io"
    	"net/http"
    )
    
    // HTTPResult is a result of a http request
    type HTTPResult struct {
    	StatusCode int
    	Body       string
    }
    
    type HTTPRunnable struct {
    	URL    string
    	Method string
    	Header map[string]string
    	Body   string
    }
    
    func (h *HTTPRunnable) Run() (RunResult[HTTPResult], error) {
    	client := &http.Client{}
    
    	reqBody := bytes.NewBufferString(h.Body)
    	req, err := http.NewRequest(h.Method, h.URL, reqBody)
    	if err != nil {
    		return RunResult[HTTPResult]{Status: ResultStatusFailed}, err
    	}
    
    	for key, value := range h.Header {
    		req.Header.Set(key, value)
    	}
    
    	resp, err := client.Do(req)
    	if err != nil {
    		return RunResult[HTTPResult]{Status: ResultStatusFailed}, err
    	}
    	defer resp.Body.Close()
    
    	body, err := io.ReadAll(resp.Body)
    	if err != nil {
    		return RunResult[HTTPResult]{Status: ResultStatusFailed}, err
    	}
    
    	return RunResult[HTTPResult]{
    		Status: ResultStatusSuccess,
    		Data: HTTPResult{
    			StatusCode: resp.StatusCode,
    			Body:       string(body),
    		},
    	}, nil
    }