Skip to content

Generic way to load a service #940

@mcarbonne

Description

@mcarbonne

I want to migrate one of my application from shoutrrr to notify but I faced an issue: there is no way to load a service in a generic way.

With shoutrrr, a unique CreateSender function can be used to load any service:

  url := "slack://token-a/token-b/token-c"
  sender, err := shoutrrr.CreateSender(url)
  sender.Send("Hello world (or slack channel) !", map[string]string { /* ... */ })

I haven't found any generic way to do something similar with notify.

I had a look at the code and they are few possibilities to implement this.
An easy approach would be to create a function looking like this:

package notify

import "github.com/nikoksr/notify/service/slack"

func NewServiceFromURL(url string) (Notifier, error) {
        serviceName, parameters := parseURL(url)
	if serviceName == "slack" {
		return slack.New(...), nil
	} else if (...) {

	} else {
		return nil, fmt.Errorf("Service %v does not exists")
	}
}

Another approach with a map to store the associations would also be possible.

I wanted to open an issue first before submitting any PR to discuss about what can be implemented and what shall be avoided.
The solution above do avoid any breaking change. More elegant solutions might be possible but for now I only had ideas which are not backward-compatible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions