Twitch SDK (Internal)
coreapi.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * Twitch Broadcasting SDK
3 *
4 * This software is supplied under the terms of a license agreement with Twitch Interactive, Inc. and
5 * may not be copied or used except in accordance with the terms of that agreement
6 * Copyright (c) 2012-2016 Twitch Interactive, Inc.
7 *********************************************************************************************/
8 
9 #pragma once
10 
17 #include "twitchsdk/core/module.h"
19 
20 #include <memory>
21 
22 namespace ttv
23 {
24  class IChannelListener;
25  class ICoreApiClient;
26  class ICoreAPIListener;
27  class ChannelRepository;
28  class IChannelStatus;
29  class CoreAPI;
30  class SettingRepository;
31  class TaskRunner;
32  class User;
33  class UserRepository;
34  struct CoreAPIInternalData;
35 
40  TTV_ErrorCode SetClientId(const std::string& clientId);
44  TTV_ErrorCode GetClientId(std::string& clientId);
45 }
46 
47 
52 {
53 public:
55 
56  virtual std::string GetClientName() = 0;
57 
58  virtual void GetRequiredOAuthScopes(std::vector<std::string>& scopes);
59 
60  virtual void CoreUserLoggedIn(std::shared_ptr<User> user);
61  virtual void CoreUserLoggedOut(std::shared_ptr<User> user);
62 
63  virtual void CoreLocalLanguageChanged(const std::string& language);
64 };
65 
66 
73 class ttv::CoreAPI : public ModuleBase
74 {
75 public:
76  using FetchUserInfoCallback = std::function<void(TTV_ErrorCode ec, const UserInfo& userInfo)>;
77  using FetchChannelInfoCallback = std::function<void(TTV_ErrorCode ec, const ChannelInfo& channelInfo)>;
78  using FetchStreamInfoCallback = std::function<void(TTV_ErrorCode ec, const StreamInfo& streamInfo)>;
79  using LogInCallback = std::function<void(TTV_ErrorCode ec, const UserInfo& userInfo)>;
80  using LogOutCallback = std::function<void(TTV_ErrorCode ec)>;
81  using UploadProfileImageCallback = std::function<void(TTV_ErrorCode ec, const std::vector<ProfileImage>& images)>;
82 
83 public:
84  CoreAPI();
85  virtual ~CoreAPI();
86 
87  // IModule implementation
88  virtual std::string GetModuleName() const override;
89  virtual TTV_ErrorCode Initialize(const InitializeCallback& callback) override;
90  virtual TTV_ErrorCode Shutdown(const ShutdownCallback& callback) override;
91  virtual TTV_ErrorCode Update() override;
92 
93  TTV_ErrorCode SetListener(std::shared_ptr<ICoreAPIListener> listener);
94 
95  std::shared_ptr<UserRepository> GetUserRepository() const { return mUserRepository; }
96  std::shared_ptr<ChannelRepository> GetChannelRepository() const { return mChannelRepository; }
97  std::shared_ptr<SettingRepository> GetSettingRepository() const { return mSettingRepository; }
98  std::shared_ptr<TrackingContext> GetTrackingContext() const { return mTrackingContext; }
102  TTV_ErrorCode GetClientId(std::string& clientId);
106  TTV_ErrorCode SetLocalLanguage(const std::string& language);
110  TTV_ErrorCode GetLocalLanguage(std::string& language);
114  TTV_ErrorCode FetchUserInfoById(UserId userId, FetchUserInfoCallback callback);
118  TTV_ErrorCode FetchUserInfoByName(const std::string& userName, FetchUserInfoCallback callback);
122  TTV_ErrorCode FetchChannelInfoById(ChannelId channelId, const FetchChannelInfoCallback& callback);
126  TTV_ErrorCode FetchChannelInfoByName(const std::string& channelName, const FetchChannelInfoCallback& callback);
135  TTV_ErrorCode FetchStreamInfoById(ChannelId channelId, const FetchStreamInfoCallback& callback);
144  TTV_ErrorCode FetchStreamInfoByName(const std::string& channelName, const FetchStreamInfoCallback& callback);
157  TTV_ErrorCode CreateChannelStatus(UserId userId, ChannelId channelId, const std::shared_ptr<IChannelListener>& listener, std::shared_ptr<IChannelStatus>& result);
161  TTV_ErrorCode GetRequiredOAuthScopes(std::vector<std::string>& modules, std::vector<std::string>& scopes);
162 
163  TTV_ErrorCode LogIn(const std::string& oauthToken, LogInCallback callback);
164  TTV_ErrorCode LogOut(UserId userId, LogOutCallback callback);
165 
166  TTV_ErrorCode ConnectPubSub(UserId userId);
167  TTV_ErrorCode DisconnectPubSub(UserId userId);
168 
169  // Reference counting so that CoreAPI isn't shutdown while other systems are using it
170  TTV_ErrorCode RegisterClient(std::shared_ptr<ICoreApiClient> client);
171  TTV_ErrorCode UnregisterClient(std::shared_ptr<ICoreApiClient> client);
172 
173  TTV_ErrorCode SetGlobalSetting(const std::string& key, const std::string& value);
174  TTV_ErrorCode RemoveGlobalSetting(const std::string& key);
175  TTV_ErrorCode GetGlobalSetting(const std::string& key, std::string& value);
176 
177 private:
179  {
180  public:
181  PubSubListener(CoreAPI* api);
182 
183  // PubSubClient::Listener implementation
184  virtual void OnStateChanged(PubSubClient* source, PubSubState state, TTV_ErrorCode ec) override;
185 
186  private:
188  };
189 
191  {
192  public:
193  UserListener(CoreAPI* owner);
194 
195  // IUserListener implementation
196  virtual void OnUserLogInComplete(User* source, TTV_ErrorCode ec) override;
197  virtual void OnUserLogOutComplete(User* source, TTV_ErrorCode ec) override;
198  virtual void OnUserInfoFetchComplete(User* source, TTV_ErrorCode ec) override;
199  virtual void OnUserAuthenticationIssue(User* source, std::shared_ptr<const OAuthToken> oauthToken, TTV_ErrorCode ec) override;
200 
201  private:
203  };
204 
205  // PubSubClient::Listener implementation
206  void OnPubSubStateChanged(PubSubClient* source, PubSubState state, TTV_ErrorCode ec);
207 
208  // IUserListener implementation
209  void OnUserLogInComplete(User* source, TTV_ErrorCode ec);
210  void OnUserLogOutComplete(User* source, TTV_ErrorCode ec);
211  void OnUserInfoFetchComplete(User* source, TTV_ErrorCode ec);
212  void OnUserAuthenticationIssue(User* source, std::shared_ptr<const OAuthToken> oauthToken, TTV_ErrorCode ec);
213 
214  // ModuleBase overrides
215  virtual bool CheckShutdown() override;
216  virtual void CompleteShutdown() override;
217 
218  TTV_ErrorCode DisposeChannelStatus(const std::shared_ptr<IChannelStatus>& channelStatus);
219 
220  void InitializeAnonymousUser();
221  TTV_ErrorCode ValidateOAuthToken(const std::string& oauth, const std::function<void(TTV_ErrorCode ec)>& callback);
222 
223  std::shared_ptr<ChannelRepository> mChannelRepository;
224  std::shared_ptr<UserRepository> mUserRepository;
225  std::shared_ptr<TrackingContext> mTrackingContext;
226  std::shared_ptr<UserListener> mUserListener;
227  std::shared_ptr<PubSubListener> mPubSubListener;
228  std::shared_ptr<TaskRunner> mTaskRunner;
229  std::shared_ptr<SettingRepository> mSettingRepository;
230  std::shared_ptr<CoreAPIInternalData> mInternalData;
231 
232  std::vector<std::shared_ptr<ICoreApiClient>> mApiClients;
233  std::string mLocalLanguage;
234 };
std::shared_ptr< CoreAPIInternalData > mInternalData
Definition: coreapi.h:230
uint32_t UserId
Definition: coretypes.h:22
std::shared_ptr< UserRepository > mUserRepository
Definition: coreapi.h:224
TTV_ErrorCode GetClientId(std::string &clientId)
Definition: coreapi.h:51
CoreAPI * mOwner
Definition: coreapi.h:202
std::shared_ptr< TrackingContext > mTrackingContext
Definition: coreapi.h:225
std::function< void(TTV_ErrorCode ec)> LogOutCallback
Definition: coreapi.h:80
CoreAPI * mOwner
Definition: coreapi.h:187
std::shared_ptr< UserListener > mUserListener
Definition: coreapi.h:226
std::function< void(TTV_ErrorCode ec, const StreamInfo &streamInfo)> FetchStreamInfoCallback
Definition: coreapi.h:78
virtual void CoreLocalLanguageChanged(const std::string &language)
std::function< void(TTV_ErrorCode ec)> InitializeCallback
Definition: module.h:48
Definition: userlistener.h:24
std::function< void(TTV_ErrorCode ec, const ChannelInfo &channelInfo)> FetchChannelInfoCallback
Definition: coreapi.h:77
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
std::shared_ptr< ChannelRepository > mChannelRepository
Definition: coreapi.h:223
virtual std::string GetClientName()=0
Definition: coreapi.h:190
std::function< void(TTV_ErrorCode ec, const std::vector< ProfileImage > &images)> UploadProfileImageCallback
Definition: coreapi.h:81
std::shared_ptr< UserRepository > GetUserRepository() const
Definition: coreapi.h:95
std::shared_ptr< SettingRepository > mSettingRepository
Definition: coreapi.h:229
Definition: pubsubclient.h:86
Definition: coreapi.h:73
uint32_t TTV_ErrorCode
Definition: errortypes.h:30
PubSubState
Definition: coretypes.h:152
virtual void CoreUserLoggedIn(std::shared_ptr< User > user)
std::shared_ptr< TaskRunner > mTaskRunner
Definition: coreapi.h:228
TTV_ErrorCode SetClientId(const std::string &clientId)
std::shared_ptr< SettingRepository > GetSettingRepository() const
Definition: coreapi.h:97
std::function< void(TTV_ErrorCode ec)> ShutdownCallback
Definition: module.h:49
std::shared_ptr< PubSubListener > mPubSubListener
Definition: coreapi.h:227
std::function< void(TTV_ErrorCode ec, const UserInfo &userInfo)> LogInCallback
Definition: coreapi.h:79
std::vector< std::shared_ptr< ICoreApiClient > > mApiClients
Definition: coreapi.h:232
Definition: module.h:85
std::shared_ptr< TrackingContext > GetTrackingContext() const
Definition: coreapi.h:98
uint32_t ChannelId
Definition: coretypes.h:23
std::function< void(TTV_ErrorCode ec, const UserInfo &userInfo)> FetchUserInfoCallback
Definition: coreapi.h:76
virtual void GetRequiredOAuthScopes(std::vector< std::string > &scopes)
Definition: user.h:33
virtual void CoreUserLoggedOut(std::shared_ptr< User > user)
Definition: pubsubclient.h:107
Definition: coreapi.h:178
std::shared_ptr< ChannelRepository > GetChannelRepository() const
Definition: coreapi.h:96
std::string mLocalLanguage
Definition: coreapi.h:233