Skip to content

fe:sse_streams #757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions lib/providers/collection_providers.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:apidash_core/apidash_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand All @@ -20,6 +22,40 @@ final selectedRequestModelProvider = StateProvider<RequestModel?>((ref) {
}
});

class SSEFramesNotifier extends StateNotifier<Map<String, List<SSEEventModel>>> {
SSEFramesNotifier() : super({});

void addFrame(String requestId, String frame) {
print(frame+"\n");
state = {
...state,
requestId: [...(state[requestId] ?? []), SSEEventModel.fromRawSSE(frame)],
};
}

void clearFrames(String requestId) {
if (state.containsKey(requestId)) {
state = {
...state,
requestId: [],
};
}
}

void removeRequest(String requestId) {
final newState = {...state};
newState.remove(requestId);
state = newState;
}
}

// Provide it globally
final sseFramesProvider =
StateNotifierProvider<SSEFramesNotifier, Map<String, List<SSEEventModel>>>(
(ref) => SSEFramesNotifier(),
);


final requestSequenceProvider = StateProvider<List<String>>((ref) {
var ids = hiveHandler.getIds();
return ids ?? [];
Expand Down Expand Up @@ -262,6 +298,7 @@ class CollectionStateNotifier
unsave();
}


Future<void> sendRequest() async {
final requestId = ref.read(selectedIdStateProvider);
ref.read(codePaneVisibleStateProvider.notifier).state = false;
Expand Down Expand Up @@ -289,6 +326,75 @@ class CollectionStateNotifier
state = map;

bool noSSL = ref.read(settingsProvider).isSSLDisabled;
if(apiType == APIType.sse){

List<SSEEventModel> frames;
await sendSSERequest(
requestId,
apiType,
substitutedHttpRequestModel,
defaultUriScheme: defaultUriScheme,
noSSL: noSSL,
onConnect: (statusCode, responseHeaders, requestHeaders,duration) {

ref.read(sseFramesProvider.notifier).clearFrames(requestId);
map = {...state!};

map[requestId] = requestModel.copyWith(
responseStatus: statusCode,
message: kResponseCodeReasons[statusCode],
httpResponseModel: baseHttpResponseModel.copyWith(
statusCode: statusCode,
headers: responseHeaders,
requestHeaders: requestHeaders,
time: duration,
),
isWorking: true,
);
state = map;


},
onData: (response) {
ref.read(sseFramesProvider.notifier).addFrame(requestId, response);
},
onError: (error, stackTrace) {
frames = ref.read(sseFramesProvider)[requestId] ?? [];
map = {...state!};
map[requestId] = requestModel.copyWith(
responseStatus: 500,
message: error.toString(),
httpResponseModel: baseHttpResponseModel.copyWith(
sseEvents: frames,
),
isWorking: false,
);
state = map;
},
onDone: () {
frames = ref.read(sseFramesProvider)[requestId] ?? [];
map = {...state!};
map[requestId] = requestModel.copyWith(
httpResponseModel: baseHttpResponseModel.copyWith(
sseEvents: frames,
),
isWorking: false,
);
state = map;
},
onCancel: () {
frames = ref.read(sseFramesProvider)[requestId] ?? [];
map = {...state!};
map[requestId] = requestModel.copyWith(
responseStatus: -1,
message: kMsgRequestCancelled,
isWorking: false,
);
state = map;
},
);
return;
}
var responseRec = await sendHttpRequest(
requestId,
apiType,
Expand Down Expand Up @@ -343,6 +449,7 @@ class CollectionStateNotifier
unsave();
}


void cancelRequest() {
final id = ref.read(selectedIdStateProvider);
cancelHttpRequest(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class EditRequestPane extends ConsumerWidget {
final apiType = ref
.watch(selectedRequestModelProvider.select((value) => value?.apiType));
return switch (apiType) {
APIType.rest => const EditRestRequestPane(),
APIType.rest || APIType.sse => const EditRestRequestPane(),
APIType.graphql => const EditGraphQLRequestPane(),
_ => kSizedBoxEmpty,
};
Expand Down
Loading