Compare commits

...

3 Commits

Author SHA1 Message Date
17bee2852a
Error on unsupported StatusCode in GetEndpoints
All checks were successful
the build was successful
2018-12-07 21:52:19 +01:00
5c4144444a
Add items to items 2018-12-07 21:51:44 +01:00
07a3c1b1e3
Add status checks to get requests of ek 2018-12-07 21:23:01 +01:00
3 changed files with 49 additions and 6 deletions

View File

@ -127,6 +127,13 @@ func (c *Client) ChannelsGetList() ([]microsub.Channel, error) {
return []microsub.Channel{}, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return []microsub.Channel{}, fmt.Errorf("HTTP Status is not 200, but %d, error while reading body", res.StatusCode)
}
return []microsub.Channel{}, fmt.Errorf("HTTP Status is not 200, but %d: %s", res.StatusCode, body)
}
type channelsResponse struct {
Channels []microsub.Channel `json:"channels"`
@ -149,6 +156,13 @@ func (c *Client) TimelineGet(before, after, channel string) (microsub.Timeline,
return microsub.Timeline{}, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return microsub.Timeline{}, fmt.Errorf("HTTP Status is not 200, but %d, error while reading body", res.StatusCode)
}
return microsub.Timeline{}, fmt.Errorf("HTTP Status is not 200, but %d: %s", res.StatusCode, body)
}
dec := json.NewDecoder(res.Body)
var timeline microsub.Timeline
err = dec.Decode(&timeline)
@ -166,8 +180,16 @@ func (c *Client) PreviewURL(url string) (microsub.Timeline, error) {
return microsub.Timeline{}, err
}
defer res.Body.Close()
dec := json.NewDecoder(res.Body)
var timeline microsub.Timeline
if res.StatusCode != 200 {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return timeline, fmt.Errorf("HTTP Status is not 200, but %d, error while reading body", res.StatusCode)
}
return timeline, fmt.Errorf("HTTP Status is not 200, but %d: %s", res.StatusCode, body)
}
dec := json.NewDecoder(res.Body)
err = dec.Decode(&timeline)
if err != nil {
return microsub.Timeline{}, err
@ -183,6 +205,13 @@ func (c *Client) FollowGetList(channel string) ([]microsub.Feed, error) {
return []microsub.Feed{}, nil
}
defer res.Body.Close()
if res.StatusCode != 200 {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return []microsub.Feed{}, fmt.Errorf("HTTP Status is not 200, but %d, error while reading body", res.StatusCode)
}
return []microsub.Feed{}, fmt.Errorf("HTTP Status is not 200, but %d: %s", res.StatusCode, body)
}
dec := json.NewDecoder(res.Body)
type followResponse struct {
Items []microsub.Feed `json:"items"`

View File

@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
@ -40,6 +41,14 @@ func GetEndpoints(me *url.URL) (Endpoints, error) {
}
defer res.Body.Close()
if res.StatusCode != 200 {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return endpoints, fmt.Errorf("error from endpoint while reading body %d", res.StatusCode)
}
return endpoints, fmt.Errorf("error from endpoint %d: %s", res.StatusCode, body)
}
var links linkheader.Links
if headers, e := res.Header["Link"]; e {

View File

@ -169,16 +169,21 @@ func simplify(itemType string, item map[string][]interface{}, author map[string]
return feedItem
}
func simplifyCard(v []interface{}) (map[string]string, error) {
card := make(map[string]string)
card["type"] = "card"
if value, ok := v[0].(*microformats.Microformat); ok {
card := make(map[string]string)
card["type"] = "card"
for ik, vk := range value.Properties {
if p, ok := vk[0].(string); ok {
card[ik] = p
}
}
return card, nil
} else if value, ok := v[0].(string); ok {
card["url"] = value
return card, nil
}
return nil, fmt.Errorf("not convertable to a card %q", v)
}
@ -226,9 +231,9 @@ func SimplifyMicroformatData(md *microformats.Data) []map[string]interface{} {
newItem := SimplifyMicroformat(item, nil)
delete(newItem, "children")
// if newItem["type"] == "entry" || newItem["type"] == "event" || newItem["type"] == "card" {
// items = append(items, newItem)
// }
if newItem["type"] == "entry" || newItem["type"] == "event" || newItem["type"] == "card" {
items = append(items, newItem)
}
// if c, e := newItem["children"]; e {
// if ar, ok := c.([]map[string]interface{}); ok {
// for _, item := range ar {