func (w *Webhook) Invoke(fs *firestore.Client, id string, useCache bool) (bool, string, error) {
	var body interface{}
	changed := false

	// Get whatever info the webhook is interested in
	if useCache {
		if w.Field == FieldConfirmed {
			body = &LastConfirmed
		} else { // w.Field == FieldStringency
			body = &LastStringency
		}
	} else {
		if w.Field == FieldConfirmed {
			confirmed, err := corona.GetLatestCases(w.Country)
			if err != nil {
				return false, "", err
			}
			body = &confirmed
			if confirmed != LastConfirmed {
				changed = true
				LastConfirmed = confirmed
			}
		} else { // w.Field == FieldStringency
			stringency, err := corona.GetLatestStringency(w.Country)
			if err != nil {
				return false, "", err
			}
			body = &stringency
			if stringency != LastStringency {
				changed = true
				LastStringency = stringency
			}
		}
	}

	// Create a post request where the body is the data associated with the Webhooks field.
	payload := new(bytes.Buffer)
	_ = json.NewEncoder(payload).Encode(body)
	req, err := http.NewRequest(http.MethodPost, w.URL, payload)
	if err != nil {
		return false, "", err
	}

	// Send request
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return false, "", err
	}
	res.Body.Close()

	if !corona.StatusIs2XX(res.StatusCode) {
		return false, "", &corona.ServerError{Err: "Remote responded with non 2XX code", StatusCode: res.StatusCode}
	}

	// Update the LastTriggered field of the webhook to now
	_, err = fs.
		Collection(WebhookCollection).
		Doc(id).
		Update(context.Background(), []firestore.Update{{Path: "LastTriggered", Value: time.Now()}})
	if err != nil {
		return false, "", err
	}

	return changed, w.Field, nil
}
// Invoke a webhook by figuring out what it's looking for and getting it.
// Returns if the invocation resulted in new data.
Invoke a webhook by figuring out what it's looking for and getting it.
Returns if the invocation resulted in new data.
// Invoke a webhook by figuring out what it ' s looking for and getting it . // Returns if the invocation resulted in new data .
func (w *Webhook) Invoke(fs *firestore.Client, id string, useCache bool) (bool, string, error) {
	var body interface{}
	changed := false
	if useCache {
		if w.Field == FieldConfirmed {
			body = &LastConfirmed
		} else { 
			body = &LastStringency
		}
	} else {
		if w.Field == FieldConfirmed {
			confirmed, err := corona.GetLatestCases(w.Country)
			if err != nil {
				return false, "", err
			}
			body = &confirmed
			if confirmed != LastConfirmed {
				changed = true
				LastConfirmed = confirmed
			}
		} else { 
			stringency, err := corona.GetLatestStringency(w.Country)
			if err != nil {
				return false, "", err
			}
			body = &stringency
			if stringency != LastStringency {
				changed = true
				LastStringency = stringency
			}
		}
	}
	payload := new(bytes.Buffer)
	_ = json.NewEncoder(payload).Encode(body)
	req, err := http.NewRequest(http.MethodPost, w.URL, payload)
	if err != nil {
		return false, "", err
	}
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return false, "", err
	}
	res.Body.Close()
	if !corona.StatusIs2XX(res.StatusCode) {
		return false, "", &corona.ServerError{Err: "Remote responded with non 2XX code", StatusCode: res.StatusCode}
	}
	_, err = fs.
		Collection(WebhookCollection).
		Doc(id).
		Update(context.Background(), []firestore.Update{{Path: "LastTriggered", Value: time.Now()}})
	if err != nil {
		return false, "", err
	}
	return changed, w.Field, nil
}
func ( w * Webhook ) Invoke ( fs * firestore . Client , id string , useCache bool ) ( bool , string , error ) { var body interface { } changed := false if useCache { if w . Field == FieldConfirmed { body = & LastConfirmed } else { body = & LastStringency } } else { if w . Field == FieldConfirmed { confirmed , err := corona . GetLatestCases ( w . Country ) if err != nil { return false , " " , err } body = & confirmed if confirmed != LastConfirmed { changed = true LastConfirmed = confirmed } } else { stringency , err := corona . GetLatestStringency ( w . Country ) if err != nil { return false , " " , err } body = & stringency if stringency != LastStringency { changed = true LastStringency = stringency } } } payload := new ( bytes . Buffer ) _ = json . NewEncoder ( payload ) . Encode ( body ) req , err := http . NewRequest ( http . MethodPost , w . URL , payload ) if err != nil { return false , " " , err } res , err := http . DefaultClient . Do ( req ) if err != nil { return false , " " , err } res . Body . Close ( ) if ! corona . StatusIs2XX ( res . StatusCode ) { return false , " " , & corona . ServerError { Err : " " , StatusCode : res . StatusCode } } _ , err = fs . Collection ( WebhookCollection ) . Doc ( id ) . Update ( context . Background ( ) , [ ] firestore . Update { { Path : " " , Value : time . Now ( ) } } ) if err != nil { return false , " " , err } return changed , w . Field , nil }
Invoke a webhook by figuring out what it's looking for and getting it.
Invoke a webhook by figuring out what it ' s looking for and getting it .
// Get whatever info the webhook is interested in // w.Field == FieldStringency // w.Field == FieldStringency // Create a post request where the body is the data associated with the Webhooks field. // Send request // Update the LastTriggered field of the webhook to now