Skip to content

Commit

Permalink
ref: make Memsource API calls from Kirby fixes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
hdodov committed Aug 19, 2020
1 parent 41264f0 commit d0e2fd8
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 77 deletions.
6 changes: 3 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Kirby\Cms\App;

load([
'Oblik\\Memsource\\Service' => 'Service.php',
'Oblik\\Memsource\\Snapshot' => 'Snapshot.php'
], __DIR__ . '/lib');

Expand Down Expand Up @@ -60,9 +61,8 @@ function walkerSettings($config = [])
],
'api' => [
'routes' => array_merge(
include 'routes/export.php',
include 'routes/import.php',
include 'routes/snapshot.php'
include 'routes/snapshot.php',
include 'routes/export.php'
)
],
'translations' => [
Expand Down
68 changes: 68 additions & 0 deletions lib/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Oblik\Memsource;

use Kirby\Http\Cookie;
use Kirby\Http\Remote;
use Kirby\Http\Response;

class Service
{
public const API_URL = 'https://cloud.memsource.com/web/api2/v1';

public $token = null;

public function __construct()
{
$this->token = Cookie::get('memsource_session');
}

public function login()
{
$remote = Remote::request(self::API_URL . '/auth/login', [
'method' => 'POST',
'data' => json_encode(kirby()->request()->data()),
'headers' => [
'Content-Type' => 'application/json'
]
]);

if ($remote->code() === 200) {
$data = json_decode($remote->content(), true);
$token = $data['token'] ?? null;
$expires = $data['expires'] ?? null;

if ($token) {
Cookie::set('memsource_session', $token, [
'lifetime' => strtotime($expires)
]);
}
}

return new Response($remote->content(), 'application/json', $remote->code());
}

public function get(string $resource)
{
$remote = Remote::request(self::API_URL . '/' . $resource . '?token=' . $this->token, [
'method' => 'GET'
]);

return new Response($remote->content(), 'application/json', $remote->code());
}

public function upload(string $projectId, string $filename)
{
$remote = Remote::request(self::API_URL . '/projects/' . $projectId . '/jobs?token=' . $this->token, [
'method' => 'POST',
'data' => json_encode(kirby()->request()->data()),
'headers' => [
'Memsource' => kirby()->request()->headers()['Memsource'],
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => "filename*=UTF-8''{$filename}"
]
]);

return new Response($remote->content(), 'application/json', $remote->code());
}
}
48 changes: 47 additions & 1 deletion routes/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,49 @@
use Exception;
use Oblik\Outsource\Util\Diff;
use Oblik\Outsource\Walker\Exporter;
use Oblik\Outsource\Walker\Importer;

return [
[
'pattern' => 'export',
'pattern' => 'memsource/login',
'method' => 'POST',
'action' => function () {
return (new Service)->login();
}
],
[
'pattern' => 'memsource/upload/(:any)/(:any)',
'method' => 'POST',
'action' => function ($project, $filename) {
return (new Service)->upload($project, $filename);
}
],
[
'pattern' => 'memsource/import',
'method' => 'POST',
'action' => function () {
if ($config = $_SERVER['HTTP_MEMSOURCE'] ?? null) {
$config = json_decode($config, true);
}

$language = $config['language'] ?? null;
$postData = file_get_contents('php://input');
$content = json_decode($postData, true);

if (empty($language)) {
throw new Exception('Missing language', 400);
}

if (empty($content)) {
throw new Exception('Missing content', 400);
}

$importer = new Importer(walkerSettings());
return $importer->import($content, $language);
}
],
[
'pattern' => 'memsource/export',
'method' => 'GET',
'action' => function () {
$pattern = $_GET['pages'] ?? null;
Expand Down Expand Up @@ -41,5 +80,12 @@

return $data;
}
],
[
'pattern' => 'memsource/(:all)',
'method' => 'GET',
'action' => function ($resource) {
return (new Service)->get($resource);
}
]
];
33 changes: 0 additions & 33 deletions routes/import.php

This file was deleted.

6 changes: 3 additions & 3 deletions routes/snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

return [
[
'pattern' => 'snapshot',
'pattern' => 'memsource/snapshot',
'method' => 'GET',
'action' => function () {
return array_map(function ($file) {
Expand All @@ -18,7 +18,7 @@
}
],
[
'pattern' => 'snapshot',
'pattern' => 'memsource/snapshot',
'method' => 'POST',
'action' => function () {
$lang = kirby()->defaultLanguage()->code();
Expand All @@ -30,7 +30,7 @@
}
],
[
'pattern' => 'snapshot',
'pattern' => 'memsource/snapshot',
'method' => 'DELETE',
'action' => function () {
return Snapshot::remove($_GET['name']);
Expand Down
4 changes: 2 additions & 2 deletions src/components/tabs/service/views/Export.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default {
}
this.$loading(
this.$store.dispatch('outsource', {
this.$store.dispatch('memsource', {
url: '/export',
params
}).then(response => {
Expand All @@ -58,7 +58,7 @@ export default {
},
created () {
this.$loading(
this.$store.dispatch('outsource', {
this.$store.dispatch('memsource', {
url: '/snapshot',
method: 'get'
}).then(response => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/tabs/service/views/Import.vue
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export default {
language: language.code
}
return this.$store.dispatch('outsource', {
return this.$store.dispatch('memsource', {
url: '/import',
method: 'post',
headers: {
Expand Down
6 changes: 2 additions & 4 deletions src/components/tabs/service/views/Upload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,10 @@ export default {
}
return this.$store.dispatch('memsource', {
url: `/projects/${ this.project.uid }/jobs`,
url: `/upload/${ this.project.uid }/${ filename }`,
method: 'post',
headers: {
'Memsource': JSON.stringify(memsourceHeader),
'Content-Type': 'application/octet-stream',
'Content-Disposition': `filename*=UTF-8''${ filename }`
Memsource: JSON.stringify(memsourceHeader),
},
data: this.data
})
Expand Down
6 changes: 3 additions & 3 deletions src/components/tabs/snapshots/Snapshots.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default {
return (new Date(seconds * 1000)).toLocaleString()
},
fetch () {
this.$store.dispatch('outsource', {
this.$store.dispatch('memsource', {
url: '/snapshot',
method: 'get'
}).then(response => {
Expand All @@ -56,7 +56,7 @@ export default {
}).catch(this.$alert)
},
create () {
this.$store.dispatch('outsource', {
this.$store.dispatch('memsource', {
url: '/snapshot',
method: 'post',
params: {
Expand All @@ -67,7 +67,7 @@ export default {
}).catch(this.$alert)
},
remove (name) {
this.$store.dispatch('outsource', {
this.$store.dispatch('memsource', {
url: '/snapshot',
method: 'delete',
params: {
Expand Down
2 changes: 1 addition & 1 deletion src/components/tabs/user/views/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default {
submit () {
this.$loading(
this.$store.dispatch('memsource', {
url: '/auth/login',
url: '/login',
method: 'post',
data: this.credentials
}).then(response => {
Expand Down
29 changes: 3 additions & 26 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ class MemsourceError extends Error {
}
}

class OutsourceError extends Error {
constructor (response) {
super(response.data.message)
this.name = 'OutsourceError'
this.code = response.data.code
this.type = response.data.exception
}
}

function formatRejection (Error) {
return function (error) {
if (error.response && error.response.data) {
Expand Down Expand Up @@ -61,20 +52,9 @@ export default (Vuex, rootStore) => new Vuex.Store({
sourceLanguage: function (state, getters) {
return getters.languages.current.code
},
msClient: function (state, getters, rootState) {
var token = (rootState.session && rootState.session.token)

msClient: function () {
return axios.create({
baseURL: 'https://cloud.memsource.com/web/api2/v1',
method: 'get',
params: {
token: token
}
})
},
exporterClient: () => {
return axios.create({
baseURL: panel.api,
baseURL: panel.api + '/memsource',
method: 'get',
headers: {
'X-CSRF': panel.csrf
Expand Down Expand Up @@ -126,7 +106,7 @@ export default (Vuex, rootStore) => new Vuex.Store({
}

if (!alert.text && alert.error) {
alert.text = `${ alert.error.name }: ${ alert.error.message }`
alert.text = `${alert.error.name}: ${alert.error.message}`
}

state.alerts.push(alert)
Expand All @@ -138,9 +118,6 @@ export default (Vuex, rootStore) => new Vuex.Store({
actions: {
memsource: ({ getters }, payload) => {
return getters.msClient(payload).catch(formatRejection(MemsourceError))
},
outsource: ({ getters }, payload) => {
return getters.exporterClient(payload).catch(formatRejection(OutsourceError))
}
}
})

0 comments on commit d0e2fd8

Please sign in to comment.