Language support

Feel free to check out any available OAuth clients for your favourite language to query the API. You can also use the following code to initialize and use Traity's API.

In general, to query the API, you'll need to initialize it with your APP_KEY and APP_SECRET, then call any of the available endpoints.

Additionally, the method to generate widget signatures is included in each language.

PHP

<?php
// Install required PHP OAuth2 client using composer:
// composer require adoy/oauth2:dev-master
require 'vendor/autoload.php';
use \Firebase\JWT\JWT;

const APP_KEY    = 'your app key';
const APP_SECRET = 'your app secret';
const TRAITY_API = 'https://trustapi.traity.com';

$api = new OAuth2\Client(APP_KEY, APP_SECRET);
$api->setAccessToken($token); // User's access token
$api->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_BEARER);

function signature($app_key, $app_secret, $current_user_id, $options = array()) {
  $payload = array_merge(array(
    'time' => time(),
    'current_user_id' => $current_user_id), $options);
  $signature = array(
    'key' => $app_key,
    'payload' => JWT::encode($payload, $app_secret));
  return JWT::encode($signature, '');
}

// API sample call
$api->fetch(TRAITY_API.'/1.0/transactions/4hd276hd2f');
%>

Ruby

# Requires oauth2 gem:
# gem install oauth2
require 'oauth2'
require 'jwt'

class API
  def initialize(key, secret, token, endpoint)
    client = OAuth2::Client.new(key, secret, site: endpoint)
    @token = OAuth2::AccessToken.new(client, token)
  end
  def request(verb, path, params={})
    body = @token.request(verb, path, body: params.to_json,
           headers: {'Content-Type' => 'application/json'}).body
    JSON.parse(body, symbolize_names: true)
  end
end

def signature(key, secret, current_user_id, options = {})
  payload = {
    time: Time.now.to_i,
    current_user_id: current_user_id }.merge(options)
  signature = {key: key, payload: JWT.encode(payload, secret, 'HS256')}
  JWT.encode(signature, nil, false)
end

api = API.new(APP_KEY, APP_SECRET, ACCESS_TOKEN, 'https://trustapi.traity.com')

# API sample call
api.request(:get, "/1.0/transactions/4hd276hd2f")

Python

from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import BackendApplicationClient
import time
import jwt

class API:
    def __init__(self, key, token, endpoint):
        self.endpoint = endpoint
        self.session = OAuth2Session(
            key,
            client=BackendApplicationClient(key))
        self.session['oauth_token'] = token

    def request(self, method, path, **kwargs):
        return self.session.request(method,
            self.endpoint + path, **kwargs).json()

def signature(key, secret, current_user_id, options={}):
  payload   = {
    'time': int(time.time()),
    'current_user_id': current_user_id
  }.copy()
  payload.update(options)
  signature = {
    'key': key,
    'payload': jwt.encode(payload, secret, algorithm='HS256')
  }
  return jwt.encode(signature, '')

api = API(API_KEY, ACCESS_TOKEN, 'https://trustapi.traity.com')

# API sample call
api.request('GET', '/1.0/transactions/4hd276hd2f')

C#

using System;
using System.Text;
using System.Net;
using Newtonsoft.Json.Linq;
using System.IO;

public class API {
    private string endpoint;
    private string token;

    public API(string token, string endpoint) {
        this.token = token
        this.endpoint = endpoint;
    }

    public string Request(string method, string path,
        string json = null) {
        WebRequest request = WebRequest.Create(endpoint + path);
        request.Headers.Add("Authorization", "Bearer " + token);
        request.Method = method;
        if (json != null) {
            request.ContentType = "application/json";
            using (var streamWriter = new StreamWriter(request.
            GetRequestStream())) {
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }
        }
        var response = (HttpWebResponse) request.GetResponse();
        using (var streamReader = new StreamReader(response.
        GetResponseStream())) {
            return streamReader.ReadToEnd();
        }
    }
}

public static string Signature(string key, string secret,
        string currentUserId) {
    int time = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).
        TotalSeconds;
    string payload = currentUserId + "-" + time + "-" + secret;
    string signature = currentUserId + "-" + time + "-" + key + "-" + 
        GetSHA256Hash(payload);
    return signature;
}
public static string GetSHA256Hash(string input) {
    SHA256Managed crypt = new SHA256Managed();
    string hash = String.Empty;
    byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(input), 0,
        Encoding.UTF8.GetByteCount(input));
    foreach (byte bit in crypto) {
        hash += bit.ToString("x2");
    }
    return hash;
}

API api = new API(ACCESS_TOKEN, "https://trustapi.traity.com");

// API sample call
api.Request("GET", "/1.0/transactions/4hd276hd2f");