xref: /haiku/src/apps/haikudepot/server/WebAppInterface.h (revision 12dba4e70f831d6d27a7f769cc9dab19c19a155d)
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 /*! This constant can be used to indicate the lack of a rating. */
37 
38 #define RATING_NONE -1
39 
40 
41 class WebAppInterface {
42 public:
43 								WebAppInterface();
44 								WebAppInterface(const WebAppInterface& other);
45 	virtual						~WebAppInterface();
46 
47 			WebAppInterface&	operator=(const WebAppInterface& other);
48 
49 			void				SetAuthorization(const BString& username,
50 									const BString& password);
51 			const BString&		Username() const
52 									{ return fUsername; }
53 
54 			void				SetPreferredLanguage(const BString& language);
55 			void				SetArchitecture(const BString& architecture);
56 
57 			status_t			GetChangelog(
58 									const BString& packageName,
59 									BMessage& message);
60 
61 			status_t			RetrieveUserRatings(
62 									const BString& packageName,
63 									const BString& architecture,
64 									int resultOffset, int maxResults,
65 									BMessage& message);
66 
67 			status_t			RetrieveUserRating(
68 									const BString& packageName,
69 									const BPackageVersion& version,
70 									const BString& architecture,
71 									const BString& repositoryCode,
72 									const BString& username,
73 									BMessage& message);
74 
75 			status_t			CreateUserRating(
76 									const BString& packageName,
77 									const BPackageVersion& version,
78 									const BString& architecture,
79 									const BString& repositoryCode,
80 									const BString& languageCode,
81 									const BString& comment,
82 									const BString& stability,
83 									int rating,
84 									BMessage& message);
85 
86 			status_t			UpdateUserRating(
87 									const BString& ratingID,
88 									const BString& languageCode,
89 									const BString& comment,
90 									const BString& stability,
91 									int rating, bool active,
92 									BMessage& message);
93 
94 			status_t			RetrieveScreenshot(
95 									const BString& code,
96 									int32 width, int32 height,
97 									BDataIO* stream);
98 
99 			status_t			RequestCaptcha(BMessage& message);
100 
101 			status_t			CreateUser(const BString& nickName,
102 									const BString& passwordClear,
103 									const BString& email,
104 									const BString& captchaToken,
105 									const BString& captchaResponse,
106 									const BString& languageCode,
107 									BMessage& message);
108 
109 			status_t			AuthenticateUser(const BString& nickName,
110 									const BString& passwordClear,
111 									BMessage& message);
112 
113 	static int32				ErrorCodeFromResponse(BMessage& response);
114 
115 private:
116 			void				_WriteStandardJsonRpcEnvelopeValues(
117 									BJsonWriter& writer,
118 									const char* methodName);
119 			status_t			_SendJsonRequest(const char* domain,
120 									const BString& jsonString, uint32 flags,
121 									BMessage& reply) const;
122 			status_t			_SendJsonRequest(const char* domain,
123 									BPositionIO* requestData,
124 									size_t requestDataSize, uint32 flags,
125 									BMessage& reply) const;
126 	static	void				_LogPayload(BPositionIO* requestData,
127 									size_t size);
128 	static	off_t				_LengthAndSeekToZero(BPositionIO* data);
129 
130 private:
131 			BString				fUsername;
132 			BString				fPassword;
133 			BString				fLanguage;
134 	static	int					fRequestIndex;
135 };
136 
137 
138 #endif // WEB_APP_INTERFACE_H
139