-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
203 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,204 @@ | ||
package resources | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"mime/multipart" | ||
"net/http" | ||
"net/http/httputil" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/AlekSi/alice" | ||
) | ||
|
||
type Quota struct { | ||
Total int | ||
Used int | ||
} | ||
|
||
type Sound struct { | ||
ID string | ||
SkillID string | ||
Size *int | ||
OriginalName string | ||
CreatedAt time.Time | ||
IsProcessed bool | ||
Error *string | ||
} | ||
|
||
type Client struct { | ||
SkillID string | ||
OAuthToken string | ||
HTTPClient *http.Client | ||
|
||
// debugging options | ||
Debugf alice.Printf // debug logger | ||
Indent bool // indent requests and responses | ||
StrictDecoder bool // disallow unexpected fields in responses | ||
} | ||
|
||
func (c *Client) do(req *http.Request, respBody interface{}) error { | ||
httpClient := c.HTTPClient | ||
if httpClient == nil { | ||
httpClient = http.DefaultClient | ||
} | ||
|
||
var jsonRequst bool | ||
if c.OAuthToken != "" { | ||
req.Header.Set("Authorization", "OAuth "+c.OAuthToken) | ||
} | ||
if req.Body != nil && req.Header.Get("Content-Type") == "" { | ||
jsonRequst = true | ||
req.Header.Set("Content-Type", "application/json; charset=utf-8") | ||
} | ||
|
||
if c.Debugf != nil { | ||
if c.Indent && jsonRequst { | ||
b, err := ioutil.ReadAll(req.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var body bytes.Buffer | ||
if err = json.Indent(&body, b, "", " "); err != nil { | ||
return err | ||
} | ||
req.Body = ioutil.NopCloser(&body) | ||
req.ContentLength = int64(body.Len()) | ||
req.TransferEncoding = nil | ||
} | ||
|
||
b, err := httputil.DumpRequestOut(req, jsonRequst) | ||
if err != nil { | ||
return err | ||
} | ||
c.debugf("Request:\n%s", b) | ||
} | ||
|
||
resp, err := httpClient.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() //nolint:errcheck | ||
|
||
if c.Debugf != nil { | ||
if c.Indent { | ||
b, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var body bytes.Buffer | ||
if err = json.Indent(&body, b, "", " "); err != nil { | ||
return err | ||
} | ||
resp.Body = ioutil.NopCloser(&body) | ||
resp.ContentLength = int64(body.Len()) | ||
resp.TransferEncoding = nil | ||
} | ||
|
||
b, err := httputil.DumpResponse(resp, true) | ||
if err != nil { | ||
return err | ||
} | ||
c.debugf("Response:\n%s", b) | ||
} | ||
|
||
if resp.StatusCode/100 != 2 { | ||
return fmt.Errorf("status code %d", resp.StatusCode) | ||
} | ||
|
||
d := json.NewDecoder(resp.Body) | ||
if c.StrictDecoder { | ||
d.DisallowUnknownFields() | ||
} | ||
return d.Decode(&respBody) | ||
} | ||
|
||
func (c *Client) http() *http.Client { | ||
if c.HTTPClient != nil { | ||
return c.HTTPClient | ||
func (c *Client) debugf(format string, a ...interface{}) { | ||
if c.Debugf != nil { | ||
c.Debugf(format, a...) | ||
} | ||
return http.DefaultClient | ||
} | ||
|
||
type StatusResponse struct { | ||
Total int | ||
Used int | ||
Images struct { | ||
Quota Quota | ||
} | ||
Sounds struct { | ||
Quota Quota | ||
} | ||
} | ||
|
||
func (c *Client) Status() (*StatusResponse, error) { | ||
req, err := http.NewRequest("GET", "https://dialogs.yandex.net/api/v1/status", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if c.OAuthToken != "" { | ||
req.Header.Set("Authorization", "OAuth "+c.OAuthToken) | ||
|
||
var res StatusResponse | ||
if err = c.do(req, &res); err != nil { | ||
return nil, err | ||
} | ||
return &res, nil | ||
} | ||
|
||
resp, err := c.http().Do(req) | ||
func (c *Client) UploadSound(name string, r io.Reader) (*Sound, error) { | ||
var buf bytes.Buffer | ||
mw := multipart.NewWriter(&buf) | ||
fw, err := mw.CreateFormFile("file", name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode/100 != 2 { | ||
return nil, fmt.Errorf("status code %d", resp.StatusCode) | ||
if _, err = io.Copy(fw, r); err != nil { | ||
return nil, err | ||
} | ||
if err = mw.Close(); err != nil { | ||
return nil, err | ||
} | ||
|
||
req, err := http.NewRequest("POST", "https://dialogs.yandex.net/api/v1/skills/"+c.SkillID+"/sounds", &buf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Add("Content-Type", mw.FormDataContentType()) | ||
|
||
var res struct { | ||
Images struct { | ||
Quota StatusResponse | ||
} | ||
Sound Sound | ||
} | ||
if err = c.do(req, &res); err != nil { | ||
return nil, err | ||
} | ||
return &res.Sound, nil | ||
} | ||
|
||
func (c *Client) UploadSoundFile(filename string) (*Sound, error) { | ||
f, err := os.Open(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() //nolint:errcheck | ||
|
||
return c.UploadSound(filepath.Base(filename), f) | ||
} | ||
|
||
func (c *Client) ListSounds() ([]Sound, error) { | ||
req, err := http.NewRequest("GET", "https://dialogs.yandex.net/api/v1/skills/"+c.SkillID+"/sounds", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var res struct { | ||
Sounds []Sound | ||
Total int | ||
} | ||
if err = json.NewDecoder(resp.Body).Decode(&res); err != nil { | ||
if err = c.do(req, &res); err != nil { | ||
return nil, err | ||
} | ||
return &res.Images.Quota, nil | ||
return res.Sounds, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters