### Initializing Appwrite SDK and Pinging Client (JavaScript) Source: https://github.com/appwrite/sdk-generator/blob/master/tests/languages/web/index.html This snippet initializes the Appwrite SDK client and various service modules (Foo, Bar, General). It then sets a project ID and performs a `ping` operation to test connectivity and retrieve basic client information. ```JavaScript Start document.getElementById("start").addEventListener("click", async () => { let response; let responseRealtime = 'Realtime failed!'; // Init SDK const { Client, Foo, Bar, General, Query, Permission, Role, ID, MockType } = Appwrite; const client = new Client(); const foo = new Foo(client); const bar = new Bar(client); const general = new General(client); // Ping client.setProject('123456'); response = await client.ping(); console.log(response.result); ``` -------------------------------- ### Performing CRUD Operations with Appwrite Foo Service (JavaScript) Source: https://github.com/appwrite/sdk-generator/blob/master/tests/languages/web/index.html This snippet demonstrates various HTTP methods (GET, POST, PUT, PATCH, DELETE) using the `Foo` service of the Appwrite SDK. Each operation is performed with sample string and integer parameters, and an array of strings. ```JavaScript // Foo response = await foo.get('string', 123, ["string in array"]); console.log(response.result); response = await foo.post('string', 123, ["string in array"]); console.log(response.result); response = await foo.put('string', 123, ["string in array"]); console.log(response.result); response = await foo.patch('string', 123, ["string in array"]); console.log(response.result); response = await foo.delete('string', 123, ["string in array"]); console.log(response.result); ``` -------------------------------- ### Installing Composer Dependencies via Docker (Windows) Source: https://github.com/appwrite/sdk-generator/blob/master/README.md This Docker command runs Composer to install project dependencies within a temporary container, mounting the current directory to '/app' and ignoring platform requirements. It's specifically designed for Windows environments, using '%cd%' for the current directory. ```bash docker run --rm --interactive --tty --volume "%cd%":/app composer install --ignore-platform-reqs ``` -------------------------------- ### Setting Up Appwrite Realtime Subscription (JavaScript) Source: https://github.com/appwrite/sdk-generator/blob/master/tests/languages/web/index.html This snippet configures the Appwrite client for real-time functionality. It sets the project ID and the real-time endpoint, then subscribes to a 'tests' channel to receive events, storing the payload in `responseRealtime`. ```JavaScript // Realtime setup client.setProject('console'); client.setEndpointRealtime('ws://cloud.appwrite.io/v1'); client.subscribe('tests', event => { responseRealtime = event.payload.response; }); ``` -------------------------------- ### Example Dart Language Test Configuration in PHP Source: https://github.com/appwrite/sdk-generator/blob/master/CONTRIBUTING.md This PHP class provides a concrete example of configuring a test for the Dart SDK. It specifies the Dart language, its SDK class, build commands to prepare the test environment (e.g., copying test files), and Docker commands for running tests against stable and beta Dart versions. It also lists the expected output responses for various test scenarios. ```php 'docker run --rm -v $(pwd):/app -w /app/tests/sdks/dart dart:stable sh -c "dart pub get && dart pub run tests/tests.dart"', 'dart-beta' => 'docker run --rm -v $(pwd):/app -w /app/tests/sdks/dart dart:beta sh -c "dart pub get && dart pub run tests/tests.dart"' ]; protected array $expectedOutput = [ ...Base::FOO_RESPONSES, ...Base::BAR_RESPONSES, ...Base::GENERAL_RESPONSES, ...Base::EXCEPTION_RESPONSES ]; } ``` -------------------------------- ### Interacting with Appwrite General Service (Redirect, Upload, Enum) (JavaScript) Source: https://github.com/appwrite/sdk-generator/blob/master/tests/languages/web/index.html This snippet demonstrates several operations using the `General` service. It includes a `redirect` call, two `upload` calls for files (presumably from HTML input elements), and an `enum` call using a `MockType`. ```JavaScript // General response = await general.redirect(); console.log(response.result); response = await general.upload( "string", 123, ["string in array"], document.getElementById("file").files[0] ); console.log(response.result); response = await general.upload( "string", 123, ["string in array"], document.getElementById("file2").files[0] ); console.log(response.result); console.log('POST:/v1/mock/tests/general/upload:passed'); // Skip InputFile tests console.log('POST:/v1/mock/tests/general/upload:passed'); // Skip InputFile tests response = await general.enum(MockType.First); console.log(response.result); ``` -------------------------------- ### Performing CRUD Operations with Appwrite Bar Service (JavaScript) Source: https://github.com/appwrite/sdk-generator/blob/master/tests/languages/web/index.html This snippet showcases various HTTP methods (GET, POST, PUT, PATCH, DELETE) using the `Bar` service of the Appwrite SDK. Similar to the Foo service, it uses sample string and integer parameters, and an array of strings. ```JavaScript // Bar response = await bar.get("string", 123, ["string in array"]); console.log(response.result); response = await bar.post("string", 123, ["string in array"]); console.log(response.result); response = await bar.put("string", 123, ["string in array"]); console.log(response.result); response = await bar.patch("string", 123, ["string in array"]); console.log(response.result); response = await bar.delete("string", 123, ["string in array"]); console.log(response.result); ``` -------------------------------- ### Executing SDK Generation Example via Docker (Bash) Source: https://github.com/appwrite/sdk-generator/blob/master/CONTRIBUTING.md This Docker command executes the `example.php` script within a PHP 8.3 CLI container. It mounts the current directory as a volume and sets it as the working directory, allowing the script to access the necessary files and generate the SDK output. This is used to test the SDK generation process for a new language. ```bash docker run --rm -v $(pwd):/app -w /app php:8.3-cli php example.php ``` -------------------------------- ### Using Appwrite Query Helper Methods (JavaScript) Source: https://github.com/appwrite/sdk-generator/blob/master/tests/languages/web/index.html This snippet demonstrates various static methods provided by the `Query` helper for constructing database queries. It covers equality, inequality, range, search, null checks, string operations, selection, ordering, cursor-based pagination, limits, offsets, and logical `OR`/`AND` conditions. ```JavaScript // Query helper tests console.log(Query.equal("released", [true])); console.log(Query.equal("title", ["Spiderman", "Dr. Strange"])); console.log(Query.notEqual("title", "Spiderman")); console.log(Query.lessThan("releasedYear", 1990)); console.log(Query.greaterThan("releasedYear", 1990)); console.log(Query.search("name", "john")); console.log(Query.isNull("name")); console.log(Query.isNotNull("name")); console.log(Query.between("age", 50, 100)); console.log(Query.between("age", 50.5, 100.5)); console.log(Query.between("name", "Anna", "Brad")); console.log(Query.startsWith("name", "Ann")); console.log(Query.endsWith("name", "nne")); console.log(Query.select(["name", "age"])); console.log(Query.orderAsc("title")); console.log(Query.orderDesc("title")); console.log(Query.cursorAfter("my_movie_id")); console.log(Query.cursorBefore("my_movie_id")); console.log(Query.limit(50)); console.log(Query.offset(20)); console.log(Query.contains("title", "Spider")); console.log(Query.contains("labels", "first")); console.log(Query.or([ Query.equal("released", true), Query.lessThan("releasedYear", 1990) ])); console.log(Query.and([ Query.equal("released", false), Query.greaterThan("releasedYear", 2015) ])); ``` -------------------------------- ### Installing Composer Dependencies via Docker (Windows Bash) Source: https://github.com/appwrite/sdk-generator/blob/master/CONTRIBUTING.md This Docker command facilitates Composer dependency updates on Windows systems. It uses the `%cd%` environment variable to mount the current directory into the container, ensuring dependencies are installed correctly within the isolated Docker environment, similar to the UNIX approach. ```bash docker run --rm --interactive --tty --volume "%cd%":/app composer update --ignore-platform-reqs --optimize-autoloader --no-plugins --no-scripts --prefer-dist ``` -------------------------------- ### Installing Composer Dependencies via Docker (UNIX) Source: https://github.com/appwrite/sdk-generator/blob/master/README.md This Docker command runs Composer to install project dependencies within a temporary container, mounting the current directory to '/app' and ignoring platform requirements. It's specifically designed for UNIX-like operating systems to ensure consistent dependency management. ```bash docker run --rm --interactive --tty --volume "$(pwd)":/app composer install --ignore-platform-reqs ``` -------------------------------- ### Using Appwrite Permission and Role Helpers (JavaScript) Source: https://github.com/appwrite/sdk-generator/blob/master/tests/languages/web/index.html This snippet demonstrates how to construct permission strings using the `Permission` and `Role` helpers. It covers various roles like `any`, `user`, `users`, `guests`, `team`, `member`, and `label`, combined with different access types (read, write, create, update, delete). ```JavaScript // Permission & Role helper tests console.log(Permission.read(Role.any())); console.log(Permission.write(Role.user(ID.custom('userid')))); console.log(Permission.create(Role.users())); console.log(Permission.update(Role.guests())); console.log(Permission.delete(Role.team('teamId', 'owner'))); console.log(Permission.delete(Role.team('teamId'))); console.log(Permission.create(Role.member('memberId'))); console.log(Permission.update(Role.users('verified'))); console.log(Permission.update(Role.user(ID.custom('userid'), 'unverified'))); console.log(Permission.create(Role.label('admin'))); ``` -------------------------------- ### Installing Composer Dependencies via CLI (Bash) Source: https://github.com/appwrite/sdk-generator/blob/master/CONTRIBUTING.md This command updates Composer dependencies for the SDK generator project. It ignores platform requirements, optimizes the autoloader, disables plugins and scripts, and prefers distribution archives for faster installation. This is a common step for setting up the development environment. ```bash composer update --ignore-platform-reqs --optimize-autoloader --no-plugins --no-scripts --prefer-dist ``` -------------------------------- ### Using Appwrite ID Helper Methods (JavaScript) Source: https://github.com/appwrite/sdk-generator/blob/master/tests/languages/web/index.html This snippet demonstrates the `ID` helper for generating unique IDs or using custom IDs. It shows how to generate a unique ID and how to specify a custom ID string. ```JavaScript // ID helper tests console.log(ID.unique()); console.log(ID.custom('custom_id')); ``` -------------------------------- ### Appwrite General Service Headers and Realtime Final Check (JavaScript) Source: https://github.com/appwrite/sdk-generator/blob/master/tests/languages/web/index.html This snippet calls the `headers` method on the `General` service to test header handling. It also includes a delay and a final log of the `responseRealtime` variable, which was populated by the real-time subscription, to confirm real-time event reception. ```JavaScript response = await general.headers(); console.log(response.result); const delay = ms => new Promise(res => setTimeout(res, ms)); await delay(5000); console.log(responseRealtime); }); ``` -------------------------------- ### Handling Errors with Appwrite General Service (JavaScript) Source: https://github.com/appwrite/sdk-generator/blob/master/tests/languages/web/index.html This snippet demonstrates error handling for various HTTP error codes (400, 500, 502) and an invalid endpoint setting using `try-catch` blocks with the `General` service. It logs the error message and response. ```JavaScript try { response = await general.empty(); } catch (error) { console.log(error); } try { response = await general.error400(); } catch (error) { console.log(error.message); console.log(error.response); } try { response = await general.error500(); } catch (error) { console.log(error.message); console.log(error.response); } try { response = await general.error502(); } catch (error) { console.log(error.message); console.log(error.response); } try { client.setEndpoint("htp://cloud.appwrite.io/v1"); } catch (error) { console.log(error.message); } ``` -------------------------------- ### Installing Composer Dependencies via Docker (UNIX Bash) Source: https://github.com/appwrite/sdk-generator/blob/master/CONTRIBUTING.md This Docker command runs Composer to update dependencies within a container for UNIX-like systems. It mounts the current working directory as a volume, removes the container after execution, and performs the same optimized Composer update as the CLI method, providing an isolated environment. ```bash docker run --rm --interactive --tty --volume "$(pwd)":/app composer update --ignore-platform-reqs --optimize-autoloader --no-plugins --no-scripts --prefer-dist ``` -------------------------------- ### Generating SDK for New Language (PHP) Source: https://github.com/appwrite/sdk-generator/blob/master/CONTRIBUTING.md This PHP snippet demonstrates how to integrate and test a newly created language class (`NewLang`) with the Appwrite SDK generator. It fetches the OpenAPI specification, initializes the SDK with the new language, sets various SDK properties like logo and license, and then generates the SDK files into a specified directory. The `getSSLPage` function is a helper to fetch the OpenAPI spec. ```php use Appwrite\Spec\Swagger2; use Appwrite\SDK\SDK; use Appwrite\SDK\Language\NewLang; function getSSLPage($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); return $result; } $spec = getSSLPage('https://appwrite.io/v1/open-api-2.json?extensions=1'); // NewLang $sdk = new SDK(new NewLang(), new Swagger2($spec)); $sdk ->setLogo('https://appwrite.io/v1/images/console.png') ->setLicenseContent('test test test') ->setWarning('**WORK IN PROGRESS - NOT READY FOR USAGE**') ; $sdk->generate(__DIR__ . '/examples/new-lang'); ``` -------------------------------- ### Generating Appwrite SDK with PHP Source: https://github.com/appwrite/sdk-generator/blob/master/README.md This PHP script demonstrates the process of generating an SDK using the Appwrite SDK Generator. It involves loading an API specification (Swagger 2), creating a language instance (PHP), setting language-specific options like Composer package details, configuring SDK metadata, and finally generating the source code into a specified output directory. ```php setComposerPackage('my-api') ->setComposerVendor('my-company') ; // Create the SDK object with the language and spec instances $sdk = new SDK($lang, $spec); $sdk ->setLogo('https://appwrite.io/v1/images/console.png') ->setLicenseContent('License content here.') ->setVersion('v1.1.0') ; $sdk->generate(__DIR__ . '/examples/php'); // Generate source code ``` -------------------------------- ### Running Appwrite SDK Tests with Docker and PHPUnit Source: https://github.com/appwrite/sdk-generator/blob/master/CONTRIBUTING.md This shell command executes the Appwrite SDK tests using Docker. It mounts the current directory, the Docker socket, and runs PHPUnit within a `php:8.3-cli-alpine` container. This command is used to initiate the entire test suite, leveraging Docker for consistent and isolated test environments. ```sh docker run --rm -v $(pwd):$(pwd):rw -w $(pwd) -v /var/run/docker.sock:/var/run/docker.sock php:8.3-cli-alpine sh -c "apk add docker-cli && vendor/bin/phpunit" ``` -------------------------------- ### Defining a Generic Language Test Class in PHP Source: https://github.com/appwrite/sdk-generator/blob/master/CONTRIBUTING.md This PHP class serves as a template for defining language-specific tests within the Appwrite SDK Generator. It extends `Base` and requires setting the language name, SDK class, build commands, environment commands for different language versions, and expected output responses. This structure facilitates cross-platform testing by standardizing test execution. ```php