playlyfe

package module
v0.0.0-...-882e7ae Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2016 License: MIT Imports: 11 Imported by: 0

README

Playlyfe Go SDK

Playlyfe Go SDK Go Report Card GoDoc coverage

This is the official OAuth 2.0 Go or Golang client SDK for the Playlyfe API. It supports the client_credentials and authorization code OAuth 2.0 flows. For a complete API Reference checkout Playlyfe Developers for more information.

Examples

The Playlyfe class allows you to make rest api calls like GET, POST, .. etc.
To get started create a new playlyfe object using client credentials flow and then start making requests

import "github.com/playlyfe/playlyfe-go-sdk"

type Player struct {
    ID string `json:"id"`
    Alias string `json:"alias"`
}

var johny Player

func main() { 
    pl := playlyfe.NewClientV2("Your client id", "Your client secret", nil, nil)
    err := pl.Get("/runtime/player", playlyfe.H{"player_id": "johny"}, johny)  // To get player profile
}

Install

go get github.com/playlyfe/playlyfe-go-sdk

Using

Create a client

If you haven't created a client for your game yet just head over to Playlyfe and login into your account, and go to the game settings and click on client.

###1. Client Credentials Flow In the client page select Yes for both the first and second questions client

import "github.com/playlyfe/playlyfe-go-sdk"

pl := playlyfe.NewClientV2("Your client id", "Your client secret", nil, nil)

###2. Authorization Code Flow In the client page select yes for the first question and no for the second auth

import "github.com/playlyfe/playlyfe-go-sdk"

pl := playlyfe.NewCodeV2("Your client id", "Your client secret", "redirect_uri", nil, nil)

In development the sdk caches the access token in memory so you don"t need to the persist access token object. But in production it is highly recommended to persist the token to a database. It is very simple and easy to do it with redis. You can see the test cases for more examples.

In production you need to pass the Load and Store functions whose signature is like this,

load func() (token string, expires_at int64) {
    println("Loading from redis")
    return "", 50
},
store func(token string, expires_at int64) {
    println("Storing to redis")
}

3. Custom Login Flow using JWT(JSON Web Token)

In the client page select no for the first question and yes for the second jwt

import "github.com/playlyfe/playlyfe-go-sdk"

token, err := playlyfe.createJWT("your client_id", "your client_secret", 
    "player_id", // The player id associated with your user
    []string{"player.runtime.read", "player.runtime.write"}, // The scopes the player has access to
    3600; // 1 hour expiry Time
)

This is used to create jwt token which can be created when your user is authenticated. This token can then be sent to the frontend and or stored in your session. With this token the user can directly send requests to the Playlyfe API as the player.

Client Scopes

Client

Your client has certain access control restrictions. There are 3 kind of resources in the Playlyfe REST API they are,

1./admin -> routes for you to perform admin actions like making a player join a team

2./design -> routes for you to make design changes programmatically

3./runtime -> routes which the users will generally use like getting a player profile, playing an action

The resources accessible to this client can be configured to have a read permission that means only GET requests will work.

The resources accessible to this client can be configured to have a write permission that means only POST, PATCH, PUT, DELETE requests will work.

The version restriction is only for the design resource and can be used to restrict the client from accessing any version of the game design other than the one specified. By default it allows all.

If access to a route is not allowed and then you make a request to that route then you will get an error like this,

{
  "error": "access_denied",
  "error_description": "You are not allowed to access this api route"
}

Methods

API

error API("GET", // The request method can be GET/POST/PUT/PATCH/DELETE
    "", // The api route to get data from
    playlyfe.H{}, // The query params that you want to send to the route
    struct{} ,// The data you want to post to the api
    result interface{}, // The unmarshalled data
    false // Whether you want the response to be in raw string form or json
)

Get

error Get("", // The api route to get data from
    playlyfe.H{}, // The query params that you want to send to the,
    result interface{}, // The unmarshalled data
)

Post

error Post("", // The api route to post data to
    playlyfe.H{}, // The query params that you want to send to the route
    struct{},// The data you want to post to the api
    result interface{}, // The unmarshalled data
)

Patch

error Patch("" // The api route to patch data
    playlyfe.H{} // The query params that you want to send to the route
    struct{} ,// The data you want to post to the api
    result interface{}, // The unmarshalled data
)

Put

error Put("" // The api route to put data
    playlyfe.H{}, // The query params that you want to send to the route
    struct{} ,// The data you want to post to the api
    result interface{}, // The unmarshalled data
)

Delete

error Delete("" // The api route to delete the component
    playlyfe.H{} // The query params that you want to send to the route,
    result interface{}, // The unmarshalled data
)

Get Login URL

GetLoginURL() string
//This will return the url to which the user needs to be redirected for the user to login.

Exchange Code

ExchangeCode(code string)
//This is used in the auth code flow so that the sdk can get the access token.
//Before any request to the playlyfe api is made this has to be called atleast once.
//This should be called in the the route/controller which you specified in your redirect_uri

Errors
An *Error is returned whenever an error from the PlaylyfeAPI occurs in each call.The Error contains a Name and Description field which can be used to determine the type of error that occurred.

You have to type cast the error to a *PlaylyfeError first like this,

err := pl.Get("/player", playlyfe.H{"player_id": "student1"}, &player)
if pe, ok := err.(*playlyfe.Error); ok {
    return pe
} else {
    panic(err) // do what needs to be done on this type of error
}

You can read the docs at GoDoc

License

Playlyfe Go SDK
http://dev.playlyfe.com/
Copyright(c) 2014-2015, Playlyfe IT Solutions Pvt. Ltd, support@playlyfe.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateJWT

func CreateJWT(clientID, clientSecret, playerID string, scopes []string, expiry time.Duration) (string, error)

CreateJWT creates an new JWT Token which can be used with the Playlyfe API

Types

type A

type A []interface{}

A is a simpler way to declare JSON arrays

type Error

type Error struct {
	Name        string `json:"error"`
	Description string `json:"error_description"`
}

Error is any error returned from the Playlyfe API

func (*Error) Error

func (e *Error) Error() string

type GraphQLClient

type GraphQLClient struct {
	EndPoint   string
	GameID     string
	GameSecret string
	Version    string
	RuntimeID  string
	Branch     string
	PlayerID   string
	HTTPClient *http.Client
}

GraphQLClient stores all information related to the graphql client

func (*GraphQLClient) Do

func (pql *GraphQLClient) Do(greq GraphQLRequest) (result map[string]interface{}, err error)

Graphql Execute a graphql request

type GraphQLFile

type GraphQLFile struct {
	Name      string
	FieldName string
	Source    io.Reader
}

GraphQLFile represents a file which you would like to upload

type GraphQLRequest

type GraphQLRequest struct {
	Query     string
	Variables map[string]interface{}
	Operation string
	Files     []*GraphQLFile
}

GraphQLRequest represents single graphql request

type H

type H map[string]interface{}

H is a simpler way to declare JSON objects

type Load

type Load func() (token string, expiresAt int64)

Load callback is called when a new request is made you need to return the stored access token and it expiresAt time

type Playlyfe

type Playlyfe struct {
	// contains filtered or unexported fields
}

Playlyfe stores all information related to the client

func New

func New(clientID string, clientSecret string, clientType string, version string, redirectURI string, load Load, store Store) *Playlyfe

New create a new playlyfe client

func NewClientV2

func NewClientV2(clientID, clientSecret string, load Load, store Store) *Playlyfe

NewClientV2 creates a new playlyfe client with client crendentials flow

func NewCodeV2

func NewCodeV2(clientID, clientSecret, redirectURI string, load Load, store Store) *Playlyfe

NewCodeV2 creates a new playlyfe client with authorization code flow

func (*Playlyfe) API

func (p *Playlyfe) API(method string, route string, query H, postbody interface{}, result interface{}, raw bool) error

API makes a an API request to the Playlyfe API

func (*Playlyfe) Delete

func (p *Playlyfe) Delete(route string, query H, result interface{}) error

Delete makes a DELETE API request to the Playlyfe API

func (*Playlyfe) ExchangeCode

func (p *Playlyfe) ExchangeCode(code string)

ExchangeCode sets the code which you got from authorization code flow

func (*Playlyfe) Get

func (p *Playlyfe) Get(route string, query H, result interface{}) error

Get makes a GET API request to the Playlyfe API

func (*Playlyfe) GetLoginURL

func (p *Playlyfe) GetLoginURL() string

GetLoginURL gets the logout url

func (*Playlyfe) GetRaw

func (p *Playlyfe) GetRaw(route string, query H, result interface{}) error

GetRaw makes a GET API request to the Playlyfe API but returns the raw data useful for images

func (*Playlyfe) Patch

func (p *Playlyfe) Patch(route string, query H, body interface{}, result interface{}) error

Patch makes a PATCH API request to the Playlyfe API

func (*Playlyfe) Post

func (p *Playlyfe) Post(route string, query H, body interface{}, result interface{}) error

Post makes a Post API request to the Playlyfe API

func (*Playlyfe) Put

func (p *Playlyfe) Put(route string, query H, body interface{}, result interface{}) error

Put makes a PUT API request to the Playlyfe API

type Store

type Store func(token string, expiresAt int64)

Store callback is called when a token is requested you need to store the token and its expiresAt time

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL