Added a basic http client which would understand AP things

This would represent the base for a future IRI dereference mechanism
This commit is contained in:
Marius Orcsik 2018-12-26 15:46:57 +01:00
parent 9e3d0a68a1
commit aa810e474c
No known key found for this signature in database
GPG key ID: 889CE8E4FB2D877A
2 changed files with 228 additions and 0 deletions

157
client/client.go Normal file
View file

@ -0,0 +1,157 @@
package client
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
as "github.com/mariusor/activitypub.go/activitystreams"
)
type signFn func(*http.Request) error
type logFn func(...interface{})
// UserAgent value that the client uses when performing requests
var UserAgent = "activitypub-go-http-client"
// ErrorLogger
var ErrorLogger logFn = func(el ...interface{}) {}
// InfoLogger
var InfoLogger logFn = func(el ...interface{}) {}
// Sign is the default function to use when signing requests
// Usually this is done using HTTP-Signatures
// See https://github.com/spacemonkeygo/httpsig
// var key *rsa.PrivateKey = ...
// signer := httpsig.NewSigner("foo", key, httpsig.RSASHA256, nil)
// client.Sign = signer.Sign
var Sign signFn = func(r *http.Request) error { return nil }
type err struct {
msg string
iri as.IRI
}
func errorf(i as.IRI, msg string, p ...interface{}) error {
return &err{
msg: fmt.Sprintf(msg, p...),
iri: i,
}
}
// Error returns the formatted error
func (e *err) Error() string {
if len(e.iri) > 0 {
return fmt.Sprintf("%s\nwhen loading: %s", e.msg, e.iri)
} else {
return fmt.Sprintf("%s", e.msg)
}
}
type client struct{}
func NewClient() client {
return client{}
}
// LoadIRI tries to dereference an IRI and load the full ActivityPub object it represents
func (c *client) LoadIRI(id as.IRI) (as.Item, error) {
if len(id) == 0 {
return nil, errorf(id, "Invalid IRI, nil value")
}
if _, err := url.ParseRequestURI(id.String()); err != nil {
return nil, errorf(id, "Invalid IRI: %s", err)
}
var err error
var obj as.Item
var resp *http.Response
if resp, err = c.Get(id.String()); err != nil {
ErrorLogger(err.Error())
return obj, err
}
if resp == nil {
err := errorf(id, "Unable to load from the AP end point: nil response")
ErrorLogger(err)
return obj, err
}
if resp.StatusCode != http.StatusOK {
err := errorf(id, "Unable to load from the AP end point: invalid status %d", resp.StatusCode)
ErrorLogger(err)
return obj, err
}
defer resp.Body.Close()
var body []byte
if body, err = ioutil.ReadAll(resp.Body); err != nil {
ErrorLogger(err)
return obj, err
}
return as.UnmarshalJSON(body)
}
func (c *client) req(method string, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", UserAgent)
//req.Header.Set("Cache-Control", "no-cache")
if err = Sign(req); err != nil {
err := errorf(as.IRI(req.URL.String()), "Unable to sign request (method %q, previous error: %s)", req.Method, err)
return req, err
}
return req, nil
}
// Head
func (c client) Head(url string) (resp *http.Response, err error) {
req, err := c.req(http.MethodHead, url, nil)
if err != nil {
return nil, err
}
return http.DefaultClient.Do(req)
}
// Get wrapper over the functionality offered by the default http.Client object
func (c client) Get(url string) (resp *http.Response, err error) {
req, err := c.req(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
return http.DefaultClient.Do(req)
}
// Post wrapper over the functionality offered by the default http.Client object
func (c *client) Post(url, contentType string, body io.Reader) (resp *http.Response, err error) {
req, err := c.req(http.MethodPost, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
return http.DefaultClient.Do(req)
}
// Put wrapper over the functionality offered by the default http.Client object
func (c client) Put(url, contentType string, body io.Reader) (resp *http.Response, err error) {
req, err := c.req(http.MethodPut, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
return http.DefaultClient.Do(req)
}
// Delete wrapper over the functionality offered by the default http.Client object
func (c client) Delete(url, contentType string, body io.Reader) (resp *http.Response, err error) {
req, err := c.req(http.MethodDelete, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
return http.DefaultClient.Do(req)
}

71
client/client_test.go Normal file
View file

@ -0,0 +1,71 @@
package client
import (
"strings"
"testing"
as "github.com/mariusor/activitypub.go/activitystreams"
)
func TestNewClient(t *testing.T) {
c := client{}
if c != NewClient() {
t.Errorf("NewClient didn't return a valid client")
}
}
func TestErr_Error(t *testing.T) {
e := err{
msg: "test",
iri: as.IRI(""),
}
if len(e.Error()) == 0 {
t.Errorf("error message should not be empty")
}
if !strings.Contains(e.Error(), "test") {
t.Errorf("error message should contain the 'test' string")
}
}
func TestClient_LoadIRI(t *testing.T) {
empty := as.IRI("")
c := NewClient()
var err error
_, err = c.LoadIRI(empty)
if err == nil {
t.Errorf("LoadIRI should have failed when using empty IRI value")
} else {
t.Logf("Valid error received: %s", err)
}
inv := as.IRI("example.com")
_, err = c.LoadIRI(inv)
if err == nil {
t.Errorf("LoadIRI should have failed when using invalid http url")
} else {
t.Logf("Valid error received: %s", err)
}
}
func TestClient_Get(t *testing.T) {
}
func TestClient_Head(t *testing.T) {
}
func TestClient_Post(t *testing.T) {
}
func TestClient_Put(t *testing.T) {
}
func TestClient_Delete(t *testing.T) {
}