Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
inverse committed Jul 25, 2018
0 parents commit 0eb417c
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor/
/composer.lock
/.idea/
/phpunit.xml
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# API Client for Tuya

An API Client build for [Tuya][1] heavily inspired by [tuyapy][0].

[0]: https://pypi.org/project/tuyapy
[1]: https://www.tuya.com/
28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "inverse/tuya-client",
"description": "API client for Tuya",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Malachi Soord",
"email": "[email protected]"
}
],
"require": {
"php": "^7.1",
"guzzlehttp/guzzle": "^6.3",
"webmozart/assert": "^1.3",
"phpunit/phpunit": "^7.2"
},
"autoload": {
"psr-4": {
"Inverse\\TuyaClient\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\Inverse\\TuyaClient\\": "tests/"
}
}
}
19 changes: 19 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
syntaxCheck="true"
bootstrap="./tests/bootstrap.php"
>
<php>
<env name="TUYA_USERNAME" value=""/>
<env name="TUYA_PASSWORD" value="" />
<env name="TUYA_COUNTRYCODE" value="" />
</php>
</phpunit>
54 changes: 54 additions & 0 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Inverse\TuyaClient;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\Psr7\UriResolver;
use Psr\Http\Message\UriInterface;

class ApiClient
{
private const BASE_URL_FORMAT = 'https://px1.tuya%s.com';

/**
* @var Client
*/
private $client;

/**
* @var Session
*/
private $session;

public function __construct(Session $session, Client $client = null)
{
$this->session = $session;
$this->client = $client ?? new Client();
}

public function getAccessToken()
{
$baseUrl = $this->getBaseUrl($this->session);

$uri = UriResolver::resolve($baseUrl, new Uri('homeassistant/auth.do'));

$response = $this->client->request('POST', $uri, [
'form_params' => [
'userName' => $this->session->getUsername(),
'password' => $this->session->getPassword(),
'countryCode' => $this->session->getCountryCode(),
'from' => 'tuya'
]
]);

$response = json_decode((string)$response->getBody(), true);

return $response;
}

private function getBaseUrl(Session $session): UriInterface
{
return new Uri(sprintf(self::BASE_URL_FORMAT, $session->getRegion()->getValue()));
}
}
31 changes: 31 additions & 0 deletions src/Region.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Inverse\TuyaClient;

use Webmozart\Assert\Assert;

class Region
{
const CN = 'cn';
const EU = 'eu';
const US = 'us';

const VALID_REGIONS = [
self::CN,
self::EU,
self::US,
];

private $value;

public function __construct(string $value)
{
Assert::oneOf($value, self::VALID_REGIONS, '%s is not a valid region');
$this->value = $value;
}

public function getValue(): string
{
return $this->value;
}
}
54 changes: 54 additions & 0 deletions src/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Inverse\TuyaClient;

class Session
{
/**
* @var string
*/
private $username;

/**
* @var string
*/
private $password;

/**
* @var int
*/
private $countryCode;

/**
* @var Region
*/
private $region;

public function __construct(string $username, string $password, int $countryCode)
{
$this->username = $username;
$this->password = $password;
$this->countryCode = $countryCode;
$this->region = new Region(Region::US);
}

public function getUsername(): string
{
return $this->username;
}

public function getPassword(): string
{
return $this->password;
}

public function getCountryCode(): int
{
return $this->countryCode;
}

public function getRegion(): Region
{
return $this->region;
}
}
17 changes: 17 additions & 0 deletions tests/ApiClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tests\Inverse\TuyaClient;

use Inverse\TuyaClient\Session;
use PHPUnit\Framework\TestCase;

class ApiClientTest extends TestCase
{
private function getSessionFromEnv(): Session
{
$username = getenv('TUYA_USERNAME');
$password = getenv('TUYA_PASSWORD');
$countryCode = getenv('TUYA_COUNTRYCODE');
return new Session($username, $password, $countryCode);
}
}
4 changes: 4 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

require __DIR__.'/../vendor/autoload.php';

0 comments on commit 0eb417c

Please sign in to comment.