github.com/prebid/prebid-server/v2@v2.18.0/privacy/gdpr/consentwriter.go (about)

     1  package gdpr
     2  
     3  import (
     4  	"github.com/prebid/openrtb/v20/openrtb2"
     5  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     6  )
     7  
     8  // ConsentWriter implements the PolicyWriter interface for GDPR TCF.
     9  type ConsentWriter struct {
    10  	Consent    string
    11  	RegExtGDPR *int8
    12  }
    13  
    14  // Write mutates an OpenRTB bid request with the GDPR TCF consent.
    15  func (c ConsentWriter) Write(req *openrtb2.BidRequest) error {
    16  	if req == nil {
    17  		return nil
    18  	}
    19  	reqWrap := &openrtb_ext.RequestWrapper{BidRequest: req}
    20  
    21  	if c.RegExtGDPR != nil {
    22  		if regsExt, err := reqWrap.GetRegExt(); err == nil {
    23  			regsExt.SetGDPR(c.RegExtGDPR)
    24  		} else {
    25  			return err
    26  		}
    27  	}
    28  
    29  	if c.Consent != "" {
    30  		if userExt, err := reqWrap.GetUserExt(); err == nil {
    31  			userExt.SetConsent(&c.Consent)
    32  		} else {
    33  			return err
    34  		}
    35  	}
    36  
    37  	if err := reqWrap.RebuildRequest(); err != nil {
    38  		return err
    39  	}
    40  
    41  	return nil
    42  }