Skip to content
Snippets Groups Projects
Select Git revision
  • 1b3bdd2f3e7db1bfd6529dac3ad88725915608cc
  • master default protected
  • 1.31
  • 4.38.8
  • 4.38.7
  • 4.38.6
  • 4.38.5
  • 4.38.4
  • 4.38.3
  • 4.38.2
  • 4.38.1
  • 4.38.0
  • 4.37.2
  • 4.37.1
  • 4.37.0
  • 4.36.0
  • 4.35.0
  • 4.34.1
  • 4.34.0
  • 4.33.1
  • 4.33.0
  • 4.32.2
  • 4.32.1
23 results

ionicons.min.css

Blame
  • runnable-http.go 2.44 KiB
    // Copyright 2023 schukai GmbH
    // SPDX-License-Identifier: AGPL-3.0
    
    package jobqueue
    
    import (
    	"bytes"
    	"context"
    	"fmt"
    	"io"
    	"net/http"
    	"sync"
    )
    
    func NewHTTPRunnableFromMap(data map[string]interface{}) (*HTTPRunnable, error) {
    	url, ok := data["url"].(string)
    	if !ok {
    		return nil, fmt.Errorf("%w: Invalid URL: %v", ErrInvalidData, data["url"])
    	}
    
    	method, ok := data["method"].(string)
    	if !ok {
    		return nil, fmt.Errorf("%w: Invalid Method: %v", ErrInvalidData, data["method"])
    	}
    
    	header, ok := data["header"].(map[string]string)
    	if !ok {
    		return nil, fmt.Errorf("%w: Invalid Header: %v", ErrInvalidData, data["header"])
    	}
    
    	body, ok := data["body"].(string)
    	if !ok {
    		return nil, fmt.Errorf("%w: Invalid Body: %v", ErrInvalidData, data["body"])
    	}
    
    	return &HTTPRunnable{
    		URL:    url,
    		Method: method,
    		Header: header,
    		Body:   body,
    	}, nil
    }
    
    // HTTPResult is a result of a http request
    type HTTPResult struct {
    	StatusCode int
    	Body       string
    }
    
    func (h *HTTPResult) GetResult() string {
    	return fmt.Sprintf("StatusCode: %d\n\n%s", h.StatusCode, h.Body)
    }
    
    func (h *HTTPResult) GetError() (string, int) {
    	if h.StatusCode >= 400 {
    		return fmt.Sprintf("StatusCode: %d\n\n%s", h.StatusCode, h.Body), h.StatusCode
    	}
    	return "", 0
    }
    
    type HTTPRunnable struct {
    	URL    string
    	Method string
    	Header map[string]string
    	Body   string
    	mu     sync.Mutex
    }
    
    func (h *HTTPRunnable) Run(ctx context.Context) (RunResult[HTTPResult], error) {
    	client := &http.Client{}