github.com/prebid/prebid-server@v0.275.0/analytics/pubstack/eventchannel/sender.go (about)

     1  package eventchannel
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"github.com/golang/glog"
     7  	"net/http"
     8  	"net/url"
     9  	"path"
    10  )
    11  
    12  type Sender = func(payload []byte) error
    13  
    14  func NewHttpSender(client *http.Client, endpoint string) Sender {
    15  	return func(payload []byte) error {
    16  		req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewReader(payload))
    17  		if err != nil {
    18  			glog.Error(err)
    19  			return err
    20  		}
    21  
    22  		req.Header.Set("Content-Type", "application/octet-stream")
    23  		req.Header.Set("Content-Encoding", "gzip")
    24  
    25  		resp, err := client.Do(req)
    26  		if err != nil {
    27  			return err
    28  		}
    29  
    30  		if resp.StatusCode != http.StatusOK {
    31  			glog.Errorf("[pubstack] Wrong code received %d instead of %d", resp.StatusCode, http.StatusOK)
    32  			return fmt.Errorf("wrong code received %d instead of %d", resp.StatusCode, http.StatusOK)
    33  		}
    34  		return nil
    35  	}
    36  }
    37  
    38  func BuildEndpointSender(client *http.Client, baseUrl string, module string) Sender {
    39  	endpoint, err := url.Parse(baseUrl)
    40  	if err != nil {
    41  		glog.Error(err)
    42  	}
    43  	endpoint.Path = path.Join(endpoint.Path, "intake", module)
    44  	return NewHttpSender(client, endpoint.String())
    45  }