Skip to content

Commit

Permalink
feat!: API refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dnys1 committed May 28, 2024
1 parent 1929a0b commit 043bb24
Show file tree
Hide file tree
Showing 46 changed files with 624 additions and 112 deletions.
8 changes: 5 additions & 3 deletions examples/gemini/celest/functions/gemini.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import 'dart:convert';
import 'package:celest/celest.dart';
import 'package:google_generative_ai/google_generative_ai.dart';

import '../resources.dart';
import '../generated/resources.dart';

/// Returns a list of available models.
@cloud
Future<List<String>> availableModels() async => _availableModels;

/// The list of available models.
Expand All @@ -22,10 +23,11 @@ const _availableModels = [
/// Prompts the Gemini [modelName] with the given [prompt] and [parameters].
///
/// Returns the generated text.
@cloud
Future<String> generateContent({
required String modelName,
required String prompt,
@Env.geminiApiKey required String apiKey,
@env.geminiApiKey required String apiKey,
}) async {
if (!_availableModels.contains(modelName)) {
throw BadRequestException('Invalid model: $modelName');
Expand All @@ -45,7 +47,7 @@ Future<String> generateContent({
print('Selected answer: $text');
return text;
case _:
throw InternalServerException('Failed to generate content');
throw InternalServerError('Failed to generate content');
}
}

Expand Down
9 changes: 9 additions & 0 deletions examples/gemini/celest/generated/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Generated Celest code

This directory contains code generated by the Celest CLI to assist in building
your backend.

This code can be safely checked into version control, but it should not be
modified directly.

It is planned to replace this directory with macros when they become stable.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ library;

import 'package:celest/celest.dart';

@Deprecated('Use `Env` instead.')
typedef env = Env;
@Deprecated('Use `env` instead.')
typedef Env = env;

abstract final class Env {
abstract final class env {
static const geminiApiKey = EnvironmentVariable(name: r'GEMINI_API_KEY');
}
7 changes: 3 additions & 4 deletions examples/gemini/celest/lib/src/client/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ class CelestFunctionsGemini {
throw Serializers.instance.deserialize<BadRequestException>($details);
case r'UnauthorizedException':
throw Serializers.instance.deserialize<UnauthorizedException>($details);
case r'InternalServerException':
throw Serializers.instance
.deserialize<InternalServerException>($details);
case r'InternalServerError':
throw Serializers.instance.deserialize<InternalServerError>($details);
case r'SerializationException':
throw Serializers.instance
.deserialize<SerializationException>($details);
Expand All @@ -56,7 +55,7 @@ class CelestFunctionsGemini {
case 400:
throw BadRequestException($code);
case _:
throw InternalServerException($code);
throw InternalServerError($code);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/gemini/celest/lib/src/client/serializers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ void initSerializers() {
},
));
Serializers.instance
.put(Serializer.define<InternalServerException, Map<String, Object?>>(
.put(Serializer.define<InternalServerError, Map<String, Object?>>(
serialize: ($value) => {r'message': $value.message},
deserialize: ($serialized) {
return InternalServerException(($serialized[r'message'] as String));
return InternalServerError(($serialized[r'message'] as String));
},
));
Serializers.instance
Expand Down
8 changes: 5 additions & 3 deletions examples/openai/celest/functions/open_ai.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:celest_backend/models.dart';
import 'package:chat_gpt_sdk/chat_gpt_sdk.dart';
import 'package:chat_gpt_sdk/src/model/chat_complete/response/chat_choice.dart';

import '../resources.dart';
import '../generated/resources.dart';

/// Creates an instance of the OpenAI client.
OpenAI _createOpenAI(String token) => OpenAI.instance.build(
Expand All @@ -18,6 +18,7 @@ OpenAI _createOpenAI(String token) => OpenAI.instance.build(
);

/// Returns a list of available models.
@cloud
Future<List<String>> availableModels() async => _availableModels;

/// The list of available models.
Expand All @@ -32,11 +33,12 @@ const _availableModels = [
/// Prompts the GPT [model] with the given [prompt] and [parameters].
///
/// Returns the generated text.
@cloud
Future<String> openAIRequest({
required String model,
required String prompt,
ModelParameters parameters = const ModelParameters(),
@Env.openAiToken required String openAiToken,
@env.openAiToken required String openAiToken,
}) async {
final openAI = _createOpenAI(openAiToken);

Expand All @@ -63,7 +65,7 @@ Future<String> openAIRequest({
case ChatCTResponse(choices: [ChatChoice(:final message?), ...]):
return message.content.trim();
default:
throw InternalServerException(
throw InternalServerError(
"Couldn't complete request. Please try again later.",
);
}
Expand Down
9 changes: 9 additions & 0 deletions examples/openai/celest/generated/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Generated Celest code

This directory contains code generated by the Celest CLI to assist in building
your backend.

This code can be safely checked into version control, but it should not be
modified directly.

It is planned to replace this directory with macros when they become stable.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ library;

import 'package:celest/celest.dart';

@Deprecated('Use `Env` instead.')
typedef env = Env;
@Deprecated('Use `env` instead.')
typedef Env = env;

abstract final class Env {
abstract final class env {
static const openAiToken = EnvironmentVariable(name: r'OPEN_AI_TOKEN');
}
5 changes: 1 addition & 4 deletions examples/openai/celest/lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ import 'src/client/serializers.dart';
final Celest celest = Celest();

enum CelestEnvironment {
local,
production;
local;

Uri get baseUri => switch (this) {
local => kIsWeb || !_$io.Platform.isAndroid
? Uri.parse('http://localhost:7777')
: Uri.parse('http://10.0.2.2:7777'),
production =>
Uri.parse('https://openai-example-xmfv-v76lntiq7q-wn.a.run.app'),
};
}

Expand Down
7 changes: 3 additions & 4 deletions examples/openai/celest/lib/src/client/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ class CelestFunctionsOpenAi {
throw Serializers.instance.deserialize<BadRequestException>($details);
case r'UnauthorizedException':
throw Serializers.instance.deserialize<UnauthorizedException>($details);
case r'InternalServerException':
throw Serializers.instance
.deserialize<InternalServerException>($details);
case r'InternalServerError':
throw Serializers.instance.deserialize<InternalServerError>($details);
case r'SerializationException':
throw Serializers.instance
.deserialize<SerializationException>($details);
Expand All @@ -41,7 +40,7 @@ class CelestFunctionsOpenAi {
case 400:
throw BadRequestException($code);
case _:
throw InternalServerException($code);
throw InternalServerError($code);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/openai/celest/lib/src/client/serializers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ void initSerializers() {
},
));
Serializers.instance
.put(Serializer.define<InternalServerException, Map<String, Object?>>(
.put(Serializer.define<InternalServerError, Map<String, Object?>>(
serialize: ($value) => {r'message': $value.message},
deserialize: ($serialized) {
return InternalServerException(($serialized[r'message'] as String));
return InternalServerError(($serialized[r'message'] as String));
},
));
Serializers.instance
Expand Down
6 changes: 6 additions & 0 deletions examples/todo/celest/functions/tasks.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import 'package:celest/celest.dart';
import 'package:celest_backend/exceptions.dart';
import 'package:celest_backend/models.dart';
import 'package:uuid/uuid.dart';

Map<String, Task> tasks = {};

@cloud
Future<Map<String, Task>> listAllTasks() async {
print('fetching tasks');
return tasks;
}

@cloud
Future<void> addTask({
required String title,
required Importance importance,
Expand All @@ -26,11 +29,13 @@ Future<void> addTask({
tasks[newTask.id] = newTask;
}

@cloud
Future<void> deleteTask({required String id}) async {
print('removing task $id');
tasks.remove(id);
}

@cloud
Future<void> markAsCompleted({required String id}) async {
print('marking as completed');
final task = tasks[id];
Expand All @@ -40,6 +45,7 @@ Future<void> markAsCompleted({required String id}) async {
tasks[id] = task.copyWith(isCompleted: true);
}

@cloud
Future<void> markAsIncomplete({required String id}) async {
print('marking as incomplete');
final task = tasks[id];
Expand Down
9 changes: 9 additions & 0 deletions examples/todo/celest/generated/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Generated Celest code

This directory contains code generated by the Celest CLI to assist in building
your backend.

This code can be safely checked into version control, but it should not be
modified directly.

It is planned to replace this directory with macros when they become stable.
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/todo/celest/lib/src/client/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CelestFunctionsTasks {
case 400:
throw BadRequestException($code);
case _:
throw InternalServerException($code);
throw InternalServerError($code);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/celest/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ analyzer:
strict-inference: true
strict-raw-types: true
errors:
camel_case_types: ignore

# To prevent issues publishing.
depend_on_referenced_packages: error
public_member_api_docs: warning
Expand Down
2 changes: 2 additions & 0 deletions packages/celest/example/celest/functions/greeting.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Cloud functions are top-level Dart functions defined in the `functions/`
// folder of your Celest project.

import 'package:celest/celest.dart';
import 'package:celest_backend/exceptions/bad_name_exception.dart';
import 'package:celest_backend/models/person.dart';

/// Says hello to a [person].
@cloud
Future<String> sayHello({required Person person}) async {
if (person.name.isEmpty) {
// Throw a custom exception defined in the `lib/exceptions/` and catch
Expand Down
9 changes: 9 additions & 0 deletions packages/celest/example/celest/generated/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Generated Celest code

This directory contains code generated by the Celest CLI to assist in building
your backend.

This code can be safely checked into version control, but it should not be
modified directly.

It is planned to replace this directory with macros when they become stable.
10 changes: 8 additions & 2 deletions packages/celest/example/celest/lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ library; // ignore_for_file: no_leading_underscores_for_library_prefixes

import 'dart:io' as _$io;

import 'package:celest_core/_internal.dart';
import 'package:celest_core/src/util/globals.dart';
import 'package:http/http.dart' as _$http;

Expand All @@ -24,12 +25,16 @@ enum CelestEnvironment {
};
}

class Celest {
class Celest with CelestBase {
var _initialized = false;

late CelestEnvironment _currentEnvironment;

late _$http.Client httpClient = _$http.Client();
late final NativeStorage _storage = NativeStorage(scope: 'celest');

@override
late _$http.Client httpClient =
CelestHttpClient(secureStorage: _storage.secure);

late Uri _baseUri;

Expand All @@ -46,6 +51,7 @@ class Celest {
CelestEnvironment get currentEnvironment =>
_checkInitialized(() => _currentEnvironment);

@override
Uri get baseUri => _checkInitialized(() => _baseUri);

CelestFunctions get functions => _checkInitialized(() => _functions);
Expand Down
13 changes: 12 additions & 1 deletion packages/celest/example/celest/lib/src/client/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'package:celest/celest.dart';
import 'package:celest_backend/exceptions/bad_name_exception.dart'
as _$bad_name_exception;
import 'package:celest_backend/models/person.dart' as _$person;
import 'package:celest_core/src/exception/cloud_exception.dart';
import 'package:celest_core/src/exception/serialization_exception.dart';

import '../../client.dart';

Expand All @@ -26,6 +28,15 @@ class CelestFunctionsGreeting {
final $code = ($error['code'] as String);
final $details = ($error['details'] as Map<String, Object?>?);
switch ($code) {
case r'BadRequestException':
throw Serializers.instance.deserialize<BadRequestException>($details);
case r'UnauthorizedException':
throw Serializers.instance.deserialize<UnauthorizedException>($details);
case r'InternalServerError':
throw Serializers.instance.deserialize<InternalServerError>($details);
case r'SerializationException':
throw Serializers.instance
.deserialize<SerializationException>($details);
case r'BadNameException':
throw Serializers.instance
.deserialize<_$bad_name_exception.BadNameException>($details);
Expand All @@ -34,7 +45,7 @@ class CelestFunctionsGreeting {
case 400:
throw BadRequestException($code);
case _:
throw InternalServerException($code);
throw InternalServerError($code);
}
}
}
Expand Down
Loading

0 comments on commit 043bb24

Please sign in to comment.