forked from wgroeneveld/go-jamming
pingback sending, beginning of webmention sending
This commit is contained in:
parent
257666439a
commit
7f2c540448
|
@ -1,9 +1,50 @@
|
||||||
package send
|
package send
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/wgroeneveld/go-jamming/app/mf"
|
"github.com/wgroeneveld/go-jamming/app/mf"
|
||||||
|
"github.com/wgroeneveld/go-jamming/rest"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SendPingbackToEndpoint(endpoint string, mention mf.Mention) {
|
type xml string
|
||||||
// do stuff
|
|
||||||
|
var body xml = `<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<methodCall>
|
||||||
|
<methodName>pingback.ping</methodName>
|
||||||
|
<params>
|
||||||
|
<param>
|
||||||
|
<value><string>{$source}</string></value>
|
||||||
|
</param>
|
||||||
|
<param>
|
||||||
|
<value><string>{$target}</string></value>
|
||||||
|
</param>
|
||||||
|
</params>
|
||||||
|
</methodCall>`
|
||||||
|
|
||||||
|
func (theXml xml) replace(key string, value string) xml {
|
||||||
|
return xml(strings.ReplaceAll(theXml.String(), key, value))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (theXml xml) String() string {
|
||||||
|
return string(theXml)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (theXml xml) fill(mention mf.Mention) string {
|
||||||
|
return theXml.
|
||||||
|
replace("{$source}", mention.Source).
|
||||||
|
replace("{$target}", mention.Target).
|
||||||
|
String()
|
||||||
|
}
|
||||||
|
|
||||||
|
type Sender struct {
|
||||||
|
RestClient rest.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sender *Sender) SendPingbackToEndpoint(endpoint string, mention mf.Mention) {
|
||||||
|
err := sender.RestClient.Post(endpoint, "text/xml", body.fill(mention))
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Str("wm", mention.String()).Msg("Unable to send pingback")
|
||||||
|
}
|
||||||
|
log.Info().Str("wm", mention.String()).Msg("Pingback sent")
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
package send
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/wgroeneveld/go-jamming/app/mf"
|
||||||
|
"github.com/wgroeneveld/go-jamming/mocks"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSendPingbackToEndpoint(t *testing.T) {
|
||||||
|
var capturedBody string
|
||||||
|
sender := Sender{
|
||||||
|
RestClient: &mocks.RestClientMock{
|
||||||
|
PostFunc: func(url string, contentType string, body string) error {
|
||||||
|
capturedBody = body
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
expectedXml := `<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<methodCall>
|
||||||
|
<methodName>pingback.ping</methodName>
|
||||||
|
<params>
|
||||||
|
<param>
|
||||||
|
<value><string>src</string></value>
|
||||||
|
</param>
|
||||||
|
<param>
|
||||||
|
<value><string>target</string></value>
|
||||||
|
</param>
|
||||||
|
</params>
|
||||||
|
</methodCall>`
|
||||||
|
|
||||||
|
sender.SendPingbackToEndpoint("http://dingdong.com/pingback", mf.Mention{
|
||||||
|
Source: "src",
|
||||||
|
Target: "target",
|
||||||
|
})
|
||||||
|
assert.Equal(t, expectedXml, capturedBody)
|
||||||
|
}
|
|
@ -3,8 +3,18 @@ package send
|
||||||
import (
|
import (
|
||||||
"github.com/wgroeneveld/go-jamming/app/mf"
|
"github.com/wgroeneveld/go-jamming/app/mf"
|
||||||
"github.com/wgroeneveld/go-jamming/app/pingback/send"
|
"github.com/wgroeneveld/go-jamming/app/pingback/send"
|
||||||
|
"github.com/wgroeneveld/go-jamming/common"
|
||||||
|
"github.com/wgroeneveld/go-jamming/rest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func mention() {
|
type Sender struct {
|
||||||
send.SendPingbackToEndpoint("endpoint", mf.Mention{})
|
RestClient rest.Client
|
||||||
|
Conf *common.Config
|
||||||
|
}
|
||||||
|
|
||||||
|
func mention() {
|
||||||
|
pingbackSender := &send.Sender{
|
||||||
|
RestClient: nil,
|
||||||
|
}
|
||||||
|
pingbackSender.SendPingbackToEndpoint("endpoint", mf.Mention{})
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
type RestClientMock struct {
|
type RestClientMock struct {
|
||||||
GetFunc func(string) (*http.Response, error)
|
GetFunc func(string) (*http.Response, error)
|
||||||
GetBodyFunc func(string) (string, error)
|
GetBodyFunc func(string) (string, error)
|
||||||
|
PostFunc func(string, string, string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// although these are still requied to match the rest.Client interface.
|
// although these are still requied to match the rest.Client interface.
|
||||||
|
@ -22,6 +23,10 @@ func (m *RestClientMock) GetBody(url string) (string, error) {
|
||||||
return m.GetBodyFunc(url)
|
return m.GetBodyFunc(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *RestClientMock) Post(url string, contentType string, body string) error {
|
||||||
|
return m.PostFunc(url, contentType, body)
|
||||||
|
}
|
||||||
|
|
||||||
func RelPathGetBodyFunc(t *testing.T, relPath string) func(string) (string, error) {
|
func RelPathGetBodyFunc(t *testing.T, relPath string) func(string) (string, error) {
|
||||||
return func(url string) (string, error) {
|
return func(url string) (string, error) {
|
||||||
// url: https://brainbaking.com/something-something.html
|
// url: https://brainbaking.com/something-something.html
|
||||||
|
|
|
@ -3,18 +3,28 @@ package rest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"io/ioutil"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client interface {
|
type Client interface {
|
||||||
Get(url string) (*http.Response, error)
|
Get(url string) (*http.Response, error)
|
||||||
|
Post(url string, contentType string, body string) error
|
||||||
GetBody(url string) (string, error)
|
GetBody(url string) (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type HttpClient struct {
|
type HttpClient struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (client *HttpClient) Post(url string, contenType string, body string) error {
|
||||||
|
_, err := http.Post(url, contenType, strings.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// something like this? https://freshman.tech/snippets/go/http-response-to-string/
|
// something like this? https://freshman.tech/snippets/go/http-response-to-string/
|
||||||
func (client *HttpClient) GetBody(url string) (string, error) {
|
func (client *HttpClient) GetBody(url string) (string, error) {
|
||||||
resp, geterr := http.Get(url)
|
resp, geterr := http.Get(url)
|
||||||
|
|
Loading…
Reference in New Issue