HaikuDepot: Work on retrieving more pkg info
* Simple, incomplete JSON string-builder. * Not yet using the new bulk information method of the web-app. * Not yet parsing the reply and doing anything with it, consequently commented out. * What works is generating a JSON command and receiving the reply.
This commit is contained in:
@@ -609,6 +609,7 @@ Model::_PopulateAllPackagesThread(bool fromCacheOnly)
|
|||||||
if (package.Get() == NULL)
|
if (package.Get() == NULL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
// _PopulatePackageInfo(package, fromCacheOnly);
|
||||||
_PopulatePackageIcon(package, fromCacheOnly);
|
_PopulatePackageIcon(package, fromCacheOnly);
|
||||||
// TODO: Average user rating. It needs to be shown in the
|
// TODO: Average user rating. It needs to be shown in the
|
||||||
// list view, so without the user clicking the package.
|
// list view, so without the user clicking the package.
|
||||||
@@ -616,6 +617,24 @@ Model::_PopulateAllPackagesThread(bool fromCacheOnly)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
Model::_PopulatePackageInfo(const PackageInfoRef& package, bool fromCacheOnly)
|
||||||
|
{
|
||||||
|
if (fromCacheOnly)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Retrieve info from web-app
|
||||||
|
WebAppInterface interface;
|
||||||
|
BMessage info;
|
||||||
|
|
||||||
|
status_t status = interface.RetrievePackageInfo(package->Title(), info);
|
||||||
|
if (status == B_OK) {
|
||||||
|
// TODO: Parse message...
|
||||||
|
info.PrintToStream();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Model::_PopulatePackageIcon(const PackageInfoRef& package, bool fromCacheOnly)
|
Model::_PopulatePackageIcon(const PackageInfoRef& package, bool fromCacheOnly)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -93,6 +93,9 @@ private:
|
|||||||
static int32 _PopulateAllPackagesEntry(void* cookie);
|
static int32 _PopulateAllPackagesEntry(void* cookie);
|
||||||
void _PopulateAllPackagesThread(bool fromCacheOnly);
|
void _PopulateAllPackagesThread(bool fromCacheOnly);
|
||||||
|
|
||||||
|
void _PopulatePackageInfo(
|
||||||
|
const PackageInfoRef& package,
|
||||||
|
bool fromCacheOnly);
|
||||||
void _PopulatePackageIcon(
|
void _PopulatePackageIcon(
|
||||||
const PackageInfoRef& package,
|
const PackageInfoRef& package,
|
||||||
bool fromCacheOnly);
|
bool fromCacheOnly);
|
||||||
|
|||||||
@@ -10,19 +10,137 @@
|
|||||||
#include <File.h>
|
#include <File.h>
|
||||||
#include <HttpHeaders.h>
|
#include <HttpHeaders.h>
|
||||||
#include <HttpRequest.h>
|
#include <HttpRequest.h>
|
||||||
|
#include <Message.h>
|
||||||
#include <Url.h>
|
#include <Url.h>
|
||||||
#include <UrlContext.h>
|
#include <UrlContext.h>
|
||||||
#include <UrlProtocolListener.h>
|
#include <UrlProtocolListener.h>
|
||||||
#include <UrlProtocolRoster.h>
|
#include <UrlProtocolRoster.h>
|
||||||
|
|
||||||
|
#include "List.h"
|
||||||
#include "PackageInfo.h"
|
#include "PackageInfo.h"
|
||||||
|
|
||||||
|
|
||||||
|
class JsonBuilder {
|
||||||
|
public:
|
||||||
|
JsonBuilder()
|
||||||
|
:
|
||||||
|
fString("{"),
|
||||||
|
fInList(false)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonBuilder& AddObject()
|
||||||
|
{
|
||||||
|
fString << '{';
|
||||||
|
fInList = false;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonBuilder& AddObject(const char* name)
|
||||||
|
{
|
||||||
|
_StartName(name);
|
||||||
|
|
||||||
|
fString << "\":{";
|
||||||
|
fInList = false;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonBuilder& EndObject()
|
||||||
|
{
|
||||||
|
fString << '}';
|
||||||
|
fInList = true;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonBuilder& AddArray(const char* name)
|
||||||
|
{
|
||||||
|
_StartName(name);
|
||||||
|
|
||||||
|
fString << "\":[";
|
||||||
|
|
||||||
|
fInList = false;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonBuilder& EndArray()
|
||||||
|
{
|
||||||
|
fString << ']';
|
||||||
|
fInList = true;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonBuilder& AddValue(const char* name, const char* value)
|
||||||
|
{
|
||||||
|
_StartName(name);
|
||||||
|
|
||||||
|
// TODO: Escape value
|
||||||
|
|
||||||
|
fString << "\":\"";
|
||||||
|
fString << value;
|
||||||
|
fString << "\"";
|
||||||
|
|
||||||
|
fInList = true;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonBuilder& AddValue(const char* name, int value)
|
||||||
|
{
|
||||||
|
_StartName(name);
|
||||||
|
|
||||||
|
fString << "\":";
|
||||||
|
fString << value;
|
||||||
|
|
||||||
|
fInList = true;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonBuilder& AddValue(const char* name, bool value)
|
||||||
|
{
|
||||||
|
_StartName(name);
|
||||||
|
|
||||||
|
fString << "\":";
|
||||||
|
if (value)
|
||||||
|
fString << "true";
|
||||||
|
else
|
||||||
|
fString << "false";
|
||||||
|
|
||||||
|
fInList = true;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
const BString& End()
|
||||||
|
{
|
||||||
|
fString << "}\n";
|
||||||
|
return fString;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void _StartName(const char* name)
|
||||||
|
{
|
||||||
|
if (fInList)
|
||||||
|
fString << ",\"";
|
||||||
|
else
|
||||||
|
fString << '"';
|
||||||
|
|
||||||
|
fString << name;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
BString fString;
|
||||||
|
bool fInList;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class ProtocolListener : public BUrlProtocolListener {
|
class ProtocolListener : public BUrlProtocolListener {
|
||||||
public:
|
public:
|
||||||
ProtocolListener()
|
ProtocolListener()
|
||||||
:
|
:
|
||||||
fDownloadIO(NULL)
|
fDownloadIO(NULL),
|
||||||
|
fDebug(false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,18 +156,23 @@ public:
|
|||||||
|
|
||||||
virtual void ResponseStarted(BUrlRequest* caller)
|
virtual void ResponseStarted(BUrlRequest* caller)
|
||||||
{
|
{
|
||||||
// printf("ResponseStarted(%p)\n", caller);
|
if (fDebug)
|
||||||
|
printf("ResponseStarted(%p)\n", caller);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void HeadersReceived(BUrlRequest* caller)
|
virtual void HeadersReceived(BUrlRequest* caller)
|
||||||
{
|
{
|
||||||
// printf("HeadersReceived(%p)\n", caller);
|
if (fDebug)
|
||||||
|
printf("HeadersReceived(%p)\n", caller);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void DataReceived(BUrlRequest* caller, const char* data,
|
virtual void DataReceived(BUrlRequest* caller, const char* data,
|
||||||
off_t position, ssize_t size)
|
off_t position, ssize_t size)
|
||||||
{
|
{
|
||||||
// printf("DataReceived(%p): %ld bytes\n", caller, size);
|
if (fDebug) {
|
||||||
|
printf("DataReceived(%p): %ld bytes\n", caller, size);
|
||||||
|
printf("%.*s", (int)size, data);
|
||||||
|
}
|
||||||
|
|
||||||
if (fDownloadIO != NULL)
|
if (fDownloadIO != NULL)
|
||||||
fDownloadIO->Write(data, size);
|
fDownloadIO->Write(data, size);
|
||||||
@@ -65,12 +188,14 @@ public:
|
|||||||
virtual void UploadProgress(BUrlRequest* caller, ssize_t bytesSent,
|
virtual void UploadProgress(BUrlRequest* caller, ssize_t bytesSent,
|
||||||
ssize_t bytesTotal)
|
ssize_t bytesTotal)
|
||||||
{
|
{
|
||||||
// printf("UploadProgress(%p): %ld/%ld\n", caller, bytesSent, bytesTotal);
|
if (fDebug)
|
||||||
|
printf("UploadProgress(%p): %ld/%ld\n", caller, bytesSent, bytesTotal);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void RequestCompleted(BUrlRequest* caller, bool success)
|
virtual void RequestCompleted(BUrlRequest* caller, bool success)
|
||||||
{
|
{
|
||||||
// printf("RequestCompleted(%p): %d\n", caller, success);
|
if (fDebug)
|
||||||
|
printf("RequestCompleted(%p): %d\n", caller, success);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void DebugMessage(BUrlRequest* caller,
|
virtual void DebugMessage(BUrlRequest* caller,
|
||||||
@@ -84,11 +209,22 @@ public:
|
|||||||
fDownloadIO = downloadIO;
|
fDownloadIO = downloadIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SetDebug(bool debug)
|
||||||
|
{
|
||||||
|
fDebug = debug;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BDataIO* fDownloadIO;
|
BDataIO* fDownloadIO;
|
||||||
|
bool fDebug;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
WebAppInterface::fRequestIndex = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
WebAppInterface::WebAppInterface()
|
WebAppInterface::WebAppInterface()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -108,62 +244,69 @@ WebAppInterface::SetAuthorization(const BString& username,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//void
|
status_t
|
||||||
//WebAppInterface::_TestGetPackage()
|
WebAppInterface::RetrievePackageInfo(const BString& packageName,
|
||||||
//{
|
BMessage& message)
|
||||||
// BUrl url("https://depot.haiku-os.org/api/v1/pkg");
|
{
|
||||||
//
|
BUrl url("https://depot.haiku-os.org/api/v1/pkg");
|
||||||
// ProtocolListener listener;
|
|
||||||
// BUrlContext context;
|
ProtocolListener listener;
|
||||||
// BHttpHeaders headers;
|
BUrlContext context;
|
||||||
//
|
BHttpHeaders headers;
|
||||||
// // Authentication
|
// Content-Type
|
||||||
// if (!fUsername.IsEmpty() && !fPassword.IsEmpty()) {
|
headers.AddHeader("Content-Type", "application/json");
|
||||||
// BString plain;
|
|
||||||
// plain << fUsername << ':' << fPassword;
|
BHttpRequest request(url, true, "HTTP", &listener, &context);
|
||||||
//
|
|
||||||
// char base64[1024];
|
// Authentication
|
||||||
// ssize_t base64Size = encode_base64(base64, plain, plain.Length(),
|
if (!fUsername.IsEmpty() && !fPassword.IsEmpty()) {
|
||||||
// false);
|
request.SetUserName(fUsername);
|
||||||
//
|
request.SetPassword(fPassword);
|
||||||
// if (base64Size > 0 && base64Size <= (ssize_t)sizeof(base64)) {
|
}
|
||||||
// base64[base64Size] = '\0';
|
|
||||||
//
|
request.SetMethod(B_HTTP_POST);
|
||||||
// BString authorization("Basic ");
|
request.SetHeaders(headers);
|
||||||
// authorization << base64;
|
|
||||||
//
|
BString jsonString = JsonBuilder()
|
||||||
// headers.AddHeader("Authorization", authorization);
|
.AddValue("jsonrpc", "2.0")
|
||||||
// }
|
.AddValue("id", ++fRequestIndex)
|
||||||
// }
|
.AddValue("method", "getPkg")
|
||||||
// // Content-Type
|
.AddArray("params")
|
||||||
// headers.AddHeader("Content-Type", "application/json");
|
.AddObject()
|
||||||
//
|
.AddValue("name", packageName)
|
||||||
// BHttpRequest request(url, true, "HTTP", &listener, &context);
|
.AddValue("architectureCode", "x86_gcc2")
|
||||||
// request.SetMethod(B_HTTP_POST);
|
.AddValue("naturalLanguageCode", "en")
|
||||||
// request.SetHeaders(headers);
|
.AddValue("versionType", "NONE")
|
||||||
//
|
.EndObject()
|
||||||
// BMallocIO* data = new BMallocIO();
|
.EndArray()
|
||||||
//
|
.End();
|
||||||
// BString jsonString
|
|
||||||
// = "{"
|
printf("Sending JSON:\n%s\n", jsonString.String());
|
||||||
// "\"jsonrpc\":\"2.0\","
|
|
||||||
// "\"id\":4143431,"
|
BMemoryIO* data = new BMemoryIO(
|
||||||
// "\"method\":\"getPkg\","
|
jsonString.String(), jsonString.Length() - 1);
|
||||||
// "\"params\":[{"
|
|
||||||
// "\"name\":\"apr\","
|
request.AdoptInputData(data, jsonString.Length() - 1);
|
||||||
// "\"architectureCode\":\"x86\","
|
|
||||||
// "\"versionType\":\"NONE\""
|
BMallocIO replyData;
|
||||||
// "}]"
|
listener.SetDownloadIO(&replyData);
|
||||||
// "}"
|
// listener.SetDebug(true);
|
||||||
// ;
|
|
||||||
//
|
thread_id thread = request.Run();
|
||||||
// data->WriteAt(0, jsonString.String(), jsonString.Length());
|
wait_for_thread(thread, NULL);
|
||||||
// data->Seek(0, SEEK_SET);
|
|
||||||
// request.AdoptInputData(data);
|
const BHttpResult& result = dynamic_cast<const BHttpResult&>(
|
||||||
//
|
request.Result());
|
||||||
// thread_id thread = request.Run();
|
|
||||||
// wait_for_thread(thread, NULL);
|
int32 statusCode = result.StatusCode();
|
||||||
//}
|
|
||||||
|
if (statusCode == 200)
|
||||||
|
return B_OK;
|
||||||
|
|
||||||
|
printf("Response code: %ld\n", statusCode);
|
||||||
|
|
||||||
|
return B_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
|
|
||||||
class BDataIO;
|
class BDataIO;
|
||||||
|
class BMessage;
|
||||||
|
|
||||||
|
|
||||||
class WebAppInterface {
|
class WebAppInterface {
|
||||||
@@ -21,6 +22,10 @@ public:
|
|||||||
void SetAuthorization(const BString& username,
|
void SetAuthorization(const BString& username,
|
||||||
const BString& password);
|
const BString& password);
|
||||||
|
|
||||||
|
status_t RetrievePackageInfo(
|
||||||
|
const BString& packageName,
|
||||||
|
BMessage& message);
|
||||||
|
|
||||||
status_t RetrievePackageIcon(
|
status_t RetrievePackageIcon(
|
||||||
const BString& packageName,
|
const BString& packageName,
|
||||||
BDataIO* stream);
|
BDataIO* stream);
|
||||||
@@ -28,6 +33,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
BString fUsername;
|
BString fUsername;
|
||||||
BString fPassword;
|
BString fPassword;
|
||||||
|
static int fRequestIndex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user