github.com/Financial-Times/publish-availability-monitor@v1.12.0/feeds/feedFactory.go (about)

     1  package feeds
     2  
     3  import (
     4  	"net/url"
     5  	"strings"
     6  	"sync"
     7  	"time"
     8  
     9  	"github.com/Financial-Times/go-logger/v2"
    10  )
    11  
    12  func NewNotificationsFeed(name string, baseURL url.URL, expiry, interval int, username, password, apiKey string, log *logger.UPPLogger) Feed {
    13  	if isNotificationsPullFeed(name) {
    14  		return newNotificationsPullFeed(name, baseURL, expiry, interval, username, password, log)
    15  	} else if isNotificationsPushFeed(name) {
    16  		return newNotificationsPushFeed(name, baseURL, expiry, interval, username, password, apiKey, log)
    17  	}
    18  
    19  	return nil
    20  }
    21  
    22  func isNotificationsPullFeed(feedName string) bool {
    23  	return feedName == "notifications" ||
    24  		feedName == "list-notifications" ||
    25  		feedName == "page-notifications"
    26  }
    27  
    28  func isNotificationsPushFeed(feedName string) bool {
    29  	return strings.HasSuffix(feedName, "notifications-push")
    30  }
    31  
    32  func newNotificationsPullFeed(name string, baseURL url.URL, expiry, interval int, username, password string, log *logger.UPPLogger) *NotificationsPullFeed {
    33  	feedURL := baseURL.String()
    34  
    35  	bootstrapValues := baseURL.Query()
    36  	bootstrapValues.Add("since", time.Now().Format(time.RFC3339))
    37  	baseURL.RawQuery = ""
    38  
    39  	log.Infof("constructing NotificationsPullFeed for [%s], baseURL = [%s], bootstrapValues = [%s]", feedURL, baseURL.String(), bootstrapValues.Encode())
    40  	return &NotificationsPullFeed{
    41  		baseNotificationsFeed: baseNotificationsFeed{
    42  			feedName:          name,
    43  			baseURL:           feedURL,
    44  			username:          username,
    45  			password:          password,
    46  			expiry:            expiry + 2*interval,
    47  			notifications:     make(map[string][]*Notification),
    48  			notificationsLock: &sync.RWMutex{},
    49  		},
    50  		notificationsURL:         baseURL.String(),
    51  		notificationsQueryString: bootstrapValues.Encode(),
    52  		notificationsURLLock:     &sync.Mutex{},
    53  		interval:                 interval,
    54  		log:                      log,
    55  	}
    56  }
    57  
    58  func newNotificationsPushFeed(name string, baseURL url.URL, expiry int, interval int, username string, password string, apiKey string, log *logger.UPPLogger) *NotificationsPushFeed {
    59  	log.Infof("constructing NotificationsPushFeed, bootstrapUrl = [%s]", baseURL.String())
    60  	return &NotificationsPushFeed{
    61  		baseNotificationsFeed: baseNotificationsFeed{
    62  			feedName:          name,
    63  			baseURL:           baseURL.String(),
    64  			username:          username,
    65  			password:          password,
    66  			expiry:            expiry + 2*interval,
    67  			notifications:     make(map[string][]*Notification),
    68  			notificationsLock: &sync.RWMutex{},
    69  		},
    70  		stopFeed:     true,
    71  		stopFeedLock: &sync.RWMutex{},
    72  		apiKey:       apiKey,
    73  		log:          log,
    74  	}
    75  }