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

60 lines
1.2 KiB
Go
Raw Normal View History

2020-02-04 20:25:57 +00:00
package main
import (
"bytes"
"encoding/json"
"fmt"
"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
}
2020-04-11 19:26:48 +00:00
inbox := (*info)["inbox"].(string)
2020-02-04 20:25:57 +00:00
var endpoints map[string]interface{}
var sharedInbox string
2020-04-11 19:26:48 +00:00
if (*info)["endpoints"] != nil {
endpoints = (*info)["endpoints"].(map[string]interface{})
2020-02-04 20:25:57 +00:00
if val, ok := endpoints["sharedInbox"]; ok {
sharedInbox = val.(string)
}
}
return RemoteActor{
iri: iri,
inbox: inbox,
sharedInbox: sharedInbox,
}, err
}
2020-04-11 19:26:48 +00:00
func get(iri string) (info *map[string]interface{}, err error) {
2020-02-04 20:25:57 +00:00
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")
2020-02-07 09:24:19 +00:00
resp, err := http.DefaultClient.Do(req)
2020-02-04 20:25:57 +00:00
if err != nil {
return
}
if !isSuccess(resp.StatusCode) {
return
}
2020-04-11 19:26:48 +00:00
var e map[string]interface{}
2020-03-26 18:04:05 +00:00
err = json.NewDecoder(resp.Body).Decode(&e)
2020-02-04 20:25:57 +00:00
if err != nil {
return
}
2020-04-11 19:26:48 +00:00
info = &e
2020-02-04 20:25:57 +00:00
return
}