this repo has no description

add a nervana client that is reusable

+1 -1
handle_create.go
···
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
-
nervanaItems, err := p.makeNervanaRequest(ctx, rec.Text)
+
nervanaItems, err := p.nervanaClient.MakeRequest(ctx, rec.Text)
if err != nil {
p.logger.Error("error making nervana items request", "error", err)
return
-60
nervana.go
···
-
package photocopy
-
-
import (
-
"bytes"
-
"context"
-
"encoding/json"
-
"fmt"
-
"io"
-
"net/http"
-
)
-
-
type NervanaItem struct {
-
Text string `json:"text"`
-
Label string `json:"label"`
-
EntityId string `json:"entityId"`
-
Description string `json:"description"`
-
}
-
-
func (p *Photocopy) newNervanaRequest(ctx context.Context, text string) (*http.Request, error) {
-
payload := map[string]string{
-
"text": text,
-
"language": "en",
-
}
-
-
b, err := json.Marshal(payload)
-
if err != nil {
-
return nil, err
-
}
-
-
req, err := http.NewRequestWithContext(ctx, "POST", p.nervanaEndpoint, bytes.NewReader(b))
-
-
req.Header.Set("Authorization", "Bearer "+p.nervanaApiKey)
-
-
return req, err
-
}
-
-
func (p *Photocopy) makeNervanaRequest(ctx context.Context, text string) ([]NervanaItem, error) {
-
req, err := p.newNervanaRequest(ctx, text)
-
if err != nil {
-
return nil, err
-
}
-
-
resp, err := p.nervanaClient.Do(req)
-
if err != nil {
-
return nil, err
-
}
-
defer resp.Body.Close()
-
-
if resp.StatusCode != 200 {
-
io.Copy(io.Discard, resp.Body)
-
return nil, fmt.Errorf("received non-200 response code: %d", resp.StatusCode)
-
}
-
-
var nervanaResp []NervanaItem
-
if err := json.NewDecoder(resp.Body).Decode(&nervanaResp); err != nil {
-
return nil, err
-
}
-
-
return nervanaResp, nil
-
}
+77
nervana/client.go
···
+
package nervana
+
+
import (
+
"bytes"
+
"context"
+
"encoding/json"
+
"fmt"
+
"io"
+
"net/http"
+
"time"
+
)
+
+
type Client struct {
+
cli *http.Client
+
endpoint string
+
apiKey string
+
}
+
+
func NewClient(endpoint string, apiKey string) *Client {
+
return &Client{
+
cli: &http.Client{
+
Timeout: 5 * time.Second,
+
},
+
endpoint: endpoint,
+
apiKey: apiKey,
+
}
+
}
+
+
type NervanaItem struct {
+
Text string `json:"text"`
+
Label string `json:"label"`
+
EntityId string `json:"entityId"`
+
Description string `json:"description"`
+
}
+
+
func (c *Client) newRequest(ctx context.Context, text string) (*http.Request, error) {
+
payload := map[string]string{
+
"text": text,
+
"language": "en",
+
}
+
+
b, err := json.Marshal(payload)
+
if err != nil {
+
return nil, err
+
}
+
+
req, err := http.NewRequestWithContext(ctx, "GET", c.endpoint, bytes.NewReader(b))
+
+
req.Header.Set("Authorization", "Bearer "+c.apiKey)
+
+
return req, err
+
}
+
+
func (c *Client) MakeRequest(ctx context.Context, text string) ([]NervanaItem, error) {
+
req, err := c.newRequest(ctx, text)
+
if err != nil {
+
return nil, err
+
}
+
+
resp, err := c.cli.Do(req)
+
if err != nil {
+
return nil, err
+
}
+
defer resp.Body.Close()
+
+
if resp.StatusCode != 200 {
+
io.Copy(io.Discard, resp.Body)
+
return nil, fmt.Errorf("received non-200 response code: %d", resp.StatusCode)
+
}
+
+
var nervanaResp []NervanaItem
+
if err := json.NewDecoder(resp.Body).Decode(&nervanaResp); err != nil {
+
return nil, err
+
}
+
+
return nervanaResp, nil
+
}
+3 -6
photocopy.go
···
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
"github.com/haileyok/photocopy/clickhouse_inserter"
+
"github.com/haileyok/photocopy/nervana"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
···
conn driver.Conn
-
nervanaClient *http.Client
+
nervanaClient *nervana.Client
nervanaEndpoint string
nervanaApiKey string
}
···
p.plcScraper = plcs
if args.NervanaApiKey != "" && args.NervanaEndpoint != "" {
-
p.nervanaClient = &http.Client{
-
Timeout: 5 * time.Second,
-
}
-
p.nervanaEndpoint = args.NervanaEndpoint
-
p.nervanaApiKey = args.NervanaApiKey
+
p.nervanaClient = nervana.NewClient(args.NervanaEndpoint, args.NervanaApiKey)
}
return p, nil