Skip to content

Commit

Permalink
Merge pull request #1 from iroegbu/master
Browse files Browse the repository at this point in the history
Updated to use Azure and included examples
  • Loading branch information
iroegbu authored Oct 18, 2017
2 parents e3085b5 + 2b76323 commit 8e8f661
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 40 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@

Asynchronous [Microsoft Translator API](https://www.microsoft.com/en-us/translator/translatorapi.aspx) client built on
the [amp](https://github.com/amphp/amp) concurrency framework.

## Usage

See examples
138 changes: 110 additions & 28 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions examples/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace DaveRandom\AsyncMicrosoftTranslate\Examples;

use DaveRandom\AsyncMicrosoftTranslate\Client;
use DaveRandom\AsyncMicrosoftTranslate\Credentials;
use Amp\Artax\Client as HttpClient;
use function Amp\resolve;

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

$credentials = new Credentials(client_id, azure_subscription_key);

$client = new Client(new HttpClient());

function token($client, $credentials)
{
return yield $client->getAccessToken($credentials);
}

\Amp\run(function () use ($client, $credentials) {
//text to be translated
$text = 'Alles ist entweder eine Kartoffel oder nicht';

//access token - note that token has lifespan
$token = yield $client->getAccessToken($credentials);

//get list of supported languages
$langs = yield $client->getSupportedLanguages($token);

//detect text language
$lang = yield $client->detectLanguage($token, $text);

//get translation using token
$trans = yield $client->getTranslation($token, $text, 'en', $lang);
echo $trans;
});
18 changes: 7 additions & 11 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,23 @@ private function callApiPostMethod(string $accessToken, string $method, \DOMDocu

private function doGetAccessToken(Credentials $credentials)
{
$body = (new FormBody)
->addField('grant_type', 'client_credentials')
->addField('scope', BASE_URL)
->addField('client_id', $credentials->getClientId())
->addField('client_secret', $credentials->getClientSecret());

$request = (new HttpRequest)
->setMethod('POST')
->setUri(AUTH_URL)
->setBody($body);
->setHeader('Content-Type', 'application/json')
->setHeader('Accept', 'application/jwt')
->setHeader('Ocp-Apim-Subscription-Key', $credentials->getClientSecret());

/** @var HttpResponse $response */
$response = yield $this->httpClient->request($request);

$decoded = json_try_decode($response->getBody());
$responseBody = $response->getBody();

if (!empty($decoded->error)){
throw new \RuntimeException($decoded->error_description);
if ($response->getStatus() !== 200) {
throw new \RuntimeException(json_try_decode($responseBody)->message);
}

return $decoded->access_token;
return $responseBody;
}

private function doGetSupportedLanguages(string $accessToken, string $locale)
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DaveRandom\AsyncMicrosoftTranslate;

const AUTH_URL = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/';
const AUTH_URL = 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken';
const BASE_URL = 'http://api.microsofttranslator.com';
const SERVICE_URL = BASE_URL . '/V2/Http.svc';

Expand Down

0 comments on commit 8e8f661

Please sign in to comment.