#pragma once // Spec: DevPlaceSwiftSDK.swift (DevPlaceRequest). One method per endpoint, callback style, all HTTP through // dp::NetworkService. Headers: Accept: application/json, Authorization: Bearer when given, Content-Type // urlencoded (default) / json / multipart. Success is any 2xx; other statuses are decoded by ApiErrorDecoder. // A body that does not decode into the model gives Error::Kind::Decode. Token parameters are optional where // Swift allowed nil. Path segments are percent encoded (Swift interpolated them raw). #include #include #include #include #include "api/ApiTypes.hpp" #include "core/Json.hpp" #include "model/Article.hpp" #include "model/ArticleDetail.hpp" #include "model/AuthToken.hpp" #include "model/BirthDate.hpp" #include "model/Bookmarks.hpp" #include "model/Feed.hpp" #include "model/Followers.hpp" #include "model/Following.hpp" #include "model/GistDetail.hpp" #include "model/Gists.hpp" #include "model/Leaderboard.hpp" #include "model/MessagesInbox.hpp" #include "model/MessagesWebSocketTicket.hpp" #include "model/News.hpp" #include "model/NotificationCounts.hpp" #include "model/NotificationOpen.hpp" #include "model/Notifications.hpp" #include "model/PostDetail.hpp" #include "model/PostTopic.hpp" #include "model/Profile.hpp" #include "model/ProjectDetail.hpp" #include "model/Projects.hpp" #include "model/PushProvider.hpp" #include "model/ReportReason.hpp" #include "model/TargetType.hpp" #include "model/UploadResponse.hpp" #include "model/UserSearch.hpp" #include "model/Vote.hpp" #include "net/NetworkService.hpp" namespace dp { class DevPlaceRequest { public: using Token = std::optional; using Str = std::optional; explicit DevPlaceRequest(NetworkService& network = NetworkService::instance()) : network_(network) {} // Auth CancelPtr getAuthToken(const std::string& email, const std::string& password, Done done); CancelPtr signUp(const std::string& username, const std::string& email, const std::string& password, const std::string& confirmPassword, const BirthDate& birthDate, bool acceptTerms, Done done); CancelPtr forgotPassword(const std::string& email, Done done); CancelPtr resetPassword(const std::string& resetToken, const std::string& password, const std::string& confirmPassword, Done done); // Feed and posts CancelPtr getFeed(const Str& tab, const Str& topic, const Str& search, const std::optional& before, const Token& token, Done done); CancelPtr getPost(const std::string& slug, const Token& token, Done done); CancelPtr createPost(const Str& title, const std::optional& topic, const std::string& content, const std::vector& attachments, const Str& pollQuestion, const std::vector& pollOptions, const Str& projectLink, const AuthToken& token, Done done); CancelPtr editPost(const std::string& slug, const std::string& title, PostTopic topic, const std::string& content, const std::string& projectLink, const AuthToken& token, Done done); CancelPtr deletePost(const std::string& slug, const AuthToken& token, Done done); // Comments CancelPtr createComment(TargetType targetType, const std::string& targetId, const std::string& content, const Str& parentId, const std::vector& attachments, const AuthToken& token, Done done); CancelPtr editComment(const std::string& uid, const std::string& content, const AuthToken& token, Done done); CancelPtr deleteComment(const std::string& uid, const AuthToken& token, Done done); // Projects, gists, news CancelPtr getProjects(const Str& tab, const Str& search, const Str& projectType, const std::optional& before, const Token& token, Done done); CancelPtr getProject(const std::string& slug, const Token& token, Done done); CancelPtr getGists(const Str& language, const Str& userId, const Str& search, const std::optional& before, const Token& token, Done done); CancelPtr getGist(const std::string& slug, const Token& token, Done done); CancelPtr getNews(const std::optional& before, const Token& token, Done done); CancelPtr getArticle(const std::string& slug, const Token& token, Done done); // Profile and social graph CancelPtr getProfile(const Str& username, const Str& tab, const Token& token, Done done); CancelPtr searchProfiles(const std::string& q, Done done); CancelPtr deleteAccount(const std::string& username, const std::string& password, const AuthToken& token, Done done); CancelPtr getFollowers(const std::string& username, const std::optional& page, const Token& token, Done done); CancelPtr getFollowing(const std::string& username, const std::optional& page, const Token& token, Done done); CancelPtr followUser(const std::string& username, const AuthToken& token, Done done); CancelPtr unfollowUser(const std::string& username, const AuthToken& token, Done done); CancelPtr blockUser(const std::string& username, const AuthToken& token, Done done); CancelPtr unblockUser(const std::string& username, const AuthToken& token, Done done); CancelPtr muteUser(const std::string& username, const AuthToken& token, Done done); CancelPtr unmuteUser(const std::string& username, const AuthToken& token, Done done); CancelPtr getLeaderboard(const Token& token, Done done); // Engagement (vote, poll choice and react are toggles on the server) CancelPtr vote(TargetType targetType, const std::string& targetId, Vote vote, const AuthToken& token, Done done); CancelPtr submitPollChoice(const std::string& pollId, const std::string& optionId, const AuthToken& token, Done done); CancelPtr react(TargetType targetType, const std::string& targetId, const std::string& emoji, const AuthToken& token, Done done); CancelPtr toggleBookmark(TargetType targetType, const std::string& targetId, const AuthToken& token, Done done); CancelPtr getBookmarks(const std::optional& before, const AuthToken& token, Done done); // Reports CancelPtr getReportReasons(Done> done); CancelPtr report(TargetType targetType, const std::string& targetId, const std::string& reason, const Str& detail, const AuthToken& token, Done done); // Messaging CancelPtr getMessages(const Str& withUid, const Str& search, const AuthToken& token, Done done); CancelPtr sendMessage(const std::string& receiverId, const std::string& content, const std::vector& attachments, const AuthToken& token, Done done); CancelPtr searchMessageRecipients(const std::string& q, const AuthToken& token, Done done); CancelPtr getMessagesWebSocketTicket(const AuthToken& token, Done done); // wss:// (or ws://) from the network base URL, trailing "/", "messages/ws?ticket=" + ticket (urlQueryAllowed). std::string messagesWebSocketUrl(const std::string& ticket) const; // Notifications CancelPtr getNotifications(const std::optional& before, const AuthToken& token, Done done); CancelPtr openNotification(const std::string& uid, const AuthToken& token, Done done); CancelPtr getNotificationCounts(const AuthToken& token, Done done); CancelPtr markNotificationRead(const std::string& uid, const AuthToken& token, Done done); CancelPtr markAllNotificationsRead(const AuthToken& token, Done done); // Push CancelPtr registerPushDevice(PushProvider provider, const std::string& deviceToken, const AuthToken& token, Done done); // Uploads (multipart for files, JSON for urls) CancelPtr uploadFile(const std::string& data, const std::string& filename, const std::string& mimeType, const AuthToken& token, Done done); CancelPtr uploadFromUrl(const std::string& url, const Str& filename, const AuthToken& token, Done done); CancelPtr deleteAttachment(const std::string& uid, const AuthToken& token, Done done); private: enum class BodyKind { UrlEncoded, Json, Multipart }; using Query = std::vector>; struct Spec { const char* method = "GET"; std::string path; Query query; BodyKind kind = BodyKind::UrlEncoded; std::string body; std::string boundary; Token token; }; static Spec makeGet(std::string path, Query query, const Token& token); static Spec makePost(std::string path, const Token& token); // urlencoded, no body static Spec makePostJson(std::string path, const Json& body, const Token& token); static std::string seg(const std::string& segment); static void addBefore(Query& query, const std::optional& before); static void addOpt(Query& query, const char* key, const Str& value); static Error decodeError(); CancelPtr exchange(Spec spec, Done done); CancelPtr perform(Spec spec, Done done); template CancelPtr fetch(Spec spec, Done done) { return exchange(std::move(spec), [done = std::move(done)](Result r) { if (!r.ok()) return done(Result(r.error())); try { done(Result(T::fromJson(Json::parse(r.value())))); } catch (const std::exception&) { done(Result(decodeError())); } }); } NetworkService& network_; }; } // namespace dp