workign http request

This commit is contained in:
Santiago Lema 2025-05-07 00:28:10 -03:00
parent 6fb8e69244
commit 2e82ca6791
3 changed files with 32 additions and 35 deletions

BIN
DumBer

Binary file not shown.

View file

@ -44,7 +44,7 @@ MainWindow::MainWindow()
BLayoutBuilder::Group<>(this, B_VERTICAL, 0).Add(menuBar).AddGlue().End(); BLayoutBuilder::Group<>(this, B_VERTICAL, 0).Add(menuBar).AddGlue().End();
_inputField = _inputField =
new BTextControl(B_TRANSLATE("Your question: "), "What is the matrix ?", new BMessage(kQuestionChanged)); new BTextControl("", "What is the matrix ?", new BMessage(kQuestionChanged));
_progress = new BStatusBar("prog"); _progress = new BStatusBar("prog");
@ -55,7 +55,6 @@ MainWindow::MainWindow()
_answerView = new BTextView("answer"); _answerView = new BTextView("answer");
BStringView* header = new BStringView("biglabel", "Let's Be Dumber!"); BStringView* header = new BStringView("biglabel", "Let's Be Dumber!");
BFont font; BFont font;
header->GetFont(&font); header->GetFont(&font);
@ -64,7 +63,7 @@ MainWindow::MainWindow()
// Info view, only one line high // Info view, only one line high
_infoView = new BTextView("info"); _infoView = new BTextView("info");
_infoView->SetText("Ask your question.."); _infoView->SetText("...");
_infoView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); _infoView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
_infoView->MakeEditable(false); _infoView->MakeEditable(false);
_infoView->MakeSelectable(false); _infoView->MakeSelectable(false);
@ -79,7 +78,7 @@ MainWindow::MainWindow()
_inputField->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, askH * 6)); _inputField->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, askH * 6));
BButton *sendButton = BButton *sendButton =
new BButton("send", B_TRANSLATE("ask"), new BMessage(kSendPrompt), new BButton("send", B_TRANSLATE("Send"), new BMessage(kSendPrompt),
B_WILL_DRAW | B_NAVIGABLE); B_WILL_DRAW | B_NAVIGABLE);
BLayoutBuilder::Group<>(this, B_VERTICAL, 0) BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
@ -171,14 +170,14 @@ void MainWindow::MessageReceived(BMessage *message) {
case UrlEvent::RequestCompleted: { case UrlEvent::RequestCompleted: {
printf("RequestCompleted\n"); printf("RequestCompleted\n");
auto identifier = message->GetInt32(UrlEventData::Id, -1); auto identifier = message->GetInt32(UrlEventData::Id, -1);
if (resultOne->Identity() == identifier) { if (_lastResult->Identity() == identifier) {
// The following call will not block, because we have been notified // The following call will not block, because we have been notified
// that the request is done. // that the request is done.
auto body = resultOne->Body(); BHttpBody body = _lastResult->Body();
//if (body) if (body.text.has_value())
// _DisplayHttpResult(body.value()); _answerView->SetText(body.text.value());
//else else
// _DisplayError(body.error()); _answerView->SetText("nuthin'");
} }
_infoView->SetText("Completed"); _infoView->SetText("Completed");
@ -198,28 +197,30 @@ void MainWindow::MessageReceived(BMessage *message) {
case UrlEvent::BytesWritten: { case UrlEvent::BytesWritten: {
_infoView->SetText("Some bytes written.."); _infoView->SetText("Some bytes written..");
// auto identifier = message->GetInt32(UrlEventData::Id, -1); auto identifier = message->GetInt32(UrlEventData::Id, -1);
// if (fResult.Identifier() == identifier) { if (_lastResult->Identity() == identifier) {
//off_t numBytes = message->GetInt64(UrlEventData::NumBytes, 0); off_t numBytes = message->GetInt64(UrlEventData::NumBytes, 0);
//_progress->SetTo(numBytes); off_t totalBytes = message->GetInt64(UrlEventData::TotalBytes, 0);
//} _progress->SetTo(numBytes);
_progress->SetMaxValue(totalBytes);
}
} break; } break;
case UrlEvent::DownloadProgress: { case UrlEvent::DownloadProgress: {
auto identifier = message->GetInt32(UrlEventData::Id, -1); auto identifier = message->GetInt32(UrlEventData::Id, -1);
if (resultOne->Identity() == identifier) { if (_lastResult->Identity() == identifier) {
off_t nn = message->GetInt64(UrlEventData::NumBytes, 0); off_t nn = message->GetInt64(UrlEventData::NumBytes, 0);
off_t totalBytes = message->GetInt64(UrlEventData::TotalBytes, 0); off_t totalBytes = message->GetInt64(UrlEventData::TotalBytes, 0);
_progress->SetMaxValue(totalBytes); _progress->SetTo(nn);
_progress->SetTo(nn); _progress->SetMaxValue(totalBytes);
_infoView->SetText("Download Progress.."); _infoView->SetText("Download Progress..");
} }
} break; } break;
default: { default: {
message->PrintToStream(); //message->PrintToStream();
BWindow::MessageReceived(message); // call the parent handler for other messages BWindow::MessageReceived(message); // call the parent handler for other messages
// _infoView->SetText(message->FindMessage()); // _infoView->SetText(message->FindMessage());
break; break;
@ -234,24 +235,21 @@ void MainWindow::MessageReceived(BMessage *message) {
void MainWindow::sendQuery() { void MainWindow::sendQuery() {
//if (resultOne!=nullptr) if (_lastResult)
// _sharedSession.Cancel(resultOne->Identity()); _sharedSession.Cancel(_lastResult->Identity());
_progress->SetMaxValue(100); _progress->SetMaxValue(100);
_progress->SetTo(0); _progress->SetTo(0);
auto url = BUrl("https://www.link-u.com/ip/"); auto url = BUrl("https://www.link-u.com/ip/");
BHttpRequest request = BHttpRequest(url); BHttpRequest request = BHttpRequest(url);
// requestOne = &request;
// request.SetTimeout()
// request.SetMethod(BHttpMethod::Get);
printf("Sending Prompt to server: %s\n", url.UrlString().String()); printf("Sending Prompt to server: %s\n", url.UrlString().String());
_lastResult = _sharedSession.Execute(std::move(request), nullptr, this);
if (_lastResult) {
BHttpResult res = _sharedSession.Execute(std::move(request), nullptr, this); printf("Result has identity: %d\n", _lastResult->Identity());
printf("Result has identity: %d\n", res.Identity()); }
resultOne = &res;
} }

View file

@ -34,7 +34,7 @@
using namespace BPrivate::Network; using namespace BPrivate::Network;
class MainWindow : public BWindow { class MainWindow : public BWindow {
public: public:
MainWindow(); MainWindow();
@ -48,8 +48,7 @@ private:
BHttpSession _sharedSession = BHttpSession (); BHttpSession _sharedSession = BHttpSession ();
BHttpResult* resultOne; std::optional<BHttpResult> _lastResult;
BHttpRequest* requestOne;
BMenuBar *_BuildMenu(); BMenuBar *_BuildMenu();
BTextView * _answerView; BTextView * _answerView;