Skip to content

Latest commit

 

History

History
189 lines (150 loc) · 4.65 KB

File metadata and controls

189 lines (150 loc) · 4.65 KB

api_gen client

Execute api_gen client for the same root directory as api_gen server. The library will generated in the current working directory.

Usage:
  api_gen client [command]

Available Commands:
  dart        Generate Dart client library
  go          Generate go client library
  typescript  Generate TypeScript client library

Go

This command generates client library.

Usage:
  api_gen client go [path] [flags]

Flags:
  -h, --help             help for go
  -o, --output string    The directory to generated client library in (default "./")
  -p, --package string   The package name of the generated library (default "client")

Example

$ api_gen client go -o ./client/go ./interfaces

Sample code

package main

import (
	"fmt"
	"net/http"

	client "example-code/frontend/go"
	_api_v1_school "example-code/frontend/go/classes/api/v1/school"
)

func main() {
	api_client := client.NewClient(http.Client{}, "http://localhost:8080")
	req := _api_v1_school.PostUpdateUserRequest{ID: "4423"}

	res, err := api_client.Api.V1.School.PostUpdateUser(&req)
	if err != nil {
		fmt.Printf("error occurred: %+v\n", err)
		return
	}

	fmt.Println(*res)
}

Dart

This command generates client library.

Usage:
  api_gen client dart [path] [flags]

Aliases:
  dart, flutter

Flags:
  -o, --file string   The directory to generate client library in (default "./")
  -h, --help          help for dart

Example

$  api_gen client go -o ./client/dart ./interfaces

Sample code

import 'dart:io';

import "./api_client.dart";
import 'package:http/http.dart' as http;
import './classes/api/v1/school/types.dart' as types__api_v_1_school;

main() async {
    final http_client = http.Client();
    final api_client = ApiClient(
      'http://localhost:8080',
      {
        "Content-Type": "application/json"
      },
      http_client
    );

    final request = api_client.getRequestObject({}, [], true);
    final feature_response = api_client.v1.school.postUpdateUser(
      types__api_v_1_school.PostUpdateUserRequest(id: "lain")
    );

    print(await feature_response);
}

TypeScript

The library uses TypeScript+fetch API

About develop

To support browsers that do not support fetch, use Polyfill. When introducing eslint etc., it is recommended to add automatic generation to ignore.

About mock server

The api_gen server supports mock servers. See the api_gen server documentation for details on how to return the mock data.
Requests to the mock server can be made from the library generated by api_gen client. However, several options exist.These are JSON encoded in the Api-Gen-Option header and sent to the mock server.
You can specify options by passing 'mock_option' as an option argument in the code generated by api_gen client`.
The options are details below.

{
    wait_ms: 10,           // Delay the response from the mock server for the specified millisecond。 (example: 10ms)
    target_file: 'error'   // Fixes the json file to be referenced by the mock server. You can omit the json file extension. (example: error.json)
}

About generate

In templates, the library can be generated with make generate in templates/frontend.

The following files are generated:

  • api/
    • api_client.ts
    • classes/
      • {HTTP Method}{Endpoint Name}{Request/Response}.ts

Example implementation:

import { PostCreateUserRequest } from "./api/classes/PostCreateUserRequest";
import { APIClient } from "./api/api_client";

// the simplest
(async () => {
    const client = new APIClient();

    const resp = await client.postCreateUser(new PostCreateUserRequest({
        ID: "id",
        Password: "password",
        Gender: 0,
    }));

    console.log(resp);
})();

// with options
(async () => {
    const client = new APIClient(
        "pass", // [optional] token for Authorization: Bearer
        {
            "X-Foobar": "foobar", // [optional] custom headers
        },
        "http://localhost:8080",  // [optional] custom endpoint
        {
            cache: "no-cache", // [optional] options for fetch API
        },
    );

    const resp = await client.postCreateUser(
        new PostCreateUserRequest({
            ID: "id",
            Password: "password",
            Gender: 0,
        }),
        {
            "X-Foobar": "hoge", // [optional] custom headers
        },
        {
            mode: "cors" // [optional] options for fetch API 
        },
    );

    console.log(resp);
})();

FAQ