Skip to content

Commit b67a12e

Browse files
committed
Extend GraphQL and REST from an HTTP base class
1 parent 7c45e1c commit b67a12e

File tree

4 files changed

+159
-189
lines changed

4 files changed

+159
-189
lines changed

example.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Appwrite\SDK\Language\Dart;
1515
use Appwrite\SDK\Language\Go;
1616
use Appwrite\SDK\Language\Deno;
17-
use Appwrite\SDK\Language\HTTP;
17+
use Appwrite\SDK\Language\REST;
1818
use Appwrite\SDK\Language\Swift;
1919
use Appwrite\SDK\Language\SwiftClient;
2020
use Appwrite\SDK\Language\DotNet;
@@ -389,8 +389,8 @@ function getSSLPage($url) {
389389

390390
$sdk->generate(__DIR__ . '/examples/dotnet');
391391

392-
// HTTP
393-
$sdk = new SDK(new HTTP(), new Swagger2($spec));
392+
// REST
393+
$sdk = new SDK(new REST(), new Swagger2($spec));
394394

395395
$sdk
396396
->setName('NAME')

src/SDK/Language/GraphQL.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Appwrite\SDK\Language;
44

5-
use Appwrite\SDK\Language;
6-
7-
class GraphQL extends Language
5+
class GraphQL extends HTTP
86
{
97
/**
108
* @return string
@@ -14,24 +12,6 @@ public function getName(): string
1412
return 'GraphQL';
1513
}
1614

17-
/**
18-
* Get Language Keywords List
19-
*
20-
* @return array
21-
*/
22-
public function getKeywords(): array
23-
{
24-
return [];
25-
}
26-
27-
/**
28-
* @return array
29-
*/
30-
public function getIdentifierOverrides(): array
31-
{
32-
return [];
33-
}
34-
3515
/**
3616
* @param $type
3717
* @return string

src/SDK/Language/HTTP.php

Lines changed: 6 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,10 @@
33
namespace Appwrite\SDK\Language;
44

55
use Appwrite\SDK\Language;
6+
use Exception;
67

7-
class HTTP extends Language
8+
abstract class HTTP extends Language
89
{
9-
/**
10-
* @return string
11-
*/
12-
public function getName(): string
13-
{
14-
return 'HTTP';
15-
}
16-
1710
/**
1811
* Get Language Keywords List
1912
*
@@ -33,164 +26,12 @@ public function getIdentifierOverrides(): array
3326
}
3427

3528
/**
36-
* @param $type
29+
* @param array $parameter
3730
* @return string
31+
* @throws Exception
3832
*/
3933
public function getTypeName(array $parameter): string
4034
{
41-
switch ($parameter['type']) {
42-
case self::TYPE_INTEGER:
43-
return 'int';
44-
case self::TYPE_STRING:
45-
return 'String';
46-
case self::TYPE_FILE:
47-
return 'MultipartFile';
48-
case self::TYPE_BOOLEAN:
49-
return 'bool';
50-
case self::TYPE_ARRAY:
51-
if (!empty($parameter['array']['type'])) {
52-
return 'List<' . $this->getTypeName($parameter['array']) . '>';
53-
}
54-
return 'List';
55-
case self::TYPE_OBJECT:
56-
return 'dynamic';
57-
}
58-
59-
return $parameter['type'];
60-
}
61-
62-
/**
63-
* @param array $param
64-
* @return string
65-
*/
66-
public function getParamDefault(array $param): string
67-
{
68-
$type = $param['type'] ?? '';
69-
$default = $param['default'] ?? '';
70-
$required = $param['required'] ?? '';
71-
72-
if ($required) {
73-
return '';
74-
}
75-
76-
$output = '';
77-
78-
if (empty($default) && $default !== 0 && $default !== false) {
79-
switch ($type) {
80-
case self::TYPE_OBJECT:
81-
$output .= '{}';
82-
break;
83-
case self::TYPE_NUMBER:
84-
case self::TYPE_INTEGER:
85-
$output .= '0';
86-
break;
87-
case self::TYPE_BOOLEAN:
88-
$output .= 'false';
89-
break;
90-
case self::TYPE_ARRAY:
91-
$output .= '[]';
92-
break;
93-
case self::TYPE_STRING:
94-
$output .= '""';
95-
break;
96-
}
97-
} else {
98-
switch ($type) {
99-
case self::TYPE_OBJECT:
100-
case self::TYPE_NUMBER:
101-
case self::TYPE_ARRAY:
102-
case self::TYPE_INTEGER:
103-
$output .= $default;
104-
break;
105-
case self::TYPE_BOOLEAN:
106-
$output .= ($default) ? 'true' : 'false';
107-
break;
108-
case self::TYPE_STRING:
109-
$output .= '"' . $default . '"';
110-
break;
111-
}
112-
}
113-
114-
return $output;
115-
}
116-
117-
/**
118-
* @param array $param
119-
* @return string
120-
*/
121-
public function getParamExample(array $param): string
122-
{
123-
$type = $param['type'] ?? '';
124-
$example = $param['example'] ?? '';
125-
126-
$output = '';
127-
128-
if (empty($example) && $example !== 0 && $example !== false) {
129-
switch ($type) {
130-
case self::TYPE_FILE:
131-
$output .= 'cf 94 84 24 8d c4 91 10 0f dc 54 26 6c 8e 4b bc
132-
e8 ee 55 94 29 e7 94 89 19 26 28 01 26 29 3f 16...';
133-
break;
134-
case self::TYPE_NUMBER:
135-
case self::TYPE_INTEGER:
136-
$output .= '0';
137-
break;
138-
case self::TYPE_BOOLEAN:
139-
$output .= 'false';
140-
break;
141-
case self::TYPE_STRING:
142-
$output .= "";
143-
break;
144-
case self::TYPE_OBJECT:
145-
$output .= '{}';
146-
break;
147-
case self::TYPE_ARRAY:
148-
$output .= '[]';
149-
break;
150-
}
151-
} else {
152-
switch ($type) {
153-
case self::TYPE_OBJECT:
154-
case self::TYPE_FILE:
155-
case self::TYPE_NUMBER:
156-
case self::TYPE_INTEGER:
157-
$output .= $example;
158-
break;
159-
case self::TYPE_ARRAY:
160-
// If array of strings, make sure any sub-strings are escaped
161-
if (\substr($example, 1, 1) === '"') {
162-
$start = \substr($example, 0, 2);
163-
$end = \substr($example, -2);
164-
$contents = \substr($example, 2, -2);
165-
$contents = \addslashes($contents);
166-
$output .= $start . $contents . $end;
167-
} else {
168-
$output .= $example;
169-
}
170-
break;
171-
case self::TYPE_STRING:
172-
$output .= '"' . \addslashes($example) . '"';
173-
break;
174-
case self::TYPE_BOOLEAN:
175-
$output .= ($example) ? 'true' : 'false';
176-
break;
177-
}
178-
}
179-
180-
return $output;
181-
}
182-
183-
/**
184-
* @return array
185-
*/
186-
public function getFiles(): array
187-
{
188-
return [
189-
[
190-
'scope' => 'method',
191-
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}',
192-
'template' => '/http/docs/example.md.twig',
193-
],
194-
];
35+
throw new Exception('Method not supported for HTTP APIs');
19536
}
196-
}
37+
}

src/SDK/Language/REST.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
namespace Appwrite\SDK\Language;
4+
5+
class REST extends HTTP
6+
{
7+
/**
8+
* @return string
9+
*/
10+
public function getName(): string
11+
{
12+
return 'REST';
13+
}
14+
15+
/**
16+
* @param array $param
17+
* @return string
18+
*/
19+
public function getParamDefault(array $param): string
20+
{
21+
$type = $param['type'] ?? '';
22+
$default = $param['default'] ?? '';
23+
$required = $param['required'] ?? '';
24+
25+
if ($required) {
26+
return '';
27+
}
28+
29+
$output = '';
30+
31+
if (empty($default) && $default !== 0 && $default !== false) {
32+
switch ($type) {
33+
case self::TYPE_OBJECT:
34+
$output .= '{}';
35+
break;
36+
case self::TYPE_NUMBER:
37+
case self::TYPE_INTEGER:
38+
$output .= '0';
39+
break;
40+
case self::TYPE_BOOLEAN:
41+
$output .= 'false';
42+
break;
43+
case self::TYPE_ARRAY:
44+
$output .= '[]';
45+
break;
46+
case self::TYPE_STRING:
47+
$output .= '""';
48+
break;
49+
}
50+
} else {
51+
switch ($type) {
52+
case self::TYPE_OBJECT:
53+
case self::TYPE_NUMBER:
54+
case self::TYPE_ARRAY:
55+
case self::TYPE_INTEGER:
56+
$output .= $default;
57+
break;
58+
case self::TYPE_BOOLEAN:
59+
$output .= ($default) ? 'true' : 'false';
60+
break;
61+
case self::TYPE_STRING:
62+
$output .= '"' . $default . '"';
63+
break;
64+
}
65+
}
66+
67+
return $output;
68+
}
69+
70+
/**
71+
* @param array $param
72+
* @return string
73+
*/
74+
public function getParamExample(array $param): string
75+
{
76+
$type = $param['type'] ?? '';
77+
$example = $param['example'] ?? '';
78+
79+
$output = '';
80+
81+
if (empty($example) && $example !== 0 && $example !== false) {
82+
switch ($type) {
83+
case self::TYPE_FILE:
84+
$output .= 'cf 94 84 24 8d c4 91 10 0f dc 54 26 6c 8e 4b bc
85+
e8 ee 55 94 29 e7 94 89 19 26 28 01 26 29 3f 16...';
86+
break;
87+
case self::TYPE_NUMBER:
88+
case self::TYPE_INTEGER:
89+
$output .= '0';
90+
break;
91+
case self::TYPE_BOOLEAN:
92+
$output .= 'false';
93+
break;
94+
case self::TYPE_STRING:
95+
$output .= "";
96+
break;
97+
case self::TYPE_OBJECT:
98+
$output .= '{}';
99+
break;
100+
case self::TYPE_ARRAY:
101+
$output .= '[]';
102+
break;
103+
}
104+
} else {
105+
switch ($type) {
106+
case self::TYPE_OBJECT:
107+
case self::TYPE_FILE:
108+
case self::TYPE_NUMBER:
109+
case self::TYPE_INTEGER:
110+
$output .= $example;
111+
break;
112+
case self::TYPE_ARRAY:
113+
// If array of strings, make sure any sub-strings are escaped
114+
if (\substr($example, 1, 1) === '"') {
115+
$start = \substr($example, 0, 2);
116+
$end = \substr($example, -2);
117+
$contents = \substr($example, 2, -2);
118+
$contents = \addslashes($contents);
119+
$output .= $start . $contents . $end;
120+
} else {
121+
$output .= $example;
122+
}
123+
break;
124+
case self::TYPE_STRING:
125+
$output .= '"' . \addslashes($example) . '"';
126+
break;
127+
case self::TYPE_BOOLEAN:
128+
$output .= ($example) ? 'true' : 'false';
129+
break;
130+
}
131+
}
132+
133+
return $output;
134+
}
135+
136+
/**
137+
* @return array
138+
*/
139+
public function getFiles(): array
140+
{
141+
return [
142+
[
143+
'scope' => 'method',
144+
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}',
145+
'template' => '/http/docs/example.md.twig',
146+
],
147+
];
148+
}
149+
}

0 commit comments

Comments
 (0)