jlelse
/
jsonpub
Archived
1
Fork 0
This repository has been archived on 2020-04-25. You can view files and clone it, but cannot push or open issues or pull requests.
jsonpub/remoteActor.go

62 lines
1.3 KiB
Go

package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type RemoteActor struct {
iri, inbox, sharedInbox string
info map[string]interface{}
}
func NewRemoteActor(iri string) (RemoteActor, error) {
info, err := get(iri)
if err != nil {
return RemoteActor{}, err
}
inbox := info["inbox"].(string)
var endpoints map[string]interface{}
var sharedInbox string
if info["endpoints"] != nil {
endpoints = info["endpoints"].(map[string]interface{})
if val, ok := endpoints["sharedInbox"]; ok {
sharedInbox = val.(string)
}
}
return RemoteActor{
iri: iri,
inbox: inbox,
sharedInbox: sharedInbox,
}, err
}
func get(iri string) (info map[string]interface{}, err error) {
buf := new(bytes.Buffer)
req, err := http.NewRequest("GET", iri, buf)
if err != nil {
return
}
req.Header.Add("Accept", ContentTypeAs2)
req.Header.Add("User-Agent", fmt.Sprintf("%s %s", libName, version))
req.Header.Add("Accept-Charset", "utf-8")
resp, err := client.Do(req)
if err != nil {
return
}
responseData, _ := ioutil.ReadAll(resp.Body)
if !isSuccess(resp.StatusCode) {
return
}
var e interface{}
err = json.Unmarshal(responseData, &e)
if err != nil {
return
}
info = e.(map[string]interface{})
return
}