-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add new http publisher Create a new publisher endpoint, capable of publishing content to an http feed. Closes: #386 Signed-off-by: Yolanda Robla <[email protected]> Signed-off-by: Yolanda Robla <[email protected]> * fix: do not use additional config struct Signed-off-by: Yolanda Robla <[email protected]> --------- Signed-off-by: Yolanda Robla <[email protected]> Signed-off-by: Yolanda Robla <[email protected]>
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package httpclientpubsub | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
const PublisherType = "http-client" | ||
|
||
var ErrHTTPRequestFailed = errors.New("HTTP request failed") | ||
|
||
type Config struct { | ||
URL string `mapstructure:"url"` | ||
} | ||
|
||
type HTTPClientPubSub struct { | ||
url string | ||
} | ||
|
||
func New(ctx context.Context, url string) (*HTTPClientPubSub, error) { | ||
pub := &HTTPClientPubSub{url: url} | ||
return pub, nil | ||
} | ||
|
||
func (pub *HTTPClientPubSub) Name() string { | ||
return PublisherType | ||
} | ||
|
||
func FromConfig(ctx context.Context, config Config) (*HTTPClientPubSub, error) { | ||
return New(ctx, config.URL) | ||
} | ||
|
||
func (pub *HTTPClientPubSub) Send(ctx context.Context, body []byte) error { | ||
log.Info("Sending event to HTTP client publisher") | ||
// Print the url to the log so that we can see where the event is being sent. | ||
req, err := http.NewRequest("POST", pub.url, bytes.NewReader(body)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("%w with status code: %d", ErrHTTPRequestFailed, resp.StatusCode) | ||
} | ||
|
||
return nil | ||
} |