// GetAuditEntriesLive returns the live audit notifications, see `GetAuditEntries` too.
GetAuditEntriesLive returns the live audit notifications, see `GetAuditEntries` too.
// GetAuditEntriesLive returns the live audit notifications see ` GetAuditEntries ` too .
func (c *Client) GetAuditEntriesLive(handler AuditEntryHandler) error {
	if handler == nil {
		return errRequired("handler")
	}
	resp, err := c.Do(http.MethodGet, auditPathSSE, contentTypeJSON, nil, func(r *http.Request) error {
		r.Header.Add(acceptHeaderKey, "application/json, text/event-stream")
		return nil
	})
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	reader, err := c.acquireResponseBodyStream(resp)
	if err != nil {
		return err
	}
	streamReader := bufio.NewReader(reader)
	for {
		line, err := streamReader.ReadBytes('\n')
		if err != nil {
			if err == io.EOF {
				return nil 
			}
			return err 
		}
		if len(line) < shiftN+1 { 
			continue
		}
		if !bytes.HasPrefix(line, dataPrefix) {
			return fmt.Errorf("client: see: fail to read the event, the incoming message has no [%s] prefix", string(dataPrefix))
		}
		message := line[shiftN:] 
		if len(message) < 2 {
			continue 
		}
		entry := AuditEntry{}
		if err = json.Unmarshal(message, &entry); err != nil {
			return err
		}
		if err = handler(entry); err != nil {
			return err 
		}
	}
}
func ( c * Client ) GetAuditEntriesLive ( handler AuditEntryHandler ) error { if handler == nil { return errRequired ( " " ) } resp , err := c . Do ( http . MethodGet , auditPathSSE , contentTypeJSON , nil , func ( r * http . Request ) error { r . Header . Add ( acceptHeaderKey , " " ) return nil } ) if err != nil { return err } defer resp . Body . Close ( ) reader , err := c . acquireResponseBodyStream ( resp ) if err != nil { return err } streamReader := bufio . NewReader ( reader ) for { line , err := streamReader . ReadBytes ( '\n' ) if err != nil { if err == io . EOF { return nil } return err } if len ( line ) < shiftN + 1 { continue } if ! bytes . HasPrefix ( line , dataPrefix ) { return fmt . Errorf ( " " , string ( dataPrefix ) ) } message := line [ shiftN : ] if len ( message ) < 2 { continue } entry := AuditEntry { } if err = json . Unmarshal ( message , & entry ) ; err != nil { return err } if err = handler ( entry ) ; err != nil { return err } } }
GetAuditEntriesLive returns the live audit notifications, see `GetAuditEntries` too.
GetAuditEntriesLive returns the live audit notifications see ` GetAuditEntries ` too .
// we read until the the end, exit with no error here. // exit on first failure. // even more +1 for the actual event. // almost empty or totally invalid line, // empty message maybe, // we don't care, we ignore them at any way. // we need everything after the 'data:'. // do NOT stop here, let the connection active. // exit on first error here as well. // stop on first error by the caller.