HaikuDepot: Fixed creating/updating ratings.
* The HTTP Basic authentication never worked. BHttpRequest.SetUserName() and SetPassword() have no effect at all, unless there is a BHttpAuthentication set for the given URL on the BUrlContext. This part of the services API isn't very intuitive, yet. * UpdateUserRating() forgot to append the "filter" array which specifies to the web app which fields of the rating are updated by the request.
This commit is contained in:
@@ -252,6 +252,11 @@ int
|
|||||||
WebAppInterface::fRequestIndex = 0;
|
WebAppInterface::fRequestIndex = 0;
|
||||||
|
|
||||||
|
|
||||||
|
enum {
|
||||||
|
NEEDS_AUTHORIZATION = 1 << 0,
|
||||||
|
DEBUG = 1 << 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
WebAppInterface::WebAppInterface()
|
WebAppInterface::WebAppInterface()
|
||||||
:
|
:
|
||||||
@@ -322,7 +327,7 @@ WebAppInterface::RetrievePackageInfo(const BString& packageName,
|
|||||||
.EndArray()
|
.EndArray()
|
||||||
.End();
|
.End();
|
||||||
|
|
||||||
return _SendJsonRequest("pkg", jsonString, false, message);
|
return _SendJsonRequest("pkg", jsonString, 0, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -354,7 +359,7 @@ WebAppInterface::RetrieveBulkPackageInfo(const StringList& packageNames,
|
|||||||
.EndArray()
|
.EndArray()
|
||||||
.End();
|
.End();
|
||||||
|
|
||||||
return _SendJsonRequest("pkg", jsonString, false, message);
|
return _SendJsonRequest("pkg", jsonString, 0, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -407,7 +412,7 @@ WebAppInterface::RetrieveUserRatings(const BString& packageName,
|
|||||||
.EndArray()
|
.EndArray()
|
||||||
.End();
|
.End();
|
||||||
|
|
||||||
return _SendJsonRequest("userrating", jsonString, false, message);
|
return _SendJsonRequest("userrating", jsonString, 0, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -434,7 +439,7 @@ WebAppInterface::RetrieveUserRating(const BString& packageName,
|
|||||||
.EndArray()
|
.EndArray()
|
||||||
.End();
|
.End();
|
||||||
|
|
||||||
return _SendJsonRequest("userrating", jsonString, false, message);
|
return _SendJsonRequest("userrating", jsonString, 0, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -462,7 +467,8 @@ WebAppInterface::CreateUserRating(const BString& packageName,
|
|||||||
.EndArray()
|
.EndArray()
|
||||||
.End();
|
.End();
|
||||||
|
|
||||||
return _SendJsonRequest("userrating", jsonString, true, message);
|
return _SendJsonRequest("userrating", jsonString, NEEDS_AUTHORIZATION,
|
||||||
|
message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -483,11 +489,19 @@ WebAppInterface::UpdateUserRating(const BString& ratingID,
|
|||||||
.AddValue("comment", comment)
|
.AddValue("comment", comment)
|
||||||
.AddValue("naturalLanguageCode", languageCode)
|
.AddValue("naturalLanguageCode", languageCode)
|
||||||
.AddValue("active", active)
|
.AddValue("active", active)
|
||||||
|
.AddArray("filter")
|
||||||
|
.AddItem("ACTIVE")
|
||||||
|
.AddItem("NATURALLANGUAGE")
|
||||||
|
.AddItem("USERRATINGSTABILITY")
|
||||||
|
.AddItem("COMMENT")
|
||||||
|
.AddItem("RATING")
|
||||||
|
.EndArray()
|
||||||
.EndObject()
|
.EndObject()
|
||||||
.EndArray()
|
.EndArray()
|
||||||
.End();
|
.End();
|
||||||
|
|
||||||
return _SendJsonRequest("userrating", jsonString, true, message);
|
return _SendJsonRequest("userrating", jsonString, NEEDS_AUTHORIZATION,
|
||||||
|
message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -541,7 +555,7 @@ WebAppInterface::RequestCaptcha(BMessage& message)
|
|||||||
.EndArray()
|
.EndArray()
|
||||||
.End();
|
.End();
|
||||||
|
|
||||||
return _SendJsonRequest("captcha", jsonString, false, message);
|
return _SendJsonRequest("captcha", jsonString, 0, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -573,7 +587,7 @@ WebAppInterface::CreateUser(const BString& nickName,
|
|||||||
|
|
||||||
BString jsonString = builder.End();
|
BString jsonString = builder.End();
|
||||||
|
|
||||||
return _SendJsonRequest("user", jsonString, false, message);
|
return _SendJsonRequest("user", jsonString, 0, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -593,7 +607,7 @@ WebAppInterface::AuthenticateUser(const BString& nickName,
|
|||||||
.EndArray()
|
.EndArray()
|
||||||
.End();
|
.End();
|
||||||
|
|
||||||
return _SendJsonRequest("user", jsonString, false, message);
|
return _SendJsonRequest("user", jsonString, 0, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -602,32 +616,36 @@ WebAppInterface::AuthenticateUser(const BString& nickName,
|
|||||||
|
|
||||||
status_t
|
status_t
|
||||||
WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString,
|
WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString,
|
||||||
bool needsAuthorization, BMessage& reply) const
|
uint32 flags, BMessage& reply) const
|
||||||
{
|
{
|
||||||
|
if ((flags & DEBUG) != 0)
|
||||||
|
printf("_SendJsonRequest(%s)\n", jsonString.String());
|
||||||
|
|
||||||
BString urlString("https://depot.haiku-os.org/api/v1/");
|
BString urlString("https://depot.haiku-os.org/api/v1/");
|
||||||
urlString << domain;
|
urlString << domain;
|
||||||
BUrl url(urlString);
|
BUrl url(urlString);
|
||||||
|
|
||||||
ProtocolListener listener;
|
ProtocolListener listener;
|
||||||
BUrlContext context;
|
BUrlContext context;
|
||||||
|
|
||||||
BHttpHeaders headers;
|
BHttpHeaders headers;
|
||||||
// Content-Type
|
|
||||||
headers.AddHeader("Content-Type", "application/json");
|
headers.AddHeader("Content-Type", "application/json");
|
||||||
headers.AddHeader("User-Agent", "X-HDS-Client");
|
headers.AddHeader("User-Agent", "X-HDS-Client");
|
||||||
|
|
||||||
BHttpRequest request(url, true, "HTTP", &listener, &context);
|
BHttpRequest request(url, true, "HTTP", &listener, &context);
|
||||||
|
request.SetMethod(B_HTTP_POST);
|
||||||
|
request.SetHeaders(headers);
|
||||||
|
|
||||||
// Authentication via Basic Authentication
|
// Authentication via Basic Authentication
|
||||||
// The other way would be to obtain a token and then use the Token Bearer
|
// The other way would be to obtain a token and then use the Token Bearer
|
||||||
// header.
|
// header.
|
||||||
if (needsAuthorization && !fUsername.IsEmpty() && !fPassword.IsEmpty()) {
|
if ((flags & NEEDS_AUTHORIZATION) != 0
|
||||||
request.SetUserName(fUsername);
|
&& !fUsername.IsEmpty() && !fPassword.IsEmpty()) {
|
||||||
request.SetPassword(fPassword);
|
BHttpAuthentication authentication(fUsername, fPassword);
|
||||||
|
authentication.SetMethod(B_HTTP_AUTHENTICATION_BASIC);
|
||||||
|
context.AddAuthentication(url, authentication);
|
||||||
}
|
}
|
||||||
|
|
||||||
request.SetMethod(B_HTTP_POST);
|
|
||||||
request.SetHeaders(headers);
|
|
||||||
|
|
||||||
BMemoryIO* data = new BMemoryIO(
|
BMemoryIO* data = new BMemoryIO(
|
||||||
jsonString.String(), jsonString.Length() - 1);
|
jsonString.String(), jsonString.Length() - 1);
|
||||||
|
|
||||||
@@ -635,7 +653,7 @@ WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString,
|
|||||||
|
|
||||||
BMallocIO replyData;
|
BMallocIO replyData;
|
||||||
listener.SetDownloadIO(&replyData);
|
listener.SetDownloadIO(&replyData);
|
||||||
// listener.SetDebug(true);
|
listener.SetDebug((flags & DEBUG) != 0);
|
||||||
|
|
||||||
thread_id thread = request.Run();
|
thread_id thread = request.Run();
|
||||||
wait_for_thread(thread, NULL);
|
wait_for_thread(thread, NULL);
|
||||||
@@ -656,8 +674,8 @@ WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString,
|
|||||||
|
|
||||||
BJson parser;
|
BJson parser;
|
||||||
status_t status = parser.Parse(reply, jsonString);
|
status_t status = parser.Parse(reply, jsonString);
|
||||||
if (status == B_BAD_DATA) {
|
if ((flags & DEBUG) != 0 && status == B_BAD_DATA) {
|
||||||
// printf("Parser choked on JSON:\n%s\n", jsonString.String());
|
printf("Parser choked on JSON:\n%s\n", jsonString.String());
|
||||||
}
|
}
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
status_t _SendJsonRequest(const char* domain,
|
status_t _SendJsonRequest(const char* domain,
|
||||||
BString jsonString, bool needsAuthorization,
|
BString jsonString, uint32 flags,
|
||||||
BMessage& reply) const;
|
BMessage& reply) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
Reference in New Issue
Block a user