Files
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

103 lines
1.9 KiB
Go

package service
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"time"
"github.com/cortezaproject/corteza/server/federation/types"
)
type (
Syncer struct {
client http.Client
}
Url struct {
Url types.SyncerURI
Meta Processer
}
Payload struct {
Payload io.Reader
Meta Processer
}
AuxResponseSet struct {
Response struct {
Filter struct {
NodeID string
ComposeModuleID string
Query string
Limit int
NextPage string
PrevPage string
} `json:"filter"`
} `json:"response"`
}
)
const FederationUserToken string = "authToken"
func (h *Syncer) Queue(url Url, out chan Url) {
out <- url
}
func (h *Syncer) Fetch(ctx context.Context, url string) (io.Reader, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
if authToken := ctx.Value(FederationUserToken); authToken != nil {
req.Header.Add("Authorization", `Bearer `+authToken.(string))
}
req.Header.Add("Accept", "application/json")
resp, err := h.client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("invalid return status: %d", resp.StatusCode))
}
return resp.Body, nil
}
func (h *Syncer) Process(ctx context.Context, payload []byte, out chan Url, url types.SyncerURI, processer Processer) (ProcesserResponse, error) {
aux, err := h.ParseHeader(ctx, payload)
if err != nil {
return 0, err
}
if aux.Response.Filter.NextPage != "" {
url.NextPage = aux.Response.Filter.NextPage
out <- Url{
Url: url,
Meta: processer,
}
}
return processer.Process(ctx, payload)
}
func (h *Syncer) ParseHeader(ctx context.Context, payload []byte) (aux AuxResponseSet, err error) {
err = json.Unmarshal(payload, &aux)
return
}
func NewSyncer() *Syncer {
return &Syncer{
client: http.Client{
Timeout: time.Duration(3) * time.Second,
},
}
}