Added additional functionality to allow for both objects and arrays to be returned from internal-apis client.
Also added a raw API url call and converted current call to a call to a resource so we are not restricted to that format to use the library.
This commit is contained in:
parent
8e6d493fbf
commit
9130630afe
2 changed files with 134 additions and 23 deletions
|
@ -16,7 +16,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultServerAddress = "https://api.lbry.com"
|
defaultServerAddress = "https://api.odysee.tv"
|
||||||
timeout = 5 * time.Second
|
timeout = 5 * time.Second
|
||||||
headerForwardedFor = "X-Forwarded-For"
|
headerForwardedFor = "X-Forwarded-For"
|
||||||
|
|
||||||
|
@ -44,9 +44,36 @@ type ClientOpts struct {
|
||||||
|
|
||||||
// APIResponse reflects internal-apis JSON response format.
|
// APIResponse reflects internal-apis JSON response format.
|
||||||
type APIResponse struct {
|
type APIResponse struct {
|
||||||
Success bool `json:"success"`
|
Success bool `json:"success"`
|
||||||
Error *string `json:"error"`
|
Error *string `json:"error"`
|
||||||
Data *ResponseData `json:"data"`
|
Data interface{} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type data struct {
|
||||||
|
obj map[string]interface{}
|
||||||
|
array []interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d data) IsObject() bool {
|
||||||
|
return d.obj != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d data) IsArray() bool {
|
||||||
|
return d.array != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d data) Object() (map[string]interface{}, error) {
|
||||||
|
if d.obj == nil {
|
||||||
|
return nil, errors.New("no object data found")
|
||||||
|
}
|
||||||
|
return d.obj, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d data) Array() ([]interface{}, error) {
|
||||||
|
if d.array == nil {
|
||||||
|
return nil, errors.New("no array data found")
|
||||||
|
}
|
||||||
|
return d.array, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// APIError wraps errors returned by LBRY API server to discern them from other kinds (like http errors).
|
// APIError wraps errors returned by LBRY API server to discern them from other kinds (like http errors).
|
||||||
|
@ -59,7 +86,12 @@ func (e APIError) Error() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResponseData is a map containing parsed json response.
|
// ResponseData is a map containing parsed json response.
|
||||||
type ResponseData map[string]interface{}
|
type ResponseData interface {
|
||||||
|
IsObject() bool
|
||||||
|
IsArray() bool
|
||||||
|
Object() (map[string]interface{}, error)
|
||||||
|
Array() ([]interface{}, error)
|
||||||
|
}
|
||||||
|
|
||||||
func makeMethodPath(obj, method string) string {
|
func makeMethodPath(obj, method string) string {
|
||||||
return fmt.Sprintf("/%s/%s", obj, method)
|
return fmt.Sprintf("/%s/%s", obj, method)
|
||||||
|
@ -111,6 +143,10 @@ func (c Client) getEndpointURL(object, method string) string {
|
||||||
return fmt.Sprintf("%s%s", c.serverAddress, makeMethodPath(object, method))
|
return fmt.Sprintf("%s%s", c.serverAddress, makeMethodPath(object, method))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c Client) getEndpointURLFromPath(path string) string {
|
||||||
|
return fmt.Sprintf("%s%s", c.serverAddress, path)
|
||||||
|
}
|
||||||
|
|
||||||
func (c Client) prepareParams(params map[string]interface{}) (string, error) {
|
func (c Client) prepareParams(params map[string]interface{}) (string, error) {
|
||||||
form := url.Values{}
|
form := url.Values{}
|
||||||
if c.AuthToken != "" {
|
if c.AuthToken != "" {
|
||||||
|
@ -164,36 +200,70 @@ func (c Client) doCall(url string, payload string) ([]byte, error) {
|
||||||
return ioutil.ReadAll(r.Body)
|
return ioutil.ReadAll(r.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call calls a remote internal-apis server, returning a response,
|
// CallResource calls a remote internal-apis server resource, returning a response,
|
||||||
// wrapped into standardized API Response struct.
|
// wrapped into standardized API Response struct.
|
||||||
func (c Client) Call(object, method string, params map[string]interface{}) (ResponseData, error) {
|
func (c Client) CallResource(object, method string, params map[string]interface{}) (ResponseData, error) {
|
||||||
var rd ResponseData
|
var d data
|
||||||
payload, err := c.prepareParams(params)
|
payload, err := c.prepareParams(params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return rd, err
|
return d, err
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := c.doCall(c.getEndpointURL(object, method), payload)
|
body, err := c.doCall(c.getEndpointURL(object, method), payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return rd, err
|
return d, err
|
||||||
}
|
}
|
||||||
var ar APIResponse
|
var ar APIResponse
|
||||||
err = json.Unmarshal(body, &ar)
|
err = json.Unmarshal(body, &ar)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return rd, err
|
return d, err
|
||||||
}
|
}
|
||||||
if !ar.Success {
|
if !ar.Success {
|
||||||
return rd, APIError{errors.New(*ar.Error)}
|
return d, APIError{errors.New(*ar.Error)}
|
||||||
}
|
}
|
||||||
return *ar.Data, err
|
if v, ok := ar.Data.([]interface{}); ok {
|
||||||
|
d.array = v
|
||||||
|
} else if v, ok := ar.Data.(map[string]interface{}); ok {
|
||||||
|
d.obj = v
|
||||||
|
}
|
||||||
|
return d, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call calls a remote internal-apis server, returning a response,
|
||||||
|
// wrapped into standardized API Response struct.
|
||||||
|
func (c Client) Call(path string, params map[string]interface{}) (ResponseData, error) {
|
||||||
|
var d data
|
||||||
|
payload, err := c.prepareParams(params)
|
||||||
|
if err != nil {
|
||||||
|
return d, err
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := c.doCall(c.getEndpointURLFromPath(path), payload)
|
||||||
|
if err != nil {
|
||||||
|
return d, err
|
||||||
|
}
|
||||||
|
var ar APIResponse
|
||||||
|
err = json.Unmarshal(body, &ar)
|
||||||
|
if err != nil {
|
||||||
|
return d, err
|
||||||
|
}
|
||||||
|
if !ar.Success {
|
||||||
|
return d, APIError{errors.New(*ar.Error)}
|
||||||
|
}
|
||||||
|
if v, ok := ar.Data.([]interface{}); ok {
|
||||||
|
d.array = v
|
||||||
|
} else if v, ok := ar.Data.(map[string]interface{}); ok {
|
||||||
|
d.obj = v
|
||||||
|
}
|
||||||
|
return d, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserMe returns user details for the user associated with the current auth_token.
|
// UserMe returns user details for the user associated with the current auth_token.
|
||||||
func (c Client) UserMe() (ResponseData, error) {
|
func (c Client) UserMe() (ResponseData, error) {
|
||||||
return c.Call(userObjectPath, userMeMethod, map[string]interface{}{})
|
return c.CallResource(userObjectPath, userMeMethod, map[string]interface{}{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserHasVerifiedEmail calls has_verified_email method.
|
// UserHasVerifiedEmail calls has_verified_email method.
|
||||||
func (c Client) UserHasVerifiedEmail() (ResponseData, error) {
|
func (c Client) UserHasVerifiedEmail() (ResponseData, error) {
|
||||||
return c.Call(userObjectPath, userHasVerifiedEmailMethod, map[string]interface{}{})
|
return c.CallResource(userObjectPath, userHasVerifiedEmailMethod, map[string]interface{}{})
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,25 @@ func TestUserMe(t *testing.T) {
|
||||||
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL})
|
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL})
|
||||||
r, err := c.UserMe()
|
r, err := c.UserMe()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "user@lbry.tv", r["primary_email"])
|
robj, err := r.Object()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
assert.Equal(t, "user@lbry.tv", robj["primary_email"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListFiltered(t *testing.T) {
|
||||||
|
ts := launchDummyServer(nil, "/file/list_filtered", listFilteredResponse, http.StatusOK)
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL})
|
||||||
|
r, err := c.CallResource("file", "list_filtered", map[string]interface{}{"with_claim_id": "true"})
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.True(t, r.IsArray())
|
||||||
|
_, err = r.Array()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUserHasVerifiedEmail(t *testing.T) {
|
func TestUserHasVerifiedEmail(t *testing.T) {
|
||||||
|
@ -52,8 +70,12 @@ func TestUserHasVerifiedEmail(t *testing.T) {
|
||||||
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL})
|
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL})
|
||||||
r, err := c.UserHasVerifiedEmail()
|
r, err := c.UserHasVerifiedEmail()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.EqualValues(t, 12345, r["user_id"])
|
robj, err := r.Object()
|
||||||
assert.Equal(t, true, r["has_verified_email"])
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
assert.EqualValues(t, 12345, robj["user_id"])
|
||||||
|
assert.Equal(t, true, robj["has_verified_email"])
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUserHasVerifiedEmailOAuth(t *testing.T) {
|
func TestUserHasVerifiedEmailOAuth(t *testing.T) {
|
||||||
|
@ -63,8 +85,12 @@ func TestUserHasVerifiedEmailOAuth(t *testing.T) {
|
||||||
c := NewOauthClient(oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "Test-Access-Token"}), &ClientOpts{ServerAddress: ts.URL})
|
c := NewOauthClient(oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "Test-Access-Token"}), &ClientOpts{ServerAddress: ts.URL})
|
||||||
r, err := c.UserHasVerifiedEmail()
|
r, err := c.UserHasVerifiedEmail()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.EqualValues(t, 12345, r["user_id"])
|
robj, err := r.Object()
|
||||||
assert.Equal(t, true, r["has_verified_email"])
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
assert.EqualValues(t, 12345, robj["user_id"])
|
||||||
|
assert.Equal(t, true, robj["has_verified_email"])
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRemoteIP(t *testing.T) {
|
func TestRemoteIP(t *testing.T) {
|
||||||
|
@ -82,7 +108,7 @@ func TestWrongToken(t *testing.T) {
|
||||||
c := NewClient("zcasdasc", nil)
|
c := NewClient("zcasdasc", nil)
|
||||||
|
|
||||||
r, err := c.UserHasVerifiedEmail()
|
r, err := c.UserHasVerifiedEmail()
|
||||||
assert.Nil(t, r)
|
assert.False(t, r.IsObject())
|
||||||
assert.EqualError(t, err, "api error: could not authenticate user")
|
assert.EqualError(t, err, "api error: could not authenticate user")
|
||||||
assert.ErrorAs(t, err, &APIError{})
|
assert.ErrorAs(t, err, &APIError{})
|
||||||
}
|
}
|
||||||
|
@ -91,7 +117,7 @@ func TestHTTPError(t *testing.T) {
|
||||||
c := NewClient("zcasdasc", &ClientOpts{ServerAddress: "http://lolcathost"})
|
c := NewClient("zcasdasc", &ClientOpts{ServerAddress: "http://lolcathost"})
|
||||||
|
|
||||||
r, err := c.UserHasVerifiedEmail()
|
r, err := c.UserHasVerifiedEmail()
|
||||||
assert.Nil(t, r)
|
assert.False(t, r.IsObject())
|
||||||
assert.EqualError(t, err, `Post "http://lolcathost/user/has_verified_email": dial tcp: lookup lolcathost: no such host`)
|
assert.EqualError(t, err, `Post "http://lolcathost/user/has_verified_email": dial tcp: lookup lolcathost: no such host`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,7 +128,7 @@ func TestGatewayError(t *testing.T) {
|
||||||
c := NewClient("zcasdasc", &ClientOpts{ServerAddress: ts.URL})
|
c := NewClient("zcasdasc", &ClientOpts{ServerAddress: ts.URL})
|
||||||
|
|
||||||
r, err := c.UserHasVerifiedEmail()
|
r, err := c.UserHasVerifiedEmail()
|
||||||
assert.Nil(t, r)
|
assert.False(t, r.IsObject())
|
||||||
assert.EqualError(t, err, `server returned non-OK status: 502`)
|
assert.EqualError(t, err, `server returned non-OK status: 502`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,3 +165,18 @@ const userHasVerifiedEmailResponse = `{
|
||||||
"has_verified_email": true
|
"has_verified_email": true
|
||||||
}
|
}
|
||||||
}`
|
}`
|
||||||
|
|
||||||
|
const listFilteredResponse = `{
|
||||||
|
"success": true,
|
||||||
|
"error": null,
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"claim_id": "322ce77e9085d9da42279c790f7c9755b4916fca",
|
||||||
|
"outpoint": "20e04af21a569061ced7aa1801a43b4ed4839dfeb79919ea49a4059c7fe114c5:0"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"claim_id": "61496c567badcd98b82d9a700a8d56fd8a5fa8fb",
|
||||||
|
"outpoint": "657e4ec774524b326f9d3ecb9f468ea085bd1f3d450565f0330feca02e8fd25b:0"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}`
|
||||||
|
|
Loading…
Add table
Reference in a new issue