Compare commits
	
		
			40 Commits
		
	
	
		
			debug/chec
			...
			23a9d648d2
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					23a9d648d2 | ||
| 
						 | 
					a3029fa3f9 | ||
| 
						 | 
					387785b40c | ||
| 
						 | 
					03dea55131 | ||
| 
						 | 
					7b8aa989f6 | ||
| 
						 | 
					6ab6383144 | ||
| 
						 | 
					690d60f9d6 | ||
| 
						 | 
					da0de80afd | ||
| 
						 | 
					cd7ae788b1 | ||
| 
						 | 
					0d96cc53bf | ||
| 
						 | 
					66fc3c5b35 | ||
| 
						 | 
					5ab3eb8a38 | ||
| 
						 | 
					fec23b4acd | ||
| 
						 | 
					901622fee0 | ||
| 
						 | 
					527e622774 | ||
| 
						 | 
					7223b79fe8 | ||
| 
						 | 
					1ade41aeae | ||
| 
						 | 
					58dc579255 | ||
| 
						 | 
					370dac201b | ||
| 
						 | 
					2a763006db | ||
| 
						 | 
					522c66653b | ||
| 
						 | 
					b57f050b81 | ||
| 
						 | 
					41ebcf150a | ||
| 
						 | 
					1499def6ad | ||
| 
						 | 
					adbab0f5d7 | ||
| 88c88cac5b | |||
| 1ae38c98ad | |||
| 2d517cc594 | |||
| a9c82bd261 | |||
| 79aec86f5f | |||
| 9b3dfc7576 | |||
| 037ae74782 | |||
| b81c60a3ce | |||
| 363ac94c47 | |||
| 378f9e5095 | |||
| 659b494ee4 | |||
| 92965c6af2 | |||
| 70cb5aec9f | |||
| d59e77d5a2 | |||
| ff1b857ab0 | 
@@ -287,7 +287,7 @@ func (m *MongoDB) Search(filters *dbs.Filters, collection_name string) (*mongo.C
 | 
			
		||||
		return nil, 503, err
 | 
			
		||||
	}
 | 
			
		||||
	opts := options.Find()
 | 
			
		||||
	opts.SetLimit(100)
 | 
			
		||||
	opts.SetLimit(1000)
 | 
			
		||||
	targetDBCollection := CollectionMap[collection_name]
 | 
			
		||||
	orList := bson.A{}
 | 
			
		||||
	andList := bson.A{}
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,6 @@ import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"cloud.o-forge.io/core/oc-lib/tools"
 | 
			
		||||
@@ -29,28 +28,8 @@ type PeerCache struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// urlFormat formats the URL of the peer with the data type API function
 | 
			
		||||
func (p *PeerCache) urlFormat(url string, dt tools.DataType) string {
 | 
			
		||||
	// localhost is replaced by the local peer URL
 | 
			
		||||
	// because localhost must collide on a web request security protocol
 | 
			
		||||
	localhost := ""
 | 
			
		||||
	if strings.Contains(url, "localhost") {
 | 
			
		||||
		localhost = "localhost"
 | 
			
		||||
	}
 | 
			
		||||
	if strings.Contains(url, "127.0.0.1") {
 | 
			
		||||
		localhost = "127.0.0.1"
 | 
			
		||||
	}
 | 
			
		||||
	if localhost != "" {
 | 
			
		||||
		r := regexp.MustCompile("(" + localhost + ":[0-9]+)")
 | 
			
		||||
		t := r.FindString(url)
 | 
			
		||||
		if t != "" {
 | 
			
		||||
			url = strings.Replace(url, t, dt.API()+":8080/oc", -1)
 | 
			
		||||
		} else {
 | 
			
		||||
			url = strings.ReplaceAll(url, localhost, dt.API()+":8080/oc")
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		url = dt.API() + "/" + url
 | 
			
		||||
	}
 | 
			
		||||
	return url
 | 
			
		||||
func (p *PeerCache) urlFormat(hostUrl string, dt tools.DataType) string {
 | 
			
		||||
	return hostUrl + "/" + strings.ReplaceAll(dt.API(), "oc-", "")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// checkPeerStatus checks the status of a peer
 | 
			
		||||
@@ -65,22 +44,20 @@ func (p *PeerCache) checkPeerStatus(peerID string, appName string) (*Peer, bool)
 | 
			
		||||
	state, services := api.CheckRemotePeer(url)
 | 
			
		||||
	res.(*Peer).ServicesState = services                              // Update the services states of the peer
 | 
			
		||||
	access.UpdateOne(res, peerID)                                     // Update the peer in the db
 | 
			
		||||
	fmt.Println("URL peer : ", url)
 | 
			
		||||
	fmt.Println("State : ", state)
 | 
			
		||||
	fmt.Println("Services : ", services)
 | 
			
		||||
	return res.(*Peer), state != tools.DEAD && services[appName] == 0 // Return the peer and its status
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// LaunchPeerExecution launches an execution on a peer
 | 
			
		||||
// The method contacts the path described by : peer.Url + datatype path (from enums) + replacement of id by dataID
 | 
			
		||||
func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
 | 
			
		||||
	dt tools.DataType, method tools.METHOD, body interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) {
 | 
			
		||||
	fmt.Println("Launching peer execution on", caller.URLS, dt, method)
 | 
			
		||||
	methods := caller.URLS[dt] // Get the methods url of the data type
 | 
			
		||||
	if m, ok := methods[method]; !ok || m == "" {
 | 
			
		||||
		return nil, errors.New("no path found")
 | 
			
		||||
		return nil, errors.New("Requested method " + method.String() + " not declared in HTTPCaller")
 | 
			
		||||
	}
 | 
			
		||||
	meth := methods[method]                        // Get the method url to execute
 | 
			
		||||
	meth = strings.ReplaceAll(meth, ":id", dataID) // Replace the id in the url in case of a DELETE / UPDATE method (it's a standard naming in OC)
 | 
			
		||||
	path := methods[method]                        // Get the path corresponding to the action we want to execute
 | 
			
		||||
	path = strings.ReplaceAll(path, ":id", dataID) // Replace the id in the path in case of a DELETE / UPDATE method (it's a standard naming in OC)
 | 
			
		||||
	url := ""
 | 
			
		||||
 | 
			
		||||
	// Check the status of the peer
 | 
			
		||||
@@ -88,20 +65,20 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
 | 
			
		||||
		// If the peer is not reachable, add the execution to the failed executions list
 | 
			
		||||
		pexec := &PeerExecution{
 | 
			
		||||
			Method:   method.String(),
 | 
			
		||||
			Url:      p.urlFormat((mypeer.Url)+meth, dt),
 | 
			
		||||
			Url:      p.urlFormat((mypeer.Url), dt) + path, // the url is constitued of : host URL + resource path + action path (ex : mypeer.com/datacenter/resourcetype/path/to/action)
 | 
			
		||||
			Body:     body,
 | 
			
		||||
			DataType: dt.EnumIndex(),
 | 
			
		||||
			DataID:   dataID,
 | 
			
		||||
		}
 | 
			
		||||
		mypeer.AddExecution(*pexec)
 | 
			
		||||
		NewShallowAccessor().UpdateOne(mypeer, peerID) // Update the peer in the db
 | 
			
		||||
		return nil, errors.New("peer is not reachable")
 | 
			
		||||
		return nil, errors.New("peer is " + peerID + " not reachable")
 | 
			
		||||
	} else {
 | 
			
		||||
		if mypeer == nil {
 | 
			
		||||
			return nil, errors.New("peer not found")
 | 
			
		||||
			return nil, errors.New("peer " + peerID + " not found")
 | 
			
		||||
		}
 | 
			
		||||
		// If the peer is reachable, launch the execution
 | 
			
		||||
		url = p.urlFormat((mypeer.Url)+meth, dt)       // Format the URL
 | 
			
		||||
		url = p.urlFormat((mypeer.Url), dt) + path     // Format the URL
 | 
			
		||||
		tmp := mypeer.FailedExecution                  // Get the failed executions list
 | 
			
		||||
		mypeer.FailedExecution = []PeerExecution{}     // Reset the failed executions list
 | 
			
		||||
		NewShallowAccessor().UpdateOne(mypeer, peerID) // Update the peer in the db
 | 
			
		||||
 
 | 
			
		||||
@@ -31,6 +31,43 @@ func (d *Workflow) GetAccessor(request *tools.APIRequest) utils.Accessor {
 | 
			
		||||
	return NewAccessor(request) // Create a new instance of the accessor
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Deps struct {
 | 
			
		||||
	Source string
 | 
			
		||||
	Dest   string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (w *Workflow) isDependancy(id string) []Deps {
 | 
			
		||||
	dependancyOfIDs := []Deps{}
 | 
			
		||||
	for _, link := range w.Graph.Links {
 | 
			
		||||
		if _, ok := w.Graph.Items[link.Destination.ID]; !ok {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		source := w.Graph.Items[link.Destination.ID].Processing
 | 
			
		||||
		if id == link.Source.ID && source != nil {
 | 
			
		||||
			dependancyOfIDs = append(dependancyOfIDs, Deps{Source: source.GetName(), Dest: link.Destination.ID})
 | 
			
		||||
		}
 | 
			
		||||
		sourceWF := w.Graph.Items[link.Destination.ID].Workflow
 | 
			
		||||
		if id == link.Source.ID && sourceWF != nil {
 | 
			
		||||
			dependancyOfIDs = append(dependancyOfIDs, Deps{Source: sourceWF.GetName(), Dest: link.Destination.ID})
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return dependancyOfIDs
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (w *Workflow) GetDependencies(id string) (dependencies []Deps) {
 | 
			
		||||
	for _, link := range w.Graph.Links {
 | 
			
		||||
		if _, ok := w.Graph.Items[link.Source.ID]; !ok {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		source := w.Graph.Items[link.Source.ID].Processing
 | 
			
		||||
		if id == link.Destination.ID && source != nil {
 | 
			
		||||
			dependencies = append(dependencies, Deps{Source: source.GetName(), Dest: link.Source.ID})
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (w *Workflow) GetGraphItems(f func(item graph.GraphItem) bool) (list_datas []graph.GraphItem) {
 | 
			
		||||
	for _, item := range w.Graph.Items {
 | 
			
		||||
		if f(item) {
 | 
			
		||||
 
 | 
			
		||||
@@ -112,6 +112,8 @@ func (d *WorkflowExecution) VerifyAuth(request *tools.APIRequest) bool {
 | 
			
		||||
func (d *WorkflowExecution) Book(executionsID string, wfID string, priceds map[tools.DataType]map[string]pricing.PricedItemITF) []*booking.Booking {
 | 
			
		||||
	booking := d.bookEach(executionsID, wfID, tools.STORAGE_RESOURCE, priceds[tools.STORAGE_RESOURCE])
 | 
			
		||||
	booking = append(booking, d.bookEach(executionsID, wfID, tools.PROCESSING_RESOURCE, priceds[tools.PROCESSING_RESOURCE])...)
 | 
			
		||||
	booking = append(booking,d.bookEach(executionsID, wfID, tools.COMPUTE_RESOURCE, priceds[tools.COMPUTE_RESOURCE])...)
 | 
			
		||||
	booking = append(booking,d.bookEach(executionsID, wfID, tools.DATA_RESOURCE, priceds[tools.DATA_RESOURCE])...)
 | 
			
		||||
	return booking
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"cloud.o-forge.io/core/oc-lib/models/booking"
 | 
			
		||||
@@ -77,22 +78,60 @@ func (ws *WorkflowSchedule) CheckBooking(wfID string, request *tools.APIRequest)
 | 
			
		||||
	bookings := []*booking.Booking{}
 | 
			
		||||
	for _, exec := range execs {
 | 
			
		||||
		bookings = append(bookings, exec.Book(ws.UUID, wfID, priceds)...)
 | 
			
		||||
		for _, b := range bookings {
 | 
			
		||||
			meth := request.Caller.URLS[tools.BOOKING][tools.GET]
 | 
			
		||||
			meth = strings.ReplaceAll(meth, ":id", b.ResourceID)
 | 
			
		||||
			meth = strings.ReplaceAll(meth, ":start_date", b.ExpectedStartDate.Format("2006-01-02T15:04:05"))
 | 
			
		||||
			meth = strings.ReplaceAll(meth, ":end_date", b.ExpectedEndDate.Format("2006-01-02T15:04:05"))
 | 
			
		||||
			request.Caller.URLS[tools.BOOKING][tools.GET] = meth
 | 
			
		||||
			_, err := (&peer.Peer{}).LaunchPeerExecution(b.DestPeerID, b.ResourceID, tools.BOOKING, tools.GET, nil, request.Caller)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return false, wf, execs, bookings, err
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	errCh := make(chan error, len(bookings))
 | 
			
		||||
	var m sync.Mutex
 | 
			
		||||
 | 
			
		||||
	for _, b := range bookings {
 | 
			
		||||
		go getBooking(b, request, wf, execs, bookings, errCh, &m)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for i := 0; i < len(bookings); i++ {
 | 
			
		||||
		if err := <-errCh; err != nil {
 | 
			
		||||
			return false, wf, execs, bookings, err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return true, wf, execs, bookings, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getBooking( b *booking.Booking, request *tools.APIRequest, wf *workflow.Workflow, execs []*WorkflowExecution, bookings []*booking.Booking, errCh chan error, m *sync.Mutex) {
 | 
			
		||||
 | 
			
		||||
	m.Lock()
 | 
			
		||||
	c, err := getCallerCopy(request, errCh)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		errCh <- err
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	m.Unlock()
 | 
			
		||||
 | 
			
		||||
	meth := c.URLS[tools.BOOKING][tools.GET]
 | 
			
		||||
	meth = strings.ReplaceAll(meth, ":id", b.ResourceID)
 | 
			
		||||
	meth = strings.ReplaceAll(meth, ":start_date", b.ExpectedStartDate.Format("2006-01-02T15:04:05"))
 | 
			
		||||
	meth = strings.ReplaceAll(meth, ":end_date", b.ExpectedEndDate.Format("2006-01-02T15:04:05"))
 | 
			
		||||
	c.URLS[tools.BOOKING][tools.GET] = meth
 | 
			
		||||
	_, err = (&peer.Peer{}).LaunchPeerExecution(b.DestPeerID, b.ResourceID, tools.BOOKING, tools.GET, nil, &c)
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		errCh <- err
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	errCh <- nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getCallerCopy(request *tools.APIRequest, errCh chan error) (tools.HTTPCaller, error) {
 | 
			
		||||
	var c tools.HTTPCaller
 | 
			
		||||
	err := request.Caller.DeepCopy(c)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		errCh <- err
 | 
			
		||||
		return tools.HTTPCaller{}, nil
 | 
			
		||||
	}
 | 
			
		||||
	c.URLS = request.Caller.URLS
 | 
			
		||||
	return c, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (ws *WorkflowSchedule) Schedules(wfID string, request *tools.APIRequest) (*WorkflowSchedule, *workflow.Workflow, []*WorkflowExecution, error) {
 | 
			
		||||
	if request == nil {
 | 
			
		||||
		return ws, nil, []*WorkflowExecution{}, errors.New("no request found")
 | 
			
		||||
@@ -111,13 +150,20 @@ func (ws *WorkflowSchedule) Schedules(wfID string, request *tools.APIRequest) (*
 | 
			
		||||
		return ws, nil, executions, errors.New("could not book the workflow : " + fmt.Sprintf("%v", err))
 | 
			
		||||
	}
 | 
			
		||||
	ws.Workflow = wf
 | 
			
		||||
 | 
			
		||||
	var errCh = make(chan error, len(bookings))
 | 
			
		||||
	var m sync.Mutex
 | 
			
		||||
 | 
			
		||||
	for _, booking := range bookings {
 | 
			
		||||
		_, err := (&peer.Peer{}).LaunchPeerExecution(booking.DestPeerID, "",
 | 
			
		||||
			tools.BOOKING, tools.POST, booking.Serialize(booking), request.Caller)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
		go ws.BookExecs(booking, request, errCh, &m)
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	for i := 0; i < len(bookings); i++ {
 | 
			
		||||
		if err := <- errCh ; err != nil {
 | 
			
		||||
			return ws, wf, executions, errors.New("could not launch the peer execution : " + fmt.Sprintf("%v", err))
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println("Schedules")
 | 
			
		||||
	for _, exec := range executions {
 | 
			
		||||
		err := exec.PurgeDraft(request)
 | 
			
		||||
@@ -131,6 +177,27 @@ func (ws *WorkflowSchedule) Schedules(wfID string, request *tools.APIRequest) (*
 | 
			
		||||
	return ws, wf, executions, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (ws *WorkflowSchedule) BookExecs(booking *booking.Booking, request *tools.APIRequest, errCh chan error, m *sync.Mutex) {
 | 
			
		||||
 | 
			
		||||
	m.Lock()
 | 
			
		||||
	c, err := getCallerCopy(request, errCh)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		errCh <- err
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	m.Unlock()
 | 
			
		||||
 | 
			
		||||
	_, err = (&peer.Peer{}).LaunchPeerExecution(booking.DestPeerID, "",
 | 
			
		||||
		tools.BOOKING, tools.POST, booking.Serialize(booking), &c)
 | 
			
		||||
	
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		errCh <- err
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	errCh <- nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
BOOKING IMPLIED TIME, not of subscription but of execution
 | 
			
		||||
so is processing time execution time applied on computes
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,7 @@ import (
 | 
			
		||||
 | 
			
		||||
	"cloud.o-forge.io/core/oc-lib/config"
 | 
			
		||||
	"cloud.o-forge.io/core/oc-lib/dbs/mongo"
 | 
			
		||||
	"cloud.o-forge.io/core/oc-lib/logs"
 | 
			
		||||
	beego "github.com/beego/beego/v2/server/web"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -135,6 +136,7 @@ func (a *API) CheckRemotePeer(url string) (State, map[string]int) {
 | 
			
		||||
// CheckRemoteAPIs checks the state of remote APIs from your proper OC
 | 
			
		||||
func (a *API) CheckRemoteAPIs(apis []DataType) (State, map[string]string, error) {
 | 
			
		||||
	// Check if the database is up
 | 
			
		||||
	l := logs.GetLogger()
 | 
			
		||||
	new := map[string]string{}
 | 
			
		||||
	caller := NewHTTPCaller(map[DataType]map[METHOD]string{}) // Create a new http caller
 | 
			
		||||
	code := 0
 | 
			
		||||
@@ -145,6 +147,7 @@ func (a *API) CheckRemoteAPIs(apis []DataType) (State, map[string]string, error)
 | 
			
		||||
		var resp APIStatusResponse
 | 
			
		||||
		b, err := caller.CallGet("http://"+api.API()+":8080", "/oc/version/status") // Call the status endpoint of the remote API (standard OC status endpoint)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			l.Error().Msg(api.String() + " not reachable")
 | 
			
		||||
			state = REDUCED_SERVICE // If a remote API is not reachable, return reduced service
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
@@ -161,6 +164,7 @@ func (a *API) CheckRemoteAPIs(apis []DataType) (State, map[string]string, error)
 | 
			
		||||
		reachable = true // If the remote API is reachable, set reachable to true cause we are not dead
 | 
			
		||||
	}
 | 
			
		||||
	if !reachable {
 | 
			
		||||
		l.Error().Msg("Peer check returned no answers")
 | 
			
		||||
		state = DEAD // If no remote API is reachable, return dead, nobody is alive
 | 
			
		||||
	}
 | 
			
		||||
	if code > 0 {
 | 
			
		||||
 
 | 
			
		||||
@@ -39,7 +39,7 @@ var ADMIRALTY_SOURCEAPI = DATACENTERAPI+"/admiralty/source"
 | 
			
		||||
var ADMIRALTY_TARGETAPI = DATACENTERAPI+"/admiralty/target"
 | 
			
		||||
var ADMIRALTY_SECRETAPI = DATACENTERAPI+"/admiralty/secret"
 | 
			
		||||
var ADMIRALTY_KUBECONFIGAPI = DATACENTERAPI+"/admiralty/kubeconfig"
 | 
			
		||||
var ADMIRALTY_NODESAPI = DATACENTERAPI+"/admiralty/nodes"
 | 
			
		||||
var ADMIRALTY_NODESAPI = DATACENTERAPI+"/admiralty/node"
 | 
			
		||||
 | 
			
		||||
// Bind the standard API name to the data type
 | 
			
		||||
var DefaultAPI = [...]string{
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ package tools
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/url"
 | 
			
		||||
@@ -49,8 +50,9 @@ func ToMethod(str string) METHOD {
 | 
			
		||||
var HTTPCallerInstance = &HTTPCaller{} // Singleton instance of the HTTPCaller
 | 
			
		||||
 | 
			
		||||
type HTTPCaller struct {
 | 
			
		||||
	URLS     map[DataType]map[METHOD]string // Map of the different methods and their urls
 | 
			
		||||
	Disabled bool                           // Disabled flag
 | 
			
		||||
	URLS     	map[DataType]map[METHOD]string 	// Map of the different methods and their urls
 | 
			
		||||
	Disabled 	bool                           	// Disabled flag
 | 
			
		||||
	LastResults map[string]interface{}			// Used to store information regarding the last execution of a given method on a given data type
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewHTTPCaller creates a new instance of the HTTP Caller
 | 
			
		||||
@@ -61,6 +63,16 @@ func NewHTTPCaller(urls map[DataType]map[METHOD]string) *HTTPCaller {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Creates a copy of the current caller, in order to have parallelized executions without race condition 
 | 
			
		||||
func (c* HTTPCaller) DeepCopy(dst HTTPCaller) error {
 | 
			
		||||
	bytes, err := json.Marshal(c)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return json.Unmarshal(bytes, &dst)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CallGet calls the GET method on the HTTP server
 | 
			
		||||
func (caller *HTTPCaller) CallGet(url string, subpath string, types ...string) ([]byte, error) {
 | 
			
		||||
	req, err := http.NewRequest(http.MethodGet, url+subpath, bytes.NewBuffer([]byte("")))
 | 
			
		||||
@@ -76,17 +88,33 @@ func (caller *HTTPCaller) CallGet(url string, subpath string, types ...string) (
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
	return io.ReadAll(resp.Body)
 | 
			
		||||
	err = caller.StoreResp(resp)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return caller.LastResults["body"].([]byte), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CallPut calls the DELETE method on the HTTP server
 | 
			
		||||
func (caller *HTTPCaller) CallDelete(url string, subpath string) ([]byte, error) {
 | 
			
		||||
	resp, err := http.NewRequest("DELETE", url+subpath, nil)
 | 
			
		||||
	if err != nil || resp == nil || resp.Body == nil {
 | 
			
		||||
	req, err := http.NewRequest("DELETE", url+subpath, nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	client := &http.Client{}
 | 
			
		||||
	resp, err := client.Do(req)
 | 
			
		||||
	if err != nil || req == nil || req.Body == nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
	return io.ReadAll(resp.Body)
 | 
			
		||||
 | 
			
		||||
	err = caller.StoreResp(resp)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return caller.LastResults["body"].([]byte), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CallPost calls the POST method on the HTTP server
 | 
			
		||||
@@ -105,7 +133,12 @@ func (caller *HTTPCaller) CallPost(url string, subpath string, body interface{},
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
	return io.ReadAll(resp.Body)
 | 
			
		||||
	err = caller.StoreResp(resp)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return caller.LastResults["body"].([]byte), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CallPost calls the POST method on the HTTP server
 | 
			
		||||
@@ -123,7 +156,12 @@ func (caller *HTTPCaller) CallPut(url string, subpath string, body map[string]in
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
	return io.ReadAll(resp.Body)
 | 
			
		||||
	err = caller.StoreResp(resp)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return caller.LastResults["body"].([]byte), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CallRaw calls the Raw method on the HTTP server
 | 
			
		||||
@@ -143,7 +181,12 @@ func (caller *HTTPCaller) CallRaw(method string, url string, subpath string,
 | 
			
		||||
		req.AddCookie(c)
 | 
			
		||||
	}
 | 
			
		||||
	client := &http.Client{}
 | 
			
		||||
	return client.Do(req)
 | 
			
		||||
	resp, err := client.Do(req)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return resp, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CallRaw calls the Raw method on the HTTP server
 | 
			
		||||
@@ -163,3 +206,17 @@ func (caller *HTTPCaller) CallForm(method string, url string, subpath string,
 | 
			
		||||
	client := &http.Client{}
 | 
			
		||||
	return client.Do(req)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (caller *HTTPCaller) StoreResp(resp *http.Response) error {
 | 
			
		||||
	caller.LastResults = make(map[string]interface{})
 | 
			
		||||
	caller.LastResults["header"] = resp.Header
 | 
			
		||||
	caller.LastResults["code"] = resp.StatusCode
 | 
			
		||||
	data, err := io.ReadAll(resp.Body)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("Error reading the body of the last request")
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	caller.LastResults["body"] = data
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user