1 /* 2 * Copyright 2014, Stephan Aßmus <superstippi@gmx.de>. 3 * Copyright 2016-2018, 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 17 18 class BDataIO; 19 class BMessage; 20 using BPackageKit::BPackageVersion; 21 22 typedef List<BString, false> StringList; 23 24 25 /*! These are error codes that are sent back to the client from the server */ 26 27 #define ERROR_CODE_NONE 0 28 #define ERROR_CODE_VALIDATION -32800 29 #define ERROR_CODE_OBJECTNOTFOUND -32801 30 #define ERROR_CODE_CAPTCHABADRESPONSE -32802 31 #define ERROR_CODE_AUTHORIZATIONFAILURE -32803 32 #define ERROR_CODE_BADPKGICON -32804 33 #define ERROR_CODE_LIMITEXCEEDED -32805 34 #define ERROR_CODE_AUTHORIZATIONRULECONFLICT -32806 35 36 37 class WebAppInterface { 38 public: 39 WebAppInterface(); 40 WebAppInterface(const WebAppInterface& other); 41 virtual ~WebAppInterface(); 42 43 WebAppInterface& operator=(const WebAppInterface& other); 44 45 void SetAuthorization(const BString& username, 46 const BString& password); 47 const BString& Username() const 48 { return fUsername; } 49 50 void SetPreferredLanguage(const BString& language); 51 void SetArchitecture(const BString& architecture); 52 53 status_t GetChangelog( 54 const BString& packageName, 55 BMessage& message); 56 57 status_t RetrieveUserRatings( 58 const BString& packageName, 59 const BString& architecture, 60 int resultOffset, int maxResults, 61 BMessage& message); 62 63 status_t RetrieveUserRating( 64 const BString& packageName, 65 const BPackageVersion& version, 66 const BString& architecture, 67 const BString& repositoryCode, 68 const BString& username, 69 BMessage& message); 70 71 status_t CreateUserRating( 72 const BString& packageName, 73 const BPackageVersion& version, 74 const BString& architecture, 75 const BString& repositoryCode, 76 const BString& languageCode, 77 const BString& comment, 78 const BString& stability, 79 int rating, 80 BMessage& message); 81 82 status_t UpdateUserRating( 83 const BString& ratingID, 84 const BString& languageCode, 85 const BString& comment, 86 const BString& stability, 87 int rating, bool active, 88 BMessage& message); 89 90 status_t RetrieveScreenshot( 91 const BString& code, 92 int32 width, int32 height, 93 BDataIO* stream); 94 95 status_t RequestCaptcha(BMessage& message); 96 97 status_t CreateUser(const BString& nickName, 98 const BString& passwordClear, 99 const BString& email, 100 const BString& captchaToken, 101 const BString& captchaResponse, 102 const BString& languageCode, 103 BMessage& message); 104 105 status_t AuthenticateUser(const BString& nickName, 106 const BString& passwordClear, 107 BMessage& message); 108 109 static int32 ErrorCodeFromResponse(BMessage& response); 110 111 private: 112 void _WriteStandardJsonRpcEnvelopeValues( 113 BJsonWriter& writer, 114 const char* methodName); 115 status_t _SendJsonRequest(const char* domain, 116 BString jsonString, uint32 flags, 117 BMessage& reply) const; 118 status_t _SendJsonRequest(const char* domain, 119 BDataIO* requestData, 120 size_t requestDataSize, uint32 flags, 121 BMessage& reply) const; 122 static void _LogPayload(const char* data, ssize_t size); 123 124 private: 125 BString fUsername; 126 BString fPassword; 127 BString fLanguage; 128 static int fRequestIndex; 129 }; 130 131 132 #endif // WEB_APP_INTERFACE_H 133