Flutter OpenAPI集成插件djangoflow_openapi的使用

Flutter OpenAPI集成插件djangoflow_openapi的使用

描述

此Dart包由OpenAPI生成器项目自动生成:

  • API版本: 0.0.1 (v1)
  • 构建包: org.openapitools.codegen.languages.DartDioClientCodegen

要求

安装与使用

pub.dev

要从pub.dev使用该包,请在pubspec.yaml文件中包含以下内容:

dependencies:
  djangoflow_openapi: 0.1.22

GitHub

如果此Dart包发布到GitHub,请在pubspec.yaml文件中包含以下内容:

dependencies:
  djangoflow_openapi:
    git:
      url: https://github.com/GIT_USER_ID/GIT_REPO_ID.git
      #ref: main

本地开发

要从本地磁盘使用该包,请在pubspec.yaml文件中包含以下内容:

dependencies:
  djangoflow_openapi:
    path: /path/to/djangoflow_openapi

入门指南

请遵循安装程序,并运行以下代码:

import 'package:djangoflow_openapi/djangoflow_openapi.dart';

final api = DjangoflowOpenapi().getAuthApi();

// 创建OTPObtainRequest对象
final OTPObtainRequest oTPObtainRequest = OTPObtainRequest(
  // 初始化参数
);

try {
  final response = await api.authOtpCreate(oTPObtainRequest);
  print(response);
} catch on DioError (e) {
  print("Exception when calling AuthApi->authOtpCreate: $e\n");
}

API端点文档

所有URI相对于http://localhost

类别 方法名称 HTTP请求 描述
AuthApi authOtpCreate POST /api/v1/auth/otp/
AuthApi authOtpDevicesConfirmCreate POST /api/v1/auth/otp-devices/{id}/confirm/
AuthApi authOtpDevicesCreate POST /api/v1/auth/otp-devices/
AuthApi authOtpDevicesDestroy DELETE /api/v1/auth/otp-devices/{id}/
AuthApi authOtpDevicesList GET /api/v1/auth/otp-devices/
AuthApi authOtpDevicesRetrieve GET /api/v1/auth/otp-devices/{id}/
AuthApi authSocialConnectCreate POST /api/v1/auth/social/connect/
AuthApi authSocialCreate POST /api/v1/auth/social/
AuthApi authTokenBlacklistCreate POST /api/v1/auth/token/blacklist/
AuthApi authTokenCreate POST /api/v1/auth/token/
AuthApi authTokenRefreshCreate POST /api/v1/auth/token/refresh/
AuthApi authTokenVerifyCreate POST /api/v1/auth/token/verify/
AuthApi authUsersCreate POST /api/v1/auth/users/
AuthApi authUsersPartialUpdate PATCH /api/v1/auth/users/{id}/
AuthApi authUsersRetrieve GET /api/v1/auth/users/{id}/
AuthApi authUsersSetPasswordCreate POST /api/v1/auth/users/{id}/set-password/
AuthApi authUsersTwoFaPartialUpdate PATCH /api/v1/auth/users/{id}/two-fa/
AuthApi authUsersTwoFaRetrieve GET /api/v1/auth/users/{id}/two-fa/
ChatApi chatRoomsCreate POST /api/v1/chat/rooms/
ChatApi chatRoomsDestroy DELETE /api/v1/chat/rooms/{id}/
ChatApi chatRoomsList GET /api/v1/chat/rooms/
ChatApi chatRoomsMemberCreate POST /api/v1/chat/rooms/{id}/member/
ChatApi chatRoomsMembersRetrieve GET /api/v1/chat/rooms/{id}/members/
ChatApi chatRoomsMessagesCreate POST /api/v1/chat/rooms/{room_id}/messages/
ChatApi chatRoomsMessagesDestroy DELETE /api/v1/chat/rooms/{room_id}/messages/{id}/
ChatApi chatRoomsMessagesList GET /api/v1/chat/rooms/{room_id}/messages/
ChatApi chatRoomsMessagesPartialUpdate PATCH /api/v1/chat/rooms/{room_id}/messages/{id}/
ChatApi chatRoomsMessagesRetrieve GET /api/v1/chat/rooms/{room_id}/messages/{id}/
ChatApi chatRoomsMessagesUpdate PUT /api/v1/chat/rooms/{room_id}/messages/{id}/
ChatApi chatRoomsPartialUpdate PATCH /api/v1/chat/rooms/{id}/
ChatApi chatRoomsRetrieve GET /api/v1/chat/rooms/{id}/
ChatApi chatRoomsUpdate PUT /api/v1/chat/rooms/{id}/
NotificationsApi notificationsActionCategoriesList GET /api/v1/notifications/action-categories/
NotificationsApi notificationsDevicesCreate POST /api/v1/notifications/devices/
NotificationsApi notificationsDevicesDestroy DELETE /api/v1/notifications/devices/{registration_id}/
NotificationsApi notificationsDevicesList GET /api/v1/notifications/devices/
NotificationsApi notificationsDevicesPartialUpdate PATCH /api/v1/notifications/devices/{registration_id}/
NotificationsApi notificationsDevicesRetrieve GET /api/v1/notifications/devices/{registration_id}/
NotificationsApi notificationsDevicesUpdate PUT /api/v1/notifications/devices/{registration_id}/
RemoteconfigApi remoteconfigRetrieve GET /api/v1/remoteconfig/

模型文档

  • ActionEnum
  • ChangePasswordRequest
  • ChatMessage
  • ChatMessageRequest
  • ChatRoom
  • ChatRoomMemberList
  • ChatRoomMembers
  • ChatRoomMembersRequest
  • ChatRoomRequest
  • ChatTypeEnum
  • Error
  • ErrorResponse
  • OTPDevice
  • OTPDeviceConfirmRequest
  • OTPDeviceRequest
  • OTPDeviceTypeEnum
  • OTPObtain
  • OTPObtainRequest
  • PaginatedChatMessageList
  • PaginatedChatRoomList
  • PaginatedOTPDeviceList
  • PaginatedUserDeviceList
  • PatchedChatMessageRequest
  • PatchedChatRoomRequest
  • PatchedUser2FARequest
  • PatchedUserDeviceRequest
  • PatchedUserIdentityRequest
  • ProviderEnum
  • PushAction
  • PushActionCategory
  • RemoteConfig
  • SocialTokenObtainRequest
  • Token
  • TokenBlacklistRequest
  • TokenObtainRequest
  • TokenRefreshRequest
  • TokenVerifyRequest
  • User
  • User2FA
  • UserDevice
  • UserDeviceRequest
  • UserDeviceTypeEnum
  • UserIdentity
  • UserIdentityRequest
  • UserRequest

授权文档

定义了API的授权方案:

basicAuth

  • 类型: HTTP基本认证

cookieAuth

  • 类型: API密钥
  • 密钥参数名: sessionid
  • 位置:

jwtAuth

  • 类型: HTTP基本认证

作者


更多关于Flutter OpenAPI集成插件djangoflow_openapi的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter OpenAPI集成插件djangoflow_openapi的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在 Flutter 项目中集成 Django 后端生成的 OpenAPI 规范时,可以使用 djangoflow_openapi 插件来简化 API 调用和模型生成。djangoflow_openapi 是一个 Flutter 插件,它基于 OpenAPI 规范自动生成 Dart 代码,使得与后端 API 的交互更加方便。

1. 安装 djangoflow_openapi 插件

首先,你需要在 pubspec.yaml 文件中添加 djangoflow_openapi 依赖:

dependencies:
  flutter:
    sdk: flutter
  djangoflow_openapi: ^latest_version

然后运行 flutter pub get 来安装依赖。

2. 生成 Dart 代码

djangoflow_openapi 插件依赖于 OpenAPI 规范文件(通常是 swagger.jsonopenapi.yaml)。你可以使用 openapi-generator 工具来生成 Dart 代码。

首先,确保你已经安装了 openapi-generator

npm install @openapitools/openapi-generator-cli -g

然后,使用以下命令生成 Dart 代码:

openapi-generator-cli generate -i path/to/your/openapi.yaml -g dart-dio -o lib/api

这将在 lib/api 目录下生成 Dart 代码。

3. 配置 djangoflow_openapi

lib/main.dart 中,你可以配置 djangoflow_openapi 插件来使用生成的 API 客户端。

import 'package:flutter/material.dart';
import 'package:djangoflow_openapi/djangoflow_openapi.dart';
import 'api/api.dart'; // 生成的 API 代码

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 配置 API 客户端
    final apiClient = ApiClient(basePath: 'https://your-api-url.com');
    final defaultApi = DefaultApi(apiClient);

    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(api: defaultApi),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final DefaultApi api;

  MyHomePage({required this.api});

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _response = '';

  Future<void> _fetchData() async {
    try {
      final response = await widget.api.someEndpoint(); // 调用 API 方法
      setState(() {
        _response = response.toString();
      });
    } catch (e) {
      setState(() {
        _response = 'Error: $e';
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter OpenAPI Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(_response),
            ElevatedButton(
              onPressed: _fetchData,
              child: Text('Fetch Data'),
            ),
          ],
        ),
      ),
    );
  }
}

4. 使用生成的 API 方法

在上面的代码中,DefaultApi 是生成的 API 客户端类,你可以通过 widget.api.someEndpoint() 来调用 API 方法。someEndpoint 是你在 OpenAPI 规范中定义的端点。

5. 处理认证

如果你的 API 需要认证,你可以通过配置 ApiClient 来处理认证信息。例如,使用 Bearer Token:

final apiClient = ApiClient(basePath: 'https://your-api-url.com');
apiClient.addDefaultHeader('Authorization', 'Bearer YOUR_TOKEN');
final defaultApi = DefaultApi(apiClient);
回到顶部