1 /* 2 * Copyright 2014, Stephan Aßmus <superstippi@gmx.de>. 3 * Copyright 2016-2019, Andrew Lindesay <apl@lindesay.co.nz>. 4 * All rights reserved. Distributed under the terms of the MIT License. 5 */ 6 #ifndef WEB_APP_INTERFACE_H 7 #define WEB_APP_INTERFACE_H 8 9 10 #include <Application.h> 11 #include <JsonWriter.h> 12 #include <String.h> 13 #include <package/PackageVersion.h> 14 15 #include "List.h" 16 #include "UserUsageConditions.h" 17 18 19 class BDataIO; 20 class BMessage; 21 using BPackageKit::BPackageVersion; 22 23 typedef List<BString, false> StringList; 24 25 26 /*! These are error codes that are sent back to the client from the server */ 27 28 #define ERROR_CODE_NONE 0 29 #define ERROR_CODE_VALIDATION -32800 30 #define ERROR_CODE_OBJECTNOTFOUND -32801 31 #define ERROR_CODE_CAPTCHABADRESPONSE -32802 32 #define ERROR_CODE_AUTHORIZATIONFAILURE -32803 33 #define ERROR_CODE_BADPKGICON -32804 34 #define ERROR_CODE_LIMITEXCEEDED -32805 35 #define ERROR_CODE_AUTHORIZATIONRULECONFLICT -32806 36 37 /*! This constant can be used to indicate the lack of a rating. */ 38 39 #define RATING_NONE -1 40 41 42 class WebAppInterface { 43 public: 44 WebAppInterface(); 45 WebAppInterface(const WebAppInterface& other); 46 virtual ~WebAppInterface(); 47 48 WebAppInterface& operator=(const WebAppInterface& other); 49 50 void SetAuthorization(const BString& username, 51 const BString& password); 52 const BString& Username() const 53 { return fUsername; } 54 55 status_t GetChangelog( 56 const BString& packageName, 57 BMessage& message); 58 59 status_t RetrieveUserRatings( 60 const BString& packageName, 61 const BString& architecture, 62 int resultOffset, int maxResults, 63 BMessage& message); 64 65 status_t RetrieveUserRating( 66 const BString& packageName, 67 const BPackageVersion& version, 68 const BString& architecture, 69 const BString& repositoryCode, 70 const BString& username, 71 BMessage& message); 72 73 status_t CreateUserRating( 74 const BString& packageName, 75 const BPackageVersion& version, 76 const BString& architecture, 77 const BString& repositoryCode, 78 const BString& languageCode, 79 const BString& comment, 80 const BString& stability, 81 int rating, 82 BMessage& message); 83 84 status_t UpdateUserRating( 85 const BString& ratingID, 86 const BString& languageCode, 87 const BString& comment, 88 const BString& stability, 89 int rating, bool active, 90 BMessage& message); 91 92 status_t RetrieveUserUsageConditions( 93 const BString& code, 94 UserUsageConditions& conditions); 95 96 status_t RetrieveScreenshot( 97 const BString& code, 98 int32 width, int32 height, 99 BDataIO* stream); 100 101 status_t RequestCaptcha(BMessage& message); 102 103 status_t CreateUser(const BString& nickName, 104 const BString& passwordClear, 105 const BString& email, 106 const BString& captchaToken, 107 const BString& captchaResponse, 108 const BString& languageCode, 109 BMessage& message); 110 111 status_t AuthenticateUser(const BString& nickName, 112 const BString& passwordClear, 113 BMessage& message); 114 115 static int32 ErrorCodeFromResponse(BMessage& response); 116 117 private: 118 status_t _SendRawGetRequest( 119 const BString urlPathComponents, 120 BDataIO* stream); 121 status_t _RetrieveUserUsageConditionsMeta( 122 const BString& code, BMessage& message); 123 status_t _RetrieveUserUsageConditionsCopy( 124 const BString& code, BDataIO* stream); 125 void _WriteStandardJsonRpcEnvelopeValues( 126 BJsonWriter& writer, 127 const char* methodName); 128 status_t _SendJsonRequest(const char* domain, 129 const BString& jsonString, uint32 flags, 130 BMessage& reply) const; 131 status_t _SendJsonRequest(const char* domain, 132 BPositionIO* requestData, 133 size_t requestDataSize, uint32 flags, 134 BMessage& reply) const; 135 static void _LogPayload(BPositionIO* requestData, 136 size_t size); 137 static off_t _LengthAndSeekToZero(BPositionIO* data); 138 139 private: 140 BString fUsername; 141 BString fPassword; 142 static int fRequestIndex; 143 }; 144 145 146 #endif // WEB_APP_INTERFACE_H 147