|
| 1 | +// Package newsbox provides a client for accessing newsbox services. |
| 2 | +package newsbox |
| 3 | + |
| 4 | +import ( |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/machinebox/sdk-go/x/boxutil" |
| 12 | + "github.com/pkg/errors" |
| 13 | +) |
| 14 | + |
| 15 | +// Analysis represents an analysis of title, content and domain. |
| 16 | +type Analysis struct { |
| 17 | + // Title is the response object for the title analysis. |
| 18 | + Title Title `json:"title"` |
| 19 | + // Content is the response object for the content analysis. |
| 20 | + Content Content `json:"content"` |
| 21 | + // Domain is the response object for the domain analysis. |
| 22 | + Domain Domain `json:"domain"` |
| 23 | +} |
| 24 | + |
| 25 | +// Title is the response object for the title analysis. |
| 26 | +type Title struct { |
| 27 | + // Decision is the string representing the decision could be bias/unsure/impartial. |
| 28 | + Decision string `json:"decision,omitempty"` |
| 29 | + // Score is the numeric score of the decision is between 0.00 (bias) and 1.00 (impartial). |
| 30 | + Score float64 `json:"score,omitempty"` |
| 31 | + // Entities represents entities discovered in the text. |
| 32 | + Entities []Entity `json:"entities,omitempty"` |
| 33 | +} |
| 34 | + |
| 35 | +// Content is the response object for the content analysis. |
| 36 | +type Content struct { |
| 37 | + // Decision is the string representing the decision could be bias/unsure/impartial. |
| 38 | + Decision string `json:"decision,omitempty"` |
| 39 | + // Score is the numeric score of the decision is between 0.00 (bias) and 1.00 (impartial). |
| 40 | + Score float64 `json:"score,omitempty"` |
| 41 | + // Entities represents entities discovered in the text. |
| 42 | + Entities []Entity `json:"entities,omitempty"` |
| 43 | + // Keywords are the most relevant keywords extracted from the text. |
| 44 | + Keywords []Keyword `json:"keywords"` |
| 45 | +} |
| 46 | + |
| 47 | +// Domain is the response object for the domain analysis. |
| 48 | +type Domain struct { |
| 49 | + // Domain is the domain extracted from the URL. |
| 50 | + Domain string `json:"domain,omitempty"` |
| 51 | + // Category is one of the listed on the API docs. |
| 52 | + Category string `json:"category,omitempty"` |
| 53 | +} |
| 54 | + |
| 55 | +// Entity represents an entity discovered in the text. |
| 56 | +type Entity struct { |
| 57 | + // Type is a string describing the kind of entity. |
| 58 | + Type string `json:"type"` |
| 59 | + // Text is the text of the entity. |
| 60 | + Text string `json:"text"` |
| 61 | + // Start is the absolute start position of the entity (in the original text). |
| 62 | + Start int `json:"start"` |
| 63 | + // Start is the absolute end position of the entity (in the original text). |
| 64 | + End int `json:"end"` |
| 65 | +} |
| 66 | + |
| 67 | +// Keyword represents a key word. |
| 68 | +type Keyword struct { |
| 69 | + Keyword string `json:"keyword"` |
| 70 | +} |
| 71 | + |
| 72 | +// Client is an HTTP client that can make requests to the box. |
| 73 | +type Client struct { |
| 74 | + addr string |
| 75 | + |
| 76 | + // HTTPClient is the http.Client that will be used to |
| 77 | + // make requests. |
| 78 | + HTTPClient *http.Client |
| 79 | +} |
| 80 | + |
| 81 | +// New makes a new Client. |
| 82 | +func New(addr string) *Client { |
| 83 | + return &Client{ |
| 84 | + addr: addr, |
| 85 | + HTTPClient: &http.Client{ |
| 86 | + Timeout: 10 * time.Second, |
| 87 | + }, |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Info gets the details about the box. |
| 92 | +func (c *Client) Info() (*boxutil.Info, error) { |
| 93 | + var info boxutil.Info |
| 94 | + u, err := url.Parse(c.addr + "/info") |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + if !u.IsAbs() { |
| 99 | + return nil, errors.New("box address must be absolute") |
| 100 | + } |
| 101 | + req, err := http.NewRequest("GET", u.String(), nil) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + req.Header.Set("Accept", "application/json; charset=utf-8") |
| 106 | + resp, err := c.HTTPClient.Do(req) |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + defer resp.Body.Close() |
| 111 | + if err := json.NewDecoder(resp.Body).Decode(&info); err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + return &info, nil |
| 115 | +} |
| 116 | + |
| 117 | +// Check passes the text from the Reader to newsbox for analysis. |
| 118 | +func (c *Client) Check(title string, content string, u *url.URL) (*Analysis, error) { |
| 119 | + uu, err := url.Parse(c.addr + "/newsbox/check") |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + if !u.IsAbs() { |
| 124 | + return nil, errors.New("box address must be absolute") |
| 125 | + } |
| 126 | + vals := url.Values{} |
| 127 | + vals.Set("title", title) |
| 128 | + vals.Set("content", content) |
| 129 | + vals.Set("url", u.String()) |
| 130 | + |
| 131 | + req, err := http.NewRequest("POST", uu.String(), strings.NewReader(vals.Encode())) |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 136 | + req.Header.Set("Accept", "application/json; charset=utf-8") |
| 137 | + resp, err := c.HTTPClient.Do(req) |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + defer resp.Body.Close() |
| 142 | + var response struct { |
| 143 | + Success bool |
| 144 | + Error string |
| 145 | + |
| 146 | + Title Title `json:"title"` |
| 147 | + Content Content `json:"content"` |
| 148 | + Domain Domain `json:"domain"` |
| 149 | + } |
| 150 | + if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { |
| 151 | + return nil, errors.Wrap(err, "decoding response") |
| 152 | + } |
| 153 | + if !response.Success { |
| 154 | + return nil, ErrNewsbox(response.Error) |
| 155 | + } |
| 156 | + return &Analysis{ |
| 157 | + Title: response.Title, |
| 158 | + Content: response.Content, |
| 159 | + Domain: response.Domain, |
| 160 | + }, nil |
| 161 | +} |
| 162 | + |
| 163 | +// ErrNewsbox represents an error from newsbox. |
| 164 | +type ErrNewsbox string |
| 165 | + |
| 166 | +func (e ErrNewsbox) Error() string { |
| 167 | + return "newsbox: " + string(e) |
| 168 | +} |
0 commit comments