github.com/prebid/prebid-server/v2@v2.18.0/endpoints/currency_rates.go (about) 1 package endpoints 2 3 import ( 4 "net/http" 5 "time" 6 7 "github.com/golang/glog" 8 "github.com/prebid/prebid-server/v2/currency" 9 "github.com/prebid/prebid-server/v2/util/jsonutil" 10 ) 11 12 // currencyRatesInfo holds currency rates information. 13 type currencyRatesInfo struct { 14 Active bool `json:"active"` 15 Source *string `json:"source,omitempty"` 16 FetchingInterval *time.Duration `json:"fetchingIntervalNs,omitempty"` 17 LastUpdated *time.Time `json:"lastUpdated,omitempty"` 18 Rates *map[string]map[string]float64 `json:"rates,omitempty"` 19 AdditionalInfo interface{} `json:"additionalInfo,omitempty"` 20 } 21 22 type rateConverter interface { 23 GetInfo() currency.ConverterInfo 24 } 25 26 // newCurrencyRatesInfo creates a new CurrencyRatesInfo instance. 27 func newCurrencyRatesInfo(rateConverter rateConverter, fetchingInterval time.Duration) currencyRatesInfo { 28 29 currencyRatesInfo := currencyRatesInfo{ 30 Active: false, 31 } 32 33 if rateConverter == nil { 34 return currencyRatesInfo 35 } 36 37 currencyRatesInfo.Active = true 38 39 infos := rateConverter.GetInfo() 40 if infos == nil { 41 return currencyRatesInfo 42 } 43 44 source := infos.Source() 45 currencyRatesInfo.Source = &source 46 47 currencyRatesInfo.FetchingInterval = &fetchingInterval 48 49 lastUpdated := infos.LastUpdated() 50 currencyRatesInfo.LastUpdated = &lastUpdated 51 52 currencyRatesInfo.Rates = infos.Rates() 53 currencyRatesInfo.AdditionalInfo = infos.AdditionalInfo() 54 55 return currencyRatesInfo 56 } 57 58 // NewCurrencyRatesEndpoint returns current currency rates applied by the PBS server. 59 func NewCurrencyRatesEndpoint(rateConverter rateConverter, fetchingInterval time.Duration) http.HandlerFunc { 60 currencyRateInfo := newCurrencyRatesInfo(rateConverter, fetchingInterval) 61 62 return func(w http.ResponseWriter, _ *http.Request) { 63 jsonOutput, err := jsonutil.Marshal(currencyRateInfo) 64 if err != nil { 65 glog.Errorf("/currency/rates Critical error when trying to marshal currencyRateInfo: %v", err) 66 w.WriteHeader(http.StatusInternalServerError) 67 return 68 } 69 70 w.Header().Set("Content-Type", "application/json") 71 w.Write(jsonOutput) 72 } 73 }