12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <AzCore/std/time.h>
- #include <AzCore/std/smart_ptr/make_shared.h>
- #include <AzCore/std/string/conversions.h>
- #include <Twitch/TwitchBus.h>
- #include <HttpRequestor/HttpRequestorBus.h>
- #include "TwitchREST.h"
- namespace Twitch
- {
- ITwitchRESTPtr ITwitchREST::Alloc()
- {
- return AZStd::make_shared<TwitchREST>();
- }
- const char * TwitchREST::kProtocol("https");
- const char * TwitchREST::kBasePath("api.twitch.tv");
- const char * TwitchREST::kVer("v5");
- const char * TwitchREST::kKraken("kraken");
- const char * TwitchREST::kAuthType("OAuth ");
- const char * TwitchREST::kAcceptType("application/vnd.twitchtv.v5+json");
- TwitchREST::TwitchREST()
- {
- // all names listed below comply with Twitch's naming rules, Do not change the case or
- // spelling of the return values! Also do not put in the ::Unknown strings, placehold only!
- m_availabilityMap[PresenceAvailability::Idle] = "idle";
- m_availabilityMap[PresenceAvailability::Online] = "online";
-
- m_activityTypeMap[PresenceActivityType::Watching] = "watching";
- m_activityTypeMap[PresenceActivityType::Playing] = "playing";
- m_activityTypeMap[PresenceActivityType::Broadcasting] = "broadcasting";
- }
- void TwitchREST::FlushEvents()
- {
- TwitchNotifyBus::ExecuteQueuedEvents();
- }
- void TwitchREST::GetUser(ReceiptID& receipt)
- {
- AZStd::string url( BuildKrakenURL("user") );
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_GET, GetDefaultHeaders(), [receipt, this](const Aws::Utils::Json::JsonView& json, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- UserInfo userinfo;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- SafeGetUserInfo(userinfo, json);
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::GetUser, UserInfoValue(userinfo, receipt, rc));
- });
- }
-
- void TwitchREST::ResetFriendsNotificationCount(const ReceiptID& receipt, const AZStd::string& friendID)
- {
- AZStd::string url( BuildBaseURL("users", friendID) + "/friends/notifications");
-
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_DELETE, GetDefaultHeaders(), [receipt](const Aws::Utils::Json::JsonView& /*json*/, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- if (httpCode == Aws::Http::HttpResponseCode::NO_CONTENT) // 204: NO_CONTENT The server successfully processed the request and is not returning any content
- {
- rc = ResultCode::Success;
- }
-
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::ResetFriendsNotificationCountNotify, Int64Value(static_cast<AZ::s64>(httpCode), receipt, rc));
- });
- }
-
- void TwitchREST::GetFriendNotificationCount(const ReceiptID& receipt, const AZStd::string& friendID)
- {
- AZStd::string url(BuildBaseURL("users", friendID) + "/friends/notifications");
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_GET, GetDefaultHeaders(), [receipt](const Aws::Utils::Json::JsonView& json, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- AZ::s64 count = 0;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- count = json.GetInteger("count");
- }
- else
- {
- count = static_cast<AZ::s64>(httpCode);
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::GetFriendNotificationCount, Int64Value(count, receipt, rc));
- });
- }
- void TwitchREST::GetFriendRecommendations(const ReceiptID& receipt, const AZStd::string& friendID)
- {
- AZStd::string url(BuildBaseURL("users", friendID) + "/friends/recommendations");
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_GET, GetDefaultHeaders(), [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- FriendRecommendationList returnRecommendations;
-
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- Aws::Utils::Array<Aws::Utils::Json::JsonView> recommendations = jsonDoc.GetArray("recommendations");
- for(size_t index =0; index<recommendations.GetLength(); index++)
- {
- FriendRecommendation fr;
- Aws::Utils::Json::JsonView item = recommendations.GetItem(index);
- fr.Reason = item.GetString("reason").c_str();
- SafeGetUserInfoFromUserContainer(fr.User, item);
- returnRecommendations.push_back(fr);
- }
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::GetFriendRecommendations, FriendRecommendationValue(returnRecommendations, receipt, rc));
- });
- }
- void TwitchREST::GetFriends(const ReceiptID& receipt, const AZStd::string& friendID, const AZStd::string& cursor)
- {
- AZStd::string url(BuildBaseURL("users", friendID) + "/friends/relationships");
- HttpRequestor::Headers headers( GetDefaultHeaders() );
- AddToHeader(headers, "limit", 256ULL);
- if( !cursor.empty() )
- AddToHeader(headers, "cursor", cursor);
-
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_GET, headers, [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- GetFriendReturn friendReturn;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- SafeGetJSONString(friendReturn.Cursor, "cursor", jsonDoc);
- Aws::Utils::Array<Aws::Utils::Json::JsonView> recommendations = jsonDoc.GetArray("friends");
- for (size_t index = 0; index < recommendations.GetLength(); index++)
- {
- FriendInfo fi;
- Aws::Utils::Json::JsonView item = recommendations.GetItem(index);
- SafeGetJSONString(fi.CreatedDate, "created_at", item);
- SafeGetUserInfoFromUserContainer(fi.User, item);
- friendReturn.Friends.push_back(fi);
- }
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::GetFriends, GetFriendValue(friendReturn, receipt, rc));
- });
- }
- void TwitchREST::GetFriendStatus(const ReceiptID& receipt, const AZStd::string& sourceFriendID, const AZStd::string& targetFriendID)
- {
- AZStd::string url(BuildBaseURL("users", sourceFriendID) + "/friends/relationships/" + targetFriendID);
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_GET, GetDefaultHeaders(), [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- FriendStatus friendStatus;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- SafeGetJSONString(friendStatus.Status, "status", jsonDoc);
- SafeGetUserInfoFromUserContainer(friendStatus.User, jsonDoc);
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::GetFriendStatus, FriendStatusValue(friendStatus, receipt, rc));
- });
- }
- void TwitchREST::AcceptFriendRequest(const ReceiptID& receipt, const AZStd::string& friendID)
- {
- AZStd::string url(BuildBaseURL("users") + "/friends/relationships/" + friendID);
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_PUT, GetDefaultHeaders(), [receipt]([[maybe_unused]] const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- if (httpCode == Aws::Http::HttpResponseCode::CREATED)
- {
- rc = ResultCode::Success;
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::AcceptFriendRequest, Int64Value(static_cast<AZ::s64>(httpCode), receipt, rc));
- });
- }
- void TwitchREST::GetFriendRequests(const ReceiptID& receipt, const AZStd::string& cursor)
- {
- AZStd::string url(BuildBaseURL("users") + "/friends/requests");
- HttpRequestor::Headers headers(GetDefaultHeaders());
- AddToHeader(headers, "limit", 512ULL);
- if (!cursor.empty())
- AddToHeader(headers, "cursor", cursor);
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_GET, headers, [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- FriendRequestResult requestResult;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- SafeGetJSONString(requestResult.Cursor, "cursor", jsonDoc);
- SafeGetJSONu64(requestResult.Total, "total", jsonDoc);
- Aws::Utils::Array<Aws::Utils::Json::JsonView> recommendations = jsonDoc.GetArray("requests");
- for (size_t index = 0; index < recommendations.GetLength(); index++)
- {
- Aws::Utils::Json::JsonView item = recommendations.GetItem(index);
- FriendRequest fr;
- SafeGetJSONbool(fr.IsRecommended, "is_recommended", item);
- SafeGetJSONbool(fr.IsStranger, "is_stranger", item);
- SafeGetJSONString(fr.NonStrangerReason, "non_stranger_reason", item);
- SafeGetJSONString(fr.RequestedDate, "requested_at", item);
- SafeGetUserInfoFromUserContainer(fr.User, item);
- requestResult.Requests.push_back(fr);
- }
- }
-
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::GetFriendRequests, FriendRequestValue(requestResult, receipt, rc));
- });
- }
- void TwitchREST::CreateFriendRequest(const ReceiptID& receipt, const AZStd::string& friendID)
- {
- AZStd::string url(BuildBaseURL("users") + "/friends/requests/" + friendID);
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_PUT, GetDefaultHeaders(), [receipt]([[maybe_unused]] const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- if (httpCode == Aws::Http::HttpResponseCode::CREATED)
- {
- rc = ResultCode::Success;
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::CreateFriendRequest, Int64Value(static_cast<AZ::s64>(httpCode), receipt, rc));
- });
- }
- void TwitchREST::DeclineFriendRequest(const ReceiptID& receipt, const AZStd::string& friendID)
- {
- AZStd::string url(BuildBaseURL("users") + "/friends/requests/" + friendID);
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_DELETE, GetDefaultHeaders(), [receipt]([[maybe_unused]] const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- if (httpCode == Aws::Http::HttpResponseCode::CREATED)
- {
- rc = ResultCode::Success;
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::DeclineFriendRequest, Int64Value(static_cast<AZ::s64>(httpCode), receipt, rc));
- });
- }
-
- void TwitchREST::UpdatePresenceStatus(const ReceiptID& receipt, PresenceAvailability availability, PresenceActivityType activityType, const AZStd::string& gameContext)
- {
- // we need to get the twitch channel this user is on, and that call requires its own receipt
- ReceiptID gcReceipt;
- InternalGetChannel(gcReceipt, [receipt, availability, activityType, gameContext, this](const ChannelInfo& channelInfo, const ReceiptID&, ResultCode)
- {
- AZStd::string url(BuildBaseURL("users") + "/status");
- HttpRequestor::Headers headers(GetDefaultHeaders());
- AddToHeader(headers, "Content-Type", "application/json");
-
- AZStd::string appID;
- TwitchRequestBus::BroadcastResult(appID, &TwitchRequests::GetApplicationID);
- Aws::Utils::Json::JsonValue jsonActivity;
- jsonActivity.WithString("type", GetPresenceActivityTypeName(activityType).c_str());
- jsonActivity.WithString("channel_id", channelInfo.Id.c_str());
- jsonActivity.WithString("game_id", appID.c_str());
- if ( (activityType == PresenceActivityType::Playing) && !gameContext.empty() )
- {
- Aws::Utils::Json::JsonValue jsonGameContext(Aws::String(gameContext.c_str()));
- if( jsonGameContext.WasParseSuccessful() )
- jsonActivity.WithObject("game_context", AZStd::move(jsonGameContext));
- }
-
- AZStd::string sessionID;
- TwitchRequestBus::BroadcastResult(sessionID, &TwitchRequests::GetSessionID);
- Aws::Utils::Json::JsonValue jsonBody;
- jsonBody.WithString("session_id", sessionID.c_str());
- jsonBody.WithString("availability", GetPresenceAvailabilityName(availability).c_str());
- jsonBody.WithObject("activities", AZStd::move(jsonActivity));
- Aws::String body(jsonBody.View().WriteCompact());
-
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_POST, headers, body.c_str(), [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- AZ::s64 pollIntervalSeconds = 0;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- SafeGetJSONs64(pollIntervalSeconds, "poll_interval_seconds", jsonDoc);
- }
- else
- {
- pollIntervalSeconds = static_cast<AZ::s64>(httpCode);
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::UpdatePresenceStatus, Int64Value(pollIntervalSeconds, receipt, rc));
- });
- });
- }
- void TwitchREST::GetPresenceStatusofFriends(const ReceiptID& receipt)
- {
- AZStd::string url(BuildBaseURL("users") + "/status/friends");
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_GET, GetDefaultHeaders(), [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- PresenceStatusList statusList;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- Aws::Utils::Array<Aws::Utils::Json::JsonView> recommendations = jsonDoc.GetArray("data");
- for (size_t index = 0; index < recommendations.GetLength(); index++)
- {
- Aws::Utils::Json::JsonView item = recommendations.GetItem(index);
- PresenceStatus ps;
- SafeGetJSONs64(ps.Index, "index", item);
- SafeGetJSONs64(ps.UpdatedDate, "UpdatedDate", item);
- SafeGetJSONString(ps.UserID, "user_id", item);
- SafeGetPresenceActivityType(ps.ActivityType, item);
- SafeGetPresenceAvailability(ps.Availability, item);
- statusList.push_back(ps);
- }
- }
-
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::GetPresenceStatusofFriends, PresenceStatusValue(statusList, receipt, rc));
- });
- }
- void TwitchREST::GetPresenceSettings(const ReceiptID& receipt)
- {
- AZStd::string url(BuildBaseURL("users") + "/status/settings");
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_GET, GetDefaultHeaders(), [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- PresenceSettings presenceSettings;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- SafeGetJSONbool(presenceSettings.IsInvisible, "is_invisible", jsonDoc);
- SafeGetJSONbool(presenceSettings.ShareActivity, "share_activity", jsonDoc);
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::GetPresenceSettings, PresenceSettingsValue(presenceSettings, receipt, rc));
- });
- }
- void TwitchREST::UpdatePresenceSettings(const ReceiptID& receipt, bool isInvisible, bool shareActivity)
- {
- AZStd::string url(BuildBaseURL("users") + "/status/settings");
- HttpRequestor::Headers headers(GetDefaultHeaders());
- AddToHeader(headers, "Content-Type", "application/json");
- Aws::Utils::Json::JsonValue jsonBody;
- jsonBody.WithBool("is_invisible", isInvisible);
- jsonBody.WithBool("share_activity", shareActivity);
- Aws::String body(jsonBody.View().WriteCompact());
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_POST, headers, body.c_str(), [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- PresenceSettings presenceSettings;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- SafeGetJSONbool(presenceSettings.IsInvisible, "is_invisible", jsonDoc);
- SafeGetJSONbool(presenceSettings.ShareActivity, "share_activity", jsonDoc);
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::UpdatePresenceSettings, PresenceSettingsValue(presenceSettings, receipt, rc));
- });
- }
- void TwitchREST::GetChannel(const ReceiptID& receipt)
- {
- InternalGetChannel(receipt, [](const ChannelInfo& channelInfo, const ReceiptID& receipt, ResultCode rc)
- {
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::GetChannel, ChannelInfoValue(channelInfo, receipt, rc));
- });
- }
- void TwitchREST::GetChannelbyID(const ReceiptID& receipt, const AZStd::string& channelID)
- {
- AZStd::string url(BuildKrakenURL("channels") + "/" + (channelID.empty() ? "0000000" : channelID));
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_GET, GetClientIDHeader(), [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- ChannelInfo channelInfo;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- channelInfo.NumItemsRecieved = SafeGetChannelInfo(channelInfo, jsonDoc);
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::GetChannelbyID, ChannelInfoValue(channelInfo, receipt, rc));
- });
- }
- void TwitchREST::UpdateChannel(const ReceiptID& receipt, const ChannelUpdateInfo& channelUpdateInfo)
- {
- /*
- ** sanity check here, at least once of these must be set to update.
- */
- if( ( !channelUpdateInfo.ChannelFeedEnabled.ToBeUpdated() ) &&
- ( !channelUpdateInfo.Delay.ToBeUpdated()) &&
- ( !channelUpdateInfo.GameName.ToBeUpdated()) &&
- ( !channelUpdateInfo.Status.ToBeUpdated()))
- {
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::UpdateChannel, ChannelInfoValue(ChannelInfo(), receipt, ResultCode::TwitchChannelNoUpdatesToMake));
- }
- // we need to get the twitch channel this user is on, and that call requires its own receipt
- ReceiptID gcReceipt;
-
- InternalGetChannel(gcReceipt, [receipt, channelUpdateInfo, this](const ChannelInfo& channelInfo, const ReceiptID&, ResultCode)
- {
- AZStd::string url(BuildKrakenURL("channels") + "/" + channelInfo.Id);
- HttpRequestor::Headers headers(GetDefaultHeaders());
- AddToHeader(headers, "Content-Type", "application/json");
- Aws::Utils::Json::JsonValue jsonChannel;
- if (channelUpdateInfo.Status.ToBeUpdated())
- {
- jsonChannel.WithString("status", channelUpdateInfo.Status.GetValue().c_str());
- }
- if (channelUpdateInfo.GameName.ToBeUpdated())
- {
- jsonChannel.WithString("game", channelUpdateInfo.GameName.GetValue().c_str());
- }
-
- if (channelUpdateInfo.Delay.ToBeUpdated())
- {
- jsonChannel.WithString("delay", AZStd::to_string(channelUpdateInfo.Delay.GetValue()).c_str());
- }
-
- if (channelUpdateInfo.ChannelFeedEnabled.ToBeUpdated())
- {
- jsonChannel.WithBool("channel_feed_enabled", channelUpdateInfo.ChannelFeedEnabled.GetValue());
- }
-
- Aws::Utils::Json::JsonValue jsonBody;
- jsonBody.WithObject("channel", AZStd::move(jsonChannel));
- Aws::String body(jsonBody.View().WriteCompact());
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_PUT, headers, body.c_str(), [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- ChannelInfo retChannelInfo;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- retChannelInfo.NumItemsRecieved = SafeGetChannelInfo(retChannelInfo, jsonDoc);
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::UpdateChannel, ChannelInfoValue(retChannelInfo, receipt, rc));
- });
- });
- }
- void TwitchREST::GetChannelEditors(ReceiptID& receipt, const AZStd::string& channelID)
- {
- AZStd::string url(BuildKrakenURL("channels") + "/" + channelID + "/editors");
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_GET, GetDefaultHeaders(), [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- UserInfoList userList;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- Aws::Utils::Array<Aws::Utils::Json::JsonView> jsonUserArray = jsonDoc.GetArray("users");
- for (size_t index = 0; index < jsonUserArray.GetLength(); index++)
- {
- Aws::Utils::Json::JsonView item = jsonUserArray.GetItem(index);
- UserInfo ui;
- SafeGetUserInfo(ui, item);
- userList.push_back(ui);
- }
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::GetChannelEditors, UserInfoListValue(userList, receipt, rc));
- });
- }
-
- void TwitchREST::GetChannelFollowers(ReceiptID& receipt, const AZStd::string& channelID, const AZStd::string& cursor, AZ::u64 offset)
- {
- AZStd::string url(BuildKrakenURL("channels") + "/" + channelID + "/follows?limit=100");
- if( !cursor.empty() )
- {
- url += "&cursor=";
- url += cursor;
- url += "&offset=";
- url += AZStd::to_string(offset);
- }
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_GET, GetClientIDHeader(), [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- FollowerResult followerResult;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- SafeGetJSONString(followerResult.Cursor, "_cursor", jsonDoc);
- SafeGetJSONu64(followerResult.Total, "_total", jsonDoc);
- Aws::Utils::Array<Aws::Utils::Json::JsonView> jsonFollowsArray = jsonDoc.GetArray("follows");
- for (size_t index = 0; index < jsonFollowsArray.GetLength(); index++)
- {
- Aws::Utils::Json::JsonView item = jsonFollowsArray.GetItem(index);
- Follower follower;
-
- SafeGetJSONString(follower.CreatedDate, "created_at", item);
- SafeGetJSONbool(follower.Notifications, "notifications", item);
- SafeGetUserInfoFromUserContainer(follower.User, item);
- followerResult.Followers.push_back(follower);
- }
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::GetChannelFollowers, FollowerResultValue(followerResult, receipt, rc));
- });
- }
- void TwitchREST::GetChannelTeams(ReceiptID& receipt, const AZStd::string& channelID)
- {
- AZStd::string url(BuildKrakenURL("channels") + "/" + channelID + "/teams");
-
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_GET, GetClientIDHeader(), [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- TeamInfoList teamInfoList;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- Aws::Utils::Array<Aws::Utils::Json::JsonView> jsonArray = jsonDoc.GetArray("teams");
- for (size_t index = 0; index < jsonArray.GetLength(); index++)
- {
- Aws::Utils::Json::JsonView item = jsonArray.GetItem(index);
- TeamInfo teamInfo;
- SafeGetTeamInfo(teamInfo, item);
- teamInfoList.push_back(teamInfo);
- }
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::GetChannelTeams, ChannelTeamValue(teamInfoList, receipt, rc));
- });
- }
- void TwitchREST::GetChannelSubscribers(ReceiptID& receipt, const AZStd::string& channelID, AZ::u64 offset)
- {
- AZStd::string url(BuildKrakenURL("channels") + "/" + channelID + "/subscriptions?limit=100");
- if (offset > 0)
- {
- url += "&offset=";
- url += AZStd::to_string(offset);
- }
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_GET, GetDefaultHeaders(), [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- Subscription subscription;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- SafeGetJSONu64(subscription.Total, "_total", jsonDoc);
- Aws::Utils::Array<Aws::Utils::Json::JsonView> jsonSubscriptionsArray = jsonDoc.GetArray("subscriptions");
- for (size_t index = 0; index < jsonSubscriptionsArray.GetLength(); index++)
- {
- Aws::Utils::Json::JsonView item = jsonSubscriptionsArray.GetItem(index);
- SubscriberInfo si;
- SafeGetJSONString(si.ID, "_id", item);
- SafeGetJSONString(si.CreatedDate, "created_at", item);
- SafeGetUserInfoFromUserContainer(si.User, item);
- subscription.Subscribers.push_back(si);
- }
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::GetChannelSubscribers, SubscriberValue(subscription, receipt, rc));
- });
- }
- void TwitchREST::CheckChannelSubscriptionbyUser(ReceiptID& receipt, const AZStd::string& channelID, const AZStd::string& userID)
- {
- AZStd::string url(BuildKrakenURL("channels") + "/" + channelID + "/subscriptions/" + userID);
-
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_GET, GetDefaultHeaders(), [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- SubscriberInfo si;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- SafeGetJSONString(si.ID, "_id", jsonDoc);
- SafeGetJSONString(si.CreatedDate, "created_at", jsonDoc);
- SafeGetUserInfoFromUserContainer(si.User, jsonDoc);
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::CheckChannelSubscriptionbyUser, SubscriberbyUserValue(si, receipt, rc));
- });
- }
- void TwitchREST::GetChannelVideos(ReceiptID& receipt, const AZStd::string& channelID, BroadCastType boradcastType, const AZStd::string& language, AZ::u64 offset)
- {
- AZStd::string url(BuildKrakenURL("channels") + "/" + channelID + "/videos?limit=100");
- if (offset > 0)
- {
- url += "&offset=";
- url += AZStd::to_string(offset);
- }
- AZStd::string bt( GetBroadCastTypeNameFromType(boradcastType) );
- if( !bt.empty() )
- {
- url += "&broadcast_type=";
- url += bt;
- }
- if( !language.empty() )
- {
- url += "&language=";
- url += language;
-
- }
-
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_GET, GetDefaultHeaders(), [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- VideoReturn videoReturn;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- SafeGetJSONu64(videoReturn.Total, "_total", jsonDoc);
- Aws::Utils::Array<Aws::Utils::Json::JsonView> jsonVideosArray = jsonDoc.GetArray("videos");
- for (size_t index = 0; index < jsonVideosArray.GetLength(); index++)
- {
- Aws::Utils::Json::JsonView item = jsonVideosArray.GetItem(index);
- VideoInfo vi;
- SafeGetJSONString(vi.ID, "_id", item);
- SafeGetJSONu64(vi.BroadcastID, "broadcast_id", item);
- SafeGetJSONBroadCastType(vi.Type, "broadcast_type", item);
- SafeGetJSONVideoChannel(vi.Channel, item);
- SafeGetJSONString(vi.CreatedDate, "created_at", item);
- SafeGetJSONString(vi.Description, "description", item);
- SafeGetJSONString(vi.DescriptionHTML, "description_html", item);
- SafeGetJSONVideoFPS(vi.FPS, item);
- SafeGetJSONString(vi.Game, "game", item);
- SafeGetJSONString(vi.Language, "language", item);
- SafeGetJSONu64(vi.Length, "length", item);
- SafeGetJSONVideoPreview(vi.Preview, item);
- SafeGetJSONString(vi.PublishedDate, "published_at", item);
- SafeGetJSONVideoResolutions(vi.Resolutions, item);
- SafeGetJSONString(vi.Status, "status", item);
- SafeGetJSONString(vi.TagList, "tag_list", item);
- SafeGetJSONVideoThumbnails(vi.Thumbnails, item);
- SafeGetJSONString(vi.Title, "title", item);
- SafeGetJSONString(vi.URL, "url", item);
- SafeGetJSONString(vi.Viewable, "viewable", item);
- SafeGetJSONString(vi.ViewableAt, "viewable_at", item);
- SafeGetJSONu64(vi.Views, "views", item);
- videoReturn.Videos.push_back(vi);
- }
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::GetChannelVideos, VideoReturnValue(videoReturn, receipt, rc));
- });
- }
- void TwitchREST::StartChannelCommercial(ReceiptID& receipt, const AZStd::string& channelID, CommercialLength length)
- {
- AZStd::string url(BuildKrakenURL("channels") + "/" + channelID + "/commercial");
- HttpRequestor::Headers headers( GetDefaultHeaders() );
- AddToHeader(headers, "Content-Type", "application/json");
- Aws::Utils::Json::JsonValue jsonBody;
- jsonBody.WithInt64("duration", GetComercialLength(length) );
- Aws::String body(jsonBody.View().WriteCompact());
-
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_POST, headers, body.c_str(), [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- StartChannelCommercialResult cr;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- SafeGetJSONu64(cr.Duration, "duration", jsonDoc);
- SafeGetJSONString(cr.Message, "message", jsonDoc);
- SafeGetJSONu64(cr.RetryAfter, "retryafter", jsonDoc);
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::StartChannelCommercial, StartChannelCommercialValue(cr, receipt, rc));
- });
- }
- void TwitchREST::ResetChannelStreamKey(ReceiptID& receipt, const AZStd::string& channelID)
- {
- AZStd::string url(BuildKrakenURL("channels") + "/" + channelID + "/stream_key");
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_DELETE, GetDefaultHeaders(), [receipt, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- ChannelInfo ci;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- SafeGetChannelInfo(ci, jsonDoc);
- }
- TwitchNotifyBus::QueueBroadcast(&TwitchNotifyBus::Events::ResetChannelStreamKey, ChannelInfoValue(ci, receipt, rc));
- });
- }
- bool TwitchREST::IsValidGameContext(const AZStd::string& gameContext) const
- {
- bool isValid = false;
- /*
- ** Insure gameContext is a valid JSON object, and that means there must be a string!
- */
- if( !gameContext.empty() )
- {
- Aws::Utils::Json::JsonValue json(Aws::String(gameContext.c_str()));
- isValid = json.WasParseSuccessful();
- }
-
- return isValid;
- }
- void TwitchREST::AddHTTPRequest(const AZStd::string& URI, Aws::Http::HttpMethod method, const HttpRequestor::Headers & headers, const HttpRequestor::Callback & callback)
- {
- HttpRequestor::HttpRequestorRequestBus::Broadcast(&HttpRequestor::HttpRequestorRequests::AddRequestWithHeaders, URI, method, headers, callback);
- }
- void TwitchREST::AddHTTPRequest(const AZStd::string& URI, Aws::Http::HttpMethod method, const HttpRequestor::Headers & headers, const AZStd::string& body, const HttpRequestor::Callback & callback)
- {
- HttpRequestor::HttpRequestorRequestBus::Broadcast(&HttpRequestor::HttpRequestorRequests::AddRequestWithHeadersAndBody, URI, method, headers, body, callback);
- }
-
- void TwitchREST::InternalGetChannel(const ReceiptID& receipt, const GetChannelCallback& callback)
- {
- AZStd::string url(BuildKrakenURL("channel"));
- AddHTTPRequest(url, Aws::Http::HttpMethod::HTTP_GET, GetDefaultHeaders(), [receipt, callback, this](const Aws::Utils::Json::JsonView& jsonDoc, Aws::Http::HttpResponseCode httpCode)
- {
- ResultCode rc(ResultCode::TwitchRESTError);
- ChannelInfo channelInfo;
- if (httpCode == Aws::Http::HttpResponseCode::OK)
- {
- rc = ResultCode::Success;
- channelInfo.NumItemsRecieved = SafeGetChannelInfo(channelInfo, jsonDoc);
- }
- callback(channelInfo, receipt, rc);
- });
- }
- AZStd::string TwitchREST::BuildBaseURL(const AZStd::string& family, const AZStd::string& friendID /*= ""*/) const
- {
- /*
- ** return a URL something like https://api.twitch.tv/v5/<family>/<clientid>
- */
- AZStd::string url(kProtocol);
- AZStd::string clientID;
- // if user id is empty, then get the current client id for the logged in user.
- if( friendID.empty() )
- {
- TwitchRequestBus::BroadcastResult(clientID, &TwitchRequests::GetUserID);
- }
- else
- {
- clientID = friendID;
- }
- url += "://";
- url += kBasePath; // kBasePath("api.twitch.tv");
- url += "/";
- url += kVer; // kVer("v5");
- url += "/";
- url += family;
- url += "/";
- url += clientID;
- return url;
- }
- AZStd::string TwitchREST::BuildKrakenURL(const AZStd::string& family) const
- {
- /*
- ** return a URL something like https://api.twitch.tv/kraken/<family>
- */
- AZStd::string url(kProtocol);
- url += "://";
- url += kBasePath; // kBasePath("api.twitch.tv");
- url += "/";
- url += kKraken; // kKraken("kraken");
- url += "/";
- url += family;
- return url;
- }
-
- HttpRequestor::Headers TwitchREST::GetDefaultHeaders()
- {
- HttpRequestor::Headers hdrs;
-
- AddAcceptToHeader(hdrs);
- AddOAuthtHeader(hdrs);
- AddClientIDHeader(hdrs);
- return hdrs;
- }
- HttpRequestor::Headers TwitchREST::GetClientIDHeader()
- {
- HttpRequestor::Headers hdrs;
- AddAcceptToHeader(hdrs);
- AddClientIDHeader(hdrs);
- return hdrs;
- }
- void TwitchREST::AddOAuthtHeader(HttpRequestor::Headers& headers)
- {
- AZStd::string oAuthToken;
- TwitchRequestBus::BroadcastResult(oAuthToken, &TwitchRequests::GetOAuthToken);
- headers["Authorization"] = kAuthType + oAuthToken;
- }
-
- // return the application id in a header, the rest docs refer this as the client-id, poorly named)
- void TwitchREST::AddClientIDHeader(HttpRequestor::Headers& headers)
- {
- AZStd::string appID;
- TwitchRequestBus::BroadcastResult(appID, &TwitchRequests::GetApplicationID);
- headers["Client-ID"] = appID;
- }
- void TwitchREST::AddAcceptToHeader(HttpRequestor::Headers& headers)
- {
- headers["Accept"] = kAcceptType;
- }
- void TwitchREST::AddToHeader(HttpRequestor::Headers& headers, const AZStd::string& name, const AZStd::string& key) const
- {
- headers[name] = key;
- }
- void TwitchREST::AddToHeader(HttpRequestor::Headers& headers, const AZStd::string& name, AZ::s64 key) const
- {
- AddToHeader(headers, name, AZStd::to_string(key));
- }
- void TwitchREST::AddToHeader(HttpRequestor::Headers& headers, const AZStd::string& name, AZ::u64 key) const
- {
- AddToHeader(headers, name, AZStd::to_string(key));
- }
-
- AZ::u64 TwitchREST::SafeGetUserInfoFromUserContainer(UserInfo& userInfo, const Aws::Utils::Json::JsonView& jsonInfo) const
- {
- AZ::u64 itemCount = 0;
- if (jsonInfo.ValueExists("user") )
- {
- Aws::Utils::Json::JsonView jsonUser(jsonInfo.GetObject("user"));
- itemCount = SafeGetUserInfo(userInfo, jsonUser);
- }
- return itemCount;
- }
- AZ::u64 TwitchREST::SafeGetUserInfo(UserInfo& userInfo, const Aws::Utils::Json::JsonView& jsonInfo) const
- {
- AZ::u64 itemCount = 0;
- itemCount += SafeGetJSONString(userInfo.ID, "_id", jsonInfo);
- itemCount += SafeGetJSONString(userInfo.Bio, "bio", jsonInfo);
- itemCount += SafeGetJSONString(userInfo.CreatedDate, "created_at", jsonInfo);
- itemCount += SafeGetJSONString(userInfo.DisplayName, "display_name", jsonInfo);
- itemCount += SafeGetJSONString(userInfo.EMail, "email", jsonInfo);
- itemCount += SafeGetJSONString(userInfo.Logo, "logo", jsonInfo);
- itemCount += SafeGetJSONString(userInfo.Name, "name", jsonInfo);
- itemCount += SafeGetJSONString(userInfo.ProfileBanner, "profile_banner", jsonInfo);
- itemCount += SafeGetJSONString(userInfo.ProfileBannerBackgroundColor, "profile_banner_background_color", jsonInfo);
- itemCount += SafeGetJSONString(userInfo.Type, "type", jsonInfo);
- itemCount += SafeGetJSONString(userInfo.UpdatedDate, "updated_at", jsonInfo);
- itemCount += SafeGetJSONbool(userInfo.EMailVerified, "email_verified", jsonInfo);
- itemCount += SafeGetJSONbool(userInfo.Partnered, "partnered", jsonInfo);
- itemCount += SafeGetJSONbool(userInfo.TwitterConnected, "twitter_connected", jsonInfo);
- itemCount += SafeGetUserNotifications(userInfo.Notifications, jsonInfo);
- return itemCount;
- }
- bool TwitchREST::SafeGetJSONString(AZStd::string& value, const char *key, const Aws::Utils::Json::JsonView& json) const
- {
- bool success = false;
- if ( (key != nullptr)&& json.ValueExists(key) )
- {
- success = true;
- value = json.GetString(key).c_str();
- }
- return success;
- }
- bool TwitchREST::SafeGetJSONu64(AZ::u64& value, const char *key, const Aws::Utils::Json::JsonView& json) const
- {
- bool success = false;
- if ((key != nullptr)&& json.ValueExists(key))
- {
- success = true;
- value = static_cast<AZ::u64>(json.GetInt64(key));
- }
- return success;
- }
-
- bool TwitchREST::SafeGetJSONs64(AZ::s64& value, const char *key, const Aws::Utils::Json::JsonView& json) const
- {
- bool success = false;
- if ((key != nullptr)&& json.ValueExists(key))
- {
- success = true;
- value = json.GetInt64(key);
- }
- return success;
- }
- bool TwitchREST::SafeGetJSONbool(bool& value, const char *key, const Aws::Utils::Json::JsonView& json) const
- {
- bool success = false;
- if ((key != nullptr)&& json.ValueExists(key))
- {
- success = true;
- value = json.GetBool(key);
- }
- return success;
- }
- bool TwitchREST::SafeGetJSONdouble(double& value, const char* key, const Aws::Utils::Json::JsonView& json) const
- {
- bool success = false;
- if ((key != nullptr) && json.ValueExists(key))
- {
- success = true;
- value = json.GetDouble(key);
- }
- return success;
- }
- AZ::u64 TwitchREST::SafeGetUserNotifications(UserNotifications& userNotifications, const Aws::Utils::Json::JsonView& json) const
- {
- // assumes the json value contains:
- // "notifications": { "email": false, "push" : true }
- AZ::u64 numItems = 0;
- if (json.ValueExists("notifications"))
- {
- Aws::Utils::Json::JsonView jsonNotifications(json.GetObject("notifications"));
- numItems += SafeGetJSONbool(userNotifications.EMail, "email", jsonNotifications);
- numItems += SafeGetJSONbool(userNotifications.Push, "push", jsonNotifications);
- }
- return numItems;
- }
-
- bool TwitchREST::SafeGetPresenceActivityType(PresenceActivityType& activityType, const Aws::Utils::Json::JsonView& json) const
- {
- bool success = false;
- AZStd::string name;
- if (SafeGetJSONString(name, "availability", json))
- {
- activityType = GetPresenceActivityType(name);
- success = true;
- }
-
- return success;
- }
- bool TwitchREST::SafeGetPresenceAvailability(PresenceAvailability& availability, const Aws::Utils::Json::JsonView& json) const
- {
- // assumes the json doc contains
- // "activity": { "type": "none"}
-
- bool success = false;
-
- if( json.ValueExists("activity") )
- {
- Aws::Utils::Json::JsonView jsonActivity( json.GetObject("activity") );
- AZStd::string typeName;
- if( SafeGetJSONString(typeName, "type", jsonActivity) )
- {
- availability = GetPresenceAvailability(typeName);
- success = true;
- }
- }
- return success;
- }
- AZ::u64 TwitchREST::SafeGetChannelInfo(ChannelInfo& channelInfo, const Aws::Utils::Json::JsonView& json) const
- {
- AZ::u64 itemCount = 0;
-
- itemCount += SafeGetJSONString(channelInfo.Id, "_id", json);
- itemCount += SafeGetJSONString(channelInfo.BroadcasterLanguage, "broadcaster_language", json);
- itemCount += SafeGetJSONString(channelInfo.CreatedDate, "created_at", json);
- itemCount += SafeGetJSONString(channelInfo.DisplayName, "display_name", json);
- itemCount += SafeGetJSONString(channelInfo.eMail, "email", json); // only returned when invoked via GetChannel
- itemCount += SafeGetJSONu64(channelInfo.NumFollowers, "followers", json);
- itemCount += SafeGetJSONString(channelInfo.GameName, "game", json);
- itemCount += SafeGetJSONString(channelInfo.Lanugage, "language", json);
- itemCount += SafeGetJSONString(channelInfo.Logo, "logo", json);
- itemCount += SafeGetJSONbool(channelInfo.Mature, "mature", json);
- itemCount += SafeGetJSONString(channelInfo.Name, "name", json);
- itemCount += SafeGetJSONbool(channelInfo.Partner, "partner", json);
- itemCount += SafeGetJSONString(channelInfo.ProfileBanner, "profile_banner", json);
- itemCount += SafeGetJSONString(channelInfo.ProfileBannerBackgroundColor, "profile_banner_background_color", json);
- itemCount += SafeGetJSONString(channelInfo.Status, "status", json);
- itemCount += SafeGetJSONString(channelInfo.StreamKey, "stream_key", json); // only returned when invoked via GetChannel
- itemCount += SafeGetJSONString(channelInfo.UpdatedDate, "updated_at", json);
- itemCount += SafeGetJSONString(channelInfo.URL, "url", json);
- itemCount += SafeGetJSONString(channelInfo.VideoBanner, "video_banner", json);
- itemCount += SafeGetJSONu64(channelInfo.NumViews, "views", json);
- return itemCount;
- }
- AZ::u64 TwitchREST::SafeGetTeamInfo(TeamInfo& teamInfo, const Aws::Utils::Json::JsonView& json) const
- {
- AZ::u64 itemCount = 0;
-
- itemCount += SafeGetJSONString(teamInfo.ID, "_id", json);
- itemCount += SafeGetJSONString(teamInfo.Background, "background", json);
- itemCount += SafeGetJSONString(teamInfo.Banner, "banner", json);
- itemCount += SafeGetJSONString(teamInfo.CreatedDate, "created_at", json);
- itemCount += SafeGetJSONString(teamInfo.DisplayName, "display_name", json);
- itemCount += SafeGetJSONString(teamInfo.Info, "info", json);
- itemCount += SafeGetJSONString(teamInfo.Logo, "logo", json);
- itemCount += SafeGetJSONString(teamInfo.Name, "name", json);
- itemCount += SafeGetJSONString(teamInfo.UpdatedDate, "updated_at", json);
-
- return itemCount;
- }
- bool TwitchREST::SafeGetJSONBroadCastType(BroadCastType& type, [[maybe_unused]] const char*key, const Aws::Utils::Json::JsonView& json) const
- {
- bool success = false;
- AZStd::string typeName;
- if( SafeGetJSONString(typeName, "broadcast_type", json) )
- {
- BroadCastType tempType = GetBroadCastTypeFromName(typeName);
- if (tempType != BroadCastType::Default)
- {
- success = true;
- type = tempType;
- }
- }
- return success;
- }
- bool TwitchREST::SafeGetJSONVideoChannel(VideoChannelInfo& channelInfo, const Aws::Utils::Json::JsonView& json) const
- {
- // assumes the json doc contains
- // "channel": { "_id": "20694610", "display_name" : "Towelliee", "name" : "towelliee" }
-
- bool success = false;
- if (json.ValueExists("channel"))
- {
- Aws::Utils::Json::JsonView jsonChannel(json.GetObject("channel"));
-
- SafeGetJSONString(channelInfo.ID, "_id", jsonChannel);
- SafeGetJSONString(channelInfo.DisplayName, "display_name", jsonChannel);
- SafeGetJSONString(channelInfo.Name, "name", jsonChannel);
-
- success = true;
- }
- return success;
- }
- bool TwitchREST::SafeGetJSONVideoFPS(FPSInfo & fps, const Aws::Utils::Json::JsonView& json) const
- {
- // assumes the json doc contains
- // "fps": { "chunked": 59.9997939597903, "high" : 30.2491085172346, "low" : 30.249192959941, "medium" : 30.2491085172346, "mobile" : 30.249192959941 }
- bool success = false;
- if (json.ValueExists("fps"))
- {
- Aws::Utils::Json::JsonView jsonFPS(json.GetObject("fps"));
- SafeGetJSONdouble(fps.Chunked, "chunked", jsonFPS);
- SafeGetJSONdouble(fps.High, "high", jsonFPS);
- SafeGetJSONdouble(fps.Low, "low", jsonFPS);
- SafeGetJSONdouble(fps.Medium, "medium", jsonFPS);
- SafeGetJSONdouble(fps.Mobile, "mobile", jsonFPS);
- success = true;
- }
- return success;
- }
- bool TwitchREST::SafeGetJSONVideoPreview(PreviewInfo& preview, const Aws::Utils::Json::JsonView& json) const
- {
- // assumes the json doc contains
- // "preview": { "large": "https://.../thumb102381501-640x360.jpg","medium" : "https://s...180.jpg","small" : "https://...81501-80x45.jpg", "template" : "https://.../thumb102381501-{width}x{height}.jpg" }
- bool success = false;
- if (json.ValueExists("preview"))
- {
- Aws::Utils::Json::JsonView jsonValue(json.GetObject("preview"));
- SafeGetJSONString(preview.Large, "large", jsonValue);
- SafeGetJSONString(preview.Medium, "medium", jsonValue);
- SafeGetJSONString(preview.Small, "small", jsonValue);
- SafeGetJSONString(preview.Template, "template", jsonValue);
- success = true;
- }
- return success;
- }
- bool TwitchREST::SafeGetJSONVideoResolutions(ResolutionsInfo& resolutions, const Aws::Utils::Json::JsonView& json) const
- {
- // assumes the json doc contains
- // "resolutions": {"chunked": "1920x1080","high" : "1280x720","low" : "640x360","medium" : "852x480","mobile" : "400x226"}
- bool success = false;
- if (json.ValueExists("resolutions"))
- {
- Aws::Utils::Json::JsonView jsonValue(json.GetObject("resolutions"));
- SafeGetJSONString(resolutions.Chunked, "chunked", jsonValue);
- SafeGetJSONString(resolutions.High, "high", jsonValue);
- SafeGetJSONString(resolutions.Low, "low", jsonValue);
- SafeGetJSONString(resolutions.Medium, "medium", jsonValue);
- SafeGetJSONString(resolutions.Mobile, "mobile", jsonValue);
- success = true;
- }
- return success;
- }
- bool TwitchREST::SafeGetJSONVideoThumbnailInfo(ThumbnailInfo& info, const char *key, const Aws::Utils::Json::JsonView& json) const
- {
- // assumes the json doc contains
- // "<key>": [{"type": "generated", "url" : "https://.../thumb102381501-640x360.jpg"}],
- bool success = false;
- if (json.ValueExists(key))
- {
- success = true;
- Aws::Utils::Array<Aws::Utils::Json::JsonView> jsonArray( json.GetArray(key) );
- for (size_t index = 0; index < jsonArray.GetLength(); index++)
- {
- Aws::Utils::Json::JsonView item(jsonArray.GetItem(index));
-
- AZStd::string temp;
- if( SafeGetJSONString(temp, "type", item) )
- {
- info.Type = temp;
- temp.clear();
- }
- if (SafeGetJSONString(temp, "url", item))
- {
- info.Url = temp;
- temp.clear();
- }
- }
- }
- return success;
- }
- bool TwitchREST::SafeGetJSONVideoThumbnails(ThumbnailsInfo& thumbnails, const Aws::Utils::Json::JsonView& json) const
- {
- // assumes the json doc contains
- // "thumbnails": {"large": [{"type": "...", "url" : "..."}],"medium" : [{"type": "...", "url" : "..."}],"small" : [{"type": "...", "url" : "..."}],"template" : [{"type": "...", "url" : "..."}] }
- bool success = false;
- if (json.ValueExists("thumbnails"))
- {
- Aws::Utils::Json::JsonView jsonValue(json.GetObject("thumbnails"));
- SafeGetJSONVideoThumbnailInfo(thumbnails.Large, "large", jsonValue);
- SafeGetJSONVideoThumbnailInfo(thumbnails.Medium, "medium", jsonValue);
- SafeGetJSONVideoThumbnailInfo(thumbnails.Small, "small", jsonValue);
- SafeGetJSONVideoThumbnailInfo(thumbnails.Template, "template", jsonValue);
- success = true;
- }
- return success;
- }
- bool TwitchREST::SafeGetChannelCommunityInfo(CommunityInfo & info, const Aws::Utils::Json::JsonView& json) const
- {
- // assumes the json doc contains
- // { "_id": "", "avatar_image_url": "", "cover_image_url": "", "description": "","description_html": "","language": "", "name": "", "owner_id": "", "rules": "", "rules_html": "", "summary": "" }
-
- AZ::u64 itemCount = 0;
- itemCount += SafeGetJSONString(info.ID, "_id", json);
- itemCount += SafeGetJSONString(info.AvatarImageURL, "avatar_image_url", json);
- itemCount += SafeGetJSONString(info.CoverImageURL, "cover_image_url", json);
- itemCount += SafeGetJSONString(info.Description, "description", json);
- itemCount += SafeGetJSONString(info.DescriptionHTML, "description_html", json);
- itemCount += SafeGetJSONString(info.Language, "language", json);
- itemCount += SafeGetJSONString(info.Name, "name", json);
- itemCount += SafeGetJSONString(info.OwnerID, "owner_id", json);
- itemCount += SafeGetJSONString(info.Rules, "rules", json);
- itemCount += SafeGetJSONString(info.RulesHTML, "rules_html", json);
- itemCount += SafeGetJSONString(info.Summary, "summary", json);
- return (itemCount > 0);
- }
- AZStd::string TwitchREST::GetPresenceAvailabilityName(PresenceAvailability availability) const
- {
- auto itr = m_availabilityMap.find(availability);
- if( itr != m_availabilityMap.end() )
- {
- return itr->second;
- }
- return "";
- }
- AZStd::string TwitchREST::GetPresenceActivityTypeName(PresenceActivityType activityType) const
- {
- auto itr = m_activityTypeMap.find(activityType);
- if (itr != m_activityTypeMap.end())
- return itr->second;
- return "";
- }
- PresenceAvailability TwitchREST::GetPresenceAvailability(const AZStd::string& name) const
- {
- for(const auto& i: m_availabilityMap)
- if( i.second == name)
- return i.first;
-
- return PresenceAvailability::Unknown;
- }
- PresenceActivityType TwitchREST::GetPresenceActivityType(const AZStd::string& name) const
- {
- for (const auto& i : m_activityTypeMap)
- if (i.second == name)
- return i.first;
-
- return PresenceActivityType::Unknown;
- }
- AZStd::string TwitchREST::GetBroadCastTypeNameFromType(BroadCastType type) const
- {
- AZStd::string name;
- AZ::u64 bits = static_cast<AZ::u64>(type);
-
- if( bits & static_cast<AZ::u64>(BroadCastType::Archive) )
- {
- name += "archive";
- }
- if( bits & static_cast<AZ::u64>(BroadCastType::Highlight) )
- {
- if( !name.empty() )
- name += ",";
- name += "highlight";
- }
- if (bits & static_cast<AZ::u64>(BroadCastType::Upload))
- {
- if (!name.empty())
- name += ",";
- name += "upload";
- }
-
- return name;
- }
- BroadCastType TwitchREST::GetBroadCastTypeFromName(const AZStd::string& name) const
- {
- AZ::u64 bits = 0;
-
- if ( name.find("archive") != AZStd::string::npos)
- bits |= static_cast<AZ::u64>(BroadCastType::Archive);
- if (name.find("highlight") != AZStd::string::npos)
- bits |= static_cast<AZ::u64>(BroadCastType::Highlight);
- if (name.find("upload") != AZStd::string::npos)
- bits |= static_cast<AZ::u64>(BroadCastType::Upload);
- return static_cast<BroadCastType>(bits);
- }
- AZ::u64 TwitchREST::GetComercialLength(CommercialLength length) const
- {
- AZ::u64 lengthInSeconds = 0;
- if (length == CommercialLength::T60Seconds)
- lengthInSeconds = 60;
- else if (length == CommercialLength::T90Seconds)
- lengthInSeconds = 90;
- else if (length == CommercialLength::T120Seconds)
- lengthInSeconds = 120;
- else if (length == CommercialLength::T150Seconds)
- lengthInSeconds = 150;
- else if (length == CommercialLength::T180Seconds)
- lengthInSeconds = 180;
- else
- lengthInSeconds = 30; // default is CommercialLength::T30Seconds
- return lengthInSeconds;
- }
- }
|