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

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