gameworks 0.2.0
A Python Rest SDK for Gameworks Services

# Gameworks Python SDK

<div>

[![Pypi Version](https://img.shields.io/pypi/v/gameworks.svg)](https://pypi.python.org/pypi/gameworks)
[![Build Status](https://circleci.com/gh/gameworks-gwx/gameworks-python-sdk.svg?style=shield&circle-token=a0edd961651da7ec99bdea856ef42bb729f11a0f)](https://circleci.com/gh/gameworks-gwx/gameworks-python-sdk)
</div>

## Introduction

A Software Development Kit for serving Gameworks applications and services. The SDK currently allows you to connect to the Gameworks Registry API.

## Features

- Is able to connect to Gameworks services through basic authentication tokens
- Is able to connect to the Registry API service:
  - User Registration
  - Player Profile Creation
  - Publisher Profile Creation



## Future Releases

- Will be able to peform NEM blockchain transactions via the Gameworks Cashier API
- Will be able to connect to Gameworks services through OAuth 2.0 Protocol



## Installation

To install Gameworks SDK (stable build), run this command in your terminal:

```shell
$ pip install gameworks
```

## Development

Code is hosted at [Gameworks Python SDK Git Repository](https://github.com/gameworks-gwx/gameworks-python-sdk)

```shell
$ git clone git://github.com/gameworks-gwx/gameworks-python-sdk.git
$ cd gameworks-python-sdk
```

Set up a virtual environment to isolate python dependencies.

```shell
$ virtualenv venv
$ source venv/bin/activate
```

Install development dependencies.

```shell
$ pip install -r requirements-dev.txt #optional
```

Run the tests.

```shell
$ python -m unittest
```

## Configuration

To get started, import the gameworks SDK python module.

```python
import gameworks
```

Create a .env file in your respository to set your environment variables. In this context, the .env file is used to configure two environment variables: authorization key for accessing the API `GAMEWORKS_KEY` and the API environment `GAMEWORKS_ENV` you would like to connect to. There are two environments provisioned for the SDK which are `sandbox` and `live`; by default the SDK points to the sandbox environment. The sandbox environment is used as a playground for development purposes, while the live environment connects to the actual Registry API. 

```sh
$ touch .env
$ sudo nano .env
```

### .env 

```sh
export GAMEWORKS_KEY="Authentication Token"
export GAMEWORKS_ENV="live" #sandbox or live
```

Then, execute commands in the .env file using source

```sh
$ source .env
```

## Usage

### Session Module

This module allows you to **configure** your authentication variables, **register** a user account and **login** using an existing user account.  You may choose to login the Registry API using your Gameworks credentials or use an existing JWT access token provided to you when you previously logged in. 



#### Registering a User Account

When creating a user, use the wallet address provided by the **Gameworks Mobile Wallet** or a **NEM wallet address.**

```python
from gameworks import Session

response = Session.register(
    firstName='Luke',
    lastName='Skywalker',
    walletAddress='TBZMQO7ZPBYNBDUR7F75MAKA2S3DHDCIFG775N3D',
    email='luke@therebellion.io',
    password='nooooooooooo!!!!!!???!?!!!'
    passwordConfirmation='nooooooooooo!!!!!!???!?!!!'
)

# At this point, the SDK has already thrown an HTTP call to the Registry API.
print(response.status_code) #Returns the status code of the response (e.g. 200, 404)
print(response.text) #Returns the whole response in text format

```

Take note that **all** **results** are returned as a **Response** object. You may refer to the [requests package response object](http://docs.python-requests.org/en/master/user/quickstart/#response-content).



#### Login using a User Account

Logging in using a user account, will trigger another token to be created. Tokens **expire within an hour** from the time of creation. 

```python
from gameworks import Session

response = Session.login(
    email='luke@therebellion.io',
    password='nooooooooooo!!!!!!???!?!!!'
)
```



#### Storing a Token

If you already have an access token, you may choose to bypass the login process and directly use the functionalities of the Gameworks services. As of the alpha release, the token exported as an environment variable is being used thus there is no need to use this function for authentication.

```python
from gameworks import Session

response = Session.store_key(
    key='eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxMTAsImV4cCI6MT...',
    userId=1
)
```

------

### Registry Module 

This module allows you create, update and view a **user** account, a **player** profile and a **publisher** account. All functionlaities contained in this module can be supplmented by tokens/keys. You may add them into the parameters in order to manually activate functionalities without having to create the .env file. See the code below as an example:

```python
from gameworks.registry import User

response = User.find(
    userId=1,
    key='eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxMTAsImV4cCI6MT...'
)
```



#### User Resource Functionalities

------

#### Find User

<span style="color:red">* Authentication Token Required </span>

To retrieve your user's account details, you may choose to query for it using the code below:

```python
from gameworks.registry import User

# You may only use your own user id for this function
response = User.find(userId=1)
```



#### Update User

<span style="color:red">* Authentication Token Required </span>

You may choose to update specific data in your account information. You don't need to fill up all the parameters in the account information. Updatable account parameters include: `firstName`, `lastName`, `walletAddress` and `pk` (private key associated to the NEM wallet address)

```python
from gameworks.registry import User

response = User.update(
    userId=1,
    firstName='Luke',
    lastName='Skywalker',
    walletAddress='TBZMQO7ZPBYNBDUR7F75MAKA2S3DHDCIC3POR2D2',
    pk='woooahtweevwoopvrrruhfdedadahhhhhhhbeepboopdoopwaaaahwoooooaevvr'
)
```



#### Confirm User

Once you have registered for a user account and have logged in, you will receive a message containg your confirmation code in the email you have provided. Use the code below to confirm your account programatically. You no longer need to input your userId since the Registry API already knows to which account the code is assigned to.

```python
from gameworks.registry import User

response = User.confirm(code='R2D2')
```



#### Resend Code

<span style="color:red">* Authentication Token Required </span>

In the case that your confirmation code hasn't been emailed yet, you may choose to have it resent using the code below:

```python
from gameworks.registry import User

response = User.resend_code(userId=1)
```



#### Player Profile Resource Functionalities

------

#### Register Player Profile

<span style="color:red">* Authentication Token Required </span>

Create a player profile by entering your desired `username` and your `userId` as shown in the code below:

```python
from gameworks.registry import Player

response = Player.register(
    userId=1,
    username='JediMasterXIII'
)
```



#### Find Player Profile

<span style="color:red">* Authentication Token Required </span>

To retrieve your player profile, query for it using the code below:

```python
from gameworks.registry import Player

response = Player.find(
    userId=1
)
```



#### Update Player Profile

<span style="color:red">* Authentication Token Required </span>

Update your `username` by executing the code below:

```python
from gameworks.registry import Player

response = Player.update(
    userId=1,
    username='JediMasterXIX'
)
```



#### Publisher Account Resource Functionalities

------

#### Register Publisher Account

<span style="color:red">* Authentication Token Required </span>

Create a publisher account by executing the code below:

```python
from gameworks.registry import Player

response = Player.register(
    userId=1,
    username='JediMasterXIII'
)
```



#### Find Publisher Profile

<span style="color:red">* Authentication Token Required </span>

To retrieve your publisher profile, query for it using the code below:

```python
from gameworks.registry import Player

response = Publisher.find(
    userId=1
)
```



#### Update Publisher Profile

<span style="color:red">* Authentication Token Required </span>

You may choose to update specific data in your publisher account information. You don't need to fill up all the parameters in the account information. Updatable account parameters include: `userId`, `walletAddress` (wallet address may not be the same as the user account's wallet address), `publisherName `, `description`.

```python
from gameworks.registry import Player

response = Publisher.update(
    userId=1,
    publisherName='The Second Order',
    walletAddress='JABBATHEHUTTBDUR7F75MAKA2S3DHDCIC3POR2D2',
    description='Upon the ashes shall arise the Second Order'
)
```

## License

```LICENSE
The MIT License (MIT)

Copyright (C) 2019 Gameworks.io

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.
```



## Others

Enjoying our platform? Leave us a star on [Github](https://github.com/gameworks-gwx/gameworks-python-sdk) to motivate us to create more services for you ;) .