SDKs

Dart / Flutter

Flutter 앱과 Dart 서버를 위한 Dart의 관용적인 패턴

간단한 케이스에는 http 패키지를, 인터셉터와 retry 가 필요하다면 dio 를 사용하세요. 아래는 http 예시입니다 —— Flutter 의 Dart SDK 에 기본 포함되어 있습니다.

pubspec.yaml

dependencies:
  http: ^1.2.2

설정

import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;

const api = 'https://openapi.thunderbit.com/openapi/v1';
final headers = <String, String>{
  'Authorization': 'Bearer ${Platform.environment['THUNDERBIT_API_KEY']}',
  'Content-Type': 'application/json',
};

페이지 Distill

Future<String> distill(String url) async {
  final res = await http.post(
    Uri.parse('$api/distill'),
    headers: headers,
    body: jsonEncode({'url': url}),
  );
  if (res.statusCode >= 400) throw Exception(res.body);
  final body = jsonDecode(res.body) as Map<String, dynamic>;
  return body['data']['markdown'] as String;
}

구조화된 데이터 Extract

Future<Map<String, dynamic>> extract(String url) async {
  final res = await http.post(
    Uri.parse('$api/extract'),
    headers: headers,
    body: jsonEncode({
      'url': url,
      'schema': {
        'type': 'object',
        'properties': {
          'name':  {'type': 'string'},
          'price': {'type': 'number'},
        },
        'required': ['name', 'price'],
      },
    }),
  );
  return (jsonDecode(res.body) as Map<String, dynamic>)['data'];
}

Flutter UI 팁

호출을 FutureBuilder 로 감싸서 UI 가 멈추지 않도록 하세요 —— 그리고 프로덕션에서는 Flutter 앱이 아닌 Dart 서버가 API Key 를 소유하도록 하세요.

Batch 작업의 경우 제출 후 서버 측 Webhook 핸들러가 처리하도록 하세요. Webhooks 를 참고하세요.

공식 Dart SDK 가 개발 중입니다 —— 곧 다시 확인해 주세요.