Skip to content

Commit

Permalink
add tests and security definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
HomelessDinosaur committed Mar 4, 2024
1 parent b67e596 commit b5b8007
Show file tree
Hide file tree
Showing 55 changed files with 1,416 additions and 929 deletions.
8 changes: 7 additions & 1 deletion example/services/nitric_example.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:nitric_sdk/nitric.dart';
import 'package:nitric_sdk/resources.dart';
import 'package:nitric_sdk/src/context/common.dart';
import 'package:uuid/uuid.dart';

class Profile {
Expand All @@ -25,6 +24,13 @@ void main() {
// Create an API named 'public'
final profileApi = Nitric.api("public");

var oidc = Nitric.oidcRule(
"profile security",
"https://dev-w7gm5ldb.us.auth0.com",
["https://test-security-definition/"]);

Nitric.attachOidc(profileApi.name, oidc);

// Define a collection named 'profiles', then request reading and writing permissions.
final profiles = Nitric.store("profiles").requires([
KeyValueStorePermission.getting,
Expand Down
1 change: 0 additions & 1 deletion lib/resource.dart

This file was deleted.

1 change: 1 addition & 0 deletions lib/src/api/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export 'keyvalue.dart';
export 'secret.dart';
export 'topic.dart';
export 'proto.dart';
export 'queue.dart';
15 changes: 10 additions & 5 deletions lib/src/api/bucket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class Bucket {
}
}

/// Get a reference to
/// Get a reference to a file by it's [key].
File file(String key) {
return File(this, key);
}

/// Create a blob event subscription when
/// Create a blob event subscription triggered on the [blobEventType] filtered by files that match the [keyPrefixFilter].
Future<void> on(BlobEventType blobEventType, String keyPrefixFilter,
BlobEventHandler handler) async {
// Create the request to register the Storage listener with the membrane
Expand All @@ -51,7 +51,7 @@ class Bucket {
}
}

/// A reference to a file in the bucket.
/// A reference to a [File] in the bucket.
class File {
final Bucket _bucket;

Expand Down Expand Up @@ -107,19 +107,24 @@ class File {
return resp.exists;
}

/// Get a presigned download URL with an [expiry] time (in seconds). Defaults to 600.
/// Get a presigned download URL with an [expiry] time (in seconds). Defaults to 600 (10 minutes), max of 604800 (7 days).
Future<String> getDownloadUrl([int expiry = 600]) async {
return _getSignedUrl($p.StoragePreSignUrlRequest_Operation.READ, expiry);
}

/// Get a presigned upload URL with an [expiry] time (in seconds). Defaults to 600.
/// Get a presigned upload URL with an [expiry] time (in seconds). Defaults to 600 (10 minutes), max of 604800 (7 days).
Future<String> getUploadUrl([int expiry = 600]) async {
return _getSignedUrl($p.StoragePreSignUrlRequest_Operation.WRITE, expiry);
}

/// Create a presigned URL with a determined [op] type and [expiry] time (in seconds).
Future<String> _getSignedUrl(
$p.StoragePreSignUrlRequest_Operation op, int expiry) async {
if (expiry < 0 || expiry > 604800) {
throw ArgumentError.value(expiry, "expiry",
"presigned url expiry must be between 0 and 604800");
}

var exp = $d.Duration(seconds: Int64(expiry));

var req = $p.StoragePreSignUrlRequest(
Expand Down
4 changes: 3 additions & 1 deletion lib/src/api/keyvalue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class KeyValueStore {
}
}

/// Get a reference to a key in the store.
/// Get a reference to a [key] in the store.
Future<Map<String, dynamic>> get(String key) async {
var req = $p.KeyValueGetRequest(ref: $p.ValueRef(key: key, store: name));

Expand All @@ -28,6 +28,7 @@ class KeyValueStore {
return Proto.mapFromStruct(resp.value.content);
}

/// Set a new [value] to a [key] in the store.
Future<void> set(String key, Map<String, dynamic> value) async {
var content = Proto.structFromMap(value);

Expand All @@ -37,6 +38,7 @@ class KeyValueStore {
await _keyValueClient.set(req);
}

/// Delete a [key] from the store.
Future<void> delete(String key) async {
var req = $p.KeyValueDeleteRequest(ref: $p.ValueRef(key: key, store: name));

Expand Down
8 changes: 5 additions & 3 deletions lib/src/api/proto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Proto {
return Value(numberValue: value.toDouble());
} else if (value is double) {
return Value(numberValue: value);
} else if (value is bool) {
return Value(boolValue: value);
} else if (value is List) {
return Value(
listValue:
Expand All @@ -42,12 +44,12 @@ class Proto {
return switch (protoValue.whichKind()) {
Value_Kind.boolValue => protoValue.boolValue,
Value_Kind.listValue => protoValue.listValue.values
.map((v) => Proto.dynamicFromValue(protoValue)),
Value_Kind.nullValue => null,
.map((v) => Proto.dynamicFromValue(v))
.toList(),
Value_Kind.numberValue => protoValue.numberValue,
Value_Kind.stringValue => protoValue.stringValue,
Value_Kind.structValue => Proto.mapFromStruct(protoValue.structValue),
_ => throw FormatException("The proto value type is invalid.")
_ => null
};
}
}
83 changes: 83 additions & 0 deletions lib/src/api/queue.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import 'dart:async';

import 'package:nitric_sdk/api.dart';
import 'package:nitric_sdk/src/grpc_helper.dart';
import 'package:nitric_sdk/src/nitric/proto/queues/v1/queues.pbgrpc.dart' as $p;

class Queue {
late $p.QueuesClient _queuesClient;

/// The name of the queue.
String name;

/// Construct a new queue.
Queue(this.name, {$p.QueuesClient? client}) {
if (client == null) {
final channel = createClientChannelFromEnvVar();

_queuesClient = $p.QueuesClient(channel);
} else {
_queuesClient = client;
}
}

/// Enqueue a list of [messages] to the queue.
Future<List<FailedMessage>> enqueue(
List<Map<String, dynamic>> messages) async {
var messageStructs = messages.map((message) =>
$p.QueueMessage(structPayload: Proto.structFromMap(message)));

var req = $p.QueueEnqueueRequest(
messages: messageStructs,
queueName: name,
);

var resp = await _queuesClient.enqueue(req);

return resp.failedMessages.map((fm) => FailedMessage(fm)).toList();
}

/// Dequeue a list of messages from the queue. The number of messages dequeued can be between 0 and [depth] messages.
Future<List<DequeuedMessage>> dequeue({int depth = 1}) async {
var req = $p.QueueDequeueRequest(queueName: name, depth: depth);

var resp = await _queuesClient.dequeue(req);

return resp.messages.map((m) => DequeuedMessage(this, m)).toList();
}
}

class DequeuedMessage {
late Queue _queue;
late String _leaseId;

/// The message contents.
late Map<String, dynamic> message;

DequeuedMessage(Queue queue, $p.DequeuedMessage message) {
_leaseId = message.leaseId;
_queue = queue;
this.message = Proto.mapFromStruct(message.message.structPayload);
}

/// Inform the queue that the message was handled successfully.
void complete() async {
var req =
$p.QueueCompleteRequest(leaseId: _leaseId, queueName: _queue.name);

await _queue._queuesClient.complete(req);
}
}

class FailedMessage {
/// The [details] of the failure.
late String details;

/// The [message] that failed.
late Map<String, dynamic> message;

FailedMessage($p.FailedEnqueueMessage failedMessage) {
details = failedMessage.details;
message = Proto.mapFromStruct(failedMessage.message.structPayload);
}
}
2 changes: 1 addition & 1 deletion lib/src/api/topic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Topic {
}

/// Publish a [message] to the topic. Optional [delay] (in seconds) can be set to delay the message publish time.
Future<void> publish(Map<String, dynamic> message, [int delay = 0]) async {
Future<void> publish(Map<String, dynamic> message, {int delay = 0}) async {
// Convert the message to a proto struct wrapped in an event message
final messageStruct = Proto.structFromMap(message);

Expand Down
Loading

0 comments on commit b5b8007

Please sign in to comment.