Пример #1
0
        /// <summary>
        /// Gets the global configuration data.
        /// </summary>
        /// <param name="clientID">The client ID of the extension</param>
        /// <param name="clientSecret">The client secret of the extension</param>
        /// <param name="ownerID">The owner user ID of the extension</param>
        /// <returns>The global configuration data for the extension</returns>
        public static async Task <ConfigurationModel> GetGlobalConfiguration(string clientID, string clientSecret, string ownerID)
        {
            Validator.ValidateString(clientID, "clientID");
            Validator.ValidateString(clientSecret, "clientSecret");
            Validator.ValidateString(ownerID, "ownerID");

            using (AdvancedHttpClient client = TwitchExtensionService.GetHttpClient(clientID, clientSecret, ownerID, null))
            {
                HttpResponseMessage response = await client.GetAsync($"https://api.twitch.tv/extensions/{clientID}/configurations/segments/global");

                if (response.IsSuccessStatusCode)
                {
                    JObject jobj = await response.ProcessJObjectResponse();

                    if (jobj != null && jobj.Count > 0)
                    {
                        ConfigurationResultModel result = jobj.Values().First().ToObject <ConfigurationResultModel>();
                        if (result != null)
                        {
                            return(new ConfigurationModel(result));
                        }
                    }
                    return(null);
                }
                else
                {
                    return(null);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Gets the configuration data for the specified channel.
        /// </summary>
        /// <param name="clientID">The client ID of the extension</param>
        /// <param name="clientSecret">The client secret of the extension</param>
        /// <param name="ownerID">The owner user ID of the extension</param>
        /// <param name="channelID">The channel ID to broadcast to</param>
        /// <returns>The channel-specific configuration data for the extension</returns>
        public static async Task <IEnumerable <ChannelConfigurationModel> > GetChannelConfiguration(string clientID, string clientSecret, string ownerID, string channelID)
        {
            Validator.ValidateString(clientID, "clientID");
            Validator.ValidateString(clientSecret, "clientSecret");
            Validator.ValidateString(ownerID, "ownerID");
            Validator.ValidateString(channelID, "channelID");

            using (AdvancedHttpClient client = TwitchExtensionService.GetHttpClient(clientID, clientSecret, ownerID, channelID))
            {
                HttpResponseMessage response = await client.GetAsync($"https://api.twitch.tv/extensions/{clientID}/configurations/segments/broadcaster?channel_id={channelID}");

                if (response.IsSuccessStatusCode)
                {
                    List <ChannelConfigurationModel> results = new List <ChannelConfigurationModel>();
                    JObject jobj = await response.ProcessJObjectResponse();

                    if (jobj != null)
                    {
                        foreach (var kvp in jobj)
                        {
                            ConfigurationResultModel result = kvp.Value.ToObject <ConfigurationResultModel>();
                            if (result != null)
                            {
                                results.Add(new ChannelConfigurationModel(result));
                            }
                        }
                    }
                    return(results);
                }
                else
                {
                    return(new List <ChannelConfigurationModel>());
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Sets the configuration data
        /// </summary>
        /// <param name="clientID">The client ID of the extension</param>
        /// <param name="clientSecret">The client secret of the extension</param>
        /// <param name="ownerID">The owner user ID of the extension</param>
        /// <param name="channelID">The channel ID to broadcast to</param>
        /// <param name="segment">The segment type of the configuration</param>
        /// <param name="version">The version for the configuration</param>
        /// <param name="data">The data to set for the configuration</param>
        /// <returns>Whether the broadcast was successful or not. Throws a HttpRestRequestException in the event of failed request</returns>
        public static async Task <bool> SetConfiguration(string clientID, string clientSecret, string ownerID, string channelID, string segment, string version, object data)
        {
            Validator.ValidateString(clientID, "clientID");
            Validator.ValidateString(clientSecret, "clientSecret");
            Validator.ValidateString(ownerID, "ownerID");
            Validator.ValidateString(segment, "segment");
            Validator.ValidateString(version, "version");
            Validator.ValidateVariable(data, "data");

            using (AdvancedHttpClient client = TwitchExtensionService.GetHttpClient(clientID, clientSecret, ownerID, channelID))
            {
                ConfigurationModel configuration = (ConfigurationModel.GlobalConfigurationSegmentValue.Equals(segment)) ?
                                                   new ConfigurationModel(segment, version, JSONSerializerHelper.SerializeToString(data)) :
                                                   new ChannelConfigurationModel(channelID, segment, version, JSONSerializerHelper.SerializeToString(data));

                HttpResponseMessage response = await client.PutAsync($"https://api.twitch.tv/extensions/{clientID}/configurations/",
                                                                     AdvancedHttpClient.CreateContentFromString(JSONSerializerHelper.SerializeToString(configuration)));

                if (response.IsSuccessStatusCode)
                {
                    return(true);
                }
                else
                {
                    throw new HttpRestRequestException(response);
                }
            }
        }
        /// <summary>
        /// Sends a broadcast to PubSub for the extension.
        /// </summary>
        /// <param name="clientID">The client ID of the extension</param>
        /// <param name="clientSecret">The client secret of the extension</param>
        /// <param name="ownerID">The owner user ID of the extension</param>
        /// <param name="channelID">The channel ID to broadcast to</param>
        /// <param name="data">The data to broadcast</param>
        /// <returns>An awaitable task</returns>
        /// <exception cref="HttpRestRequestException">Throw in the event of a failed request</exception>
        public static async Task SendBroadcast(string clientID, string clientSecret, string ownerID, string channelID, object data)
        {
            Validator.ValidateString(clientID, "clientID");
            Validator.ValidateString(clientSecret, "clientSecret");
            Validator.ValidateString(ownerID, "ownerID");
            Validator.ValidateString(channelID, "channelID");
            Validator.ValidateVariable(data, "data");

            using (AdvancedHttpClient client = TwitchExtensionService.GetHttpClient(clientID, clientSecret, ownerID, channelID))
            {
                HttpResponseMessage response = await client.PostAsync("https://api.twitch.tv/extensions/message/" + channelID,
                                                                      AdvancedHttpClient.CreateContentFromString(JSONSerializerHelper.SerializeToString(new BroadcastBodyModel(data))));

                if (!response.IsSuccessStatusCode)
                {
                    throw new HttpRestRequestException(response);
                }
            }
        }
        /// <summary>
        /// Sends a chat message to the specified channel from the extension.
        /// </summary>
        /// <param name="clientID">The client ID of the extension</param>
        /// <param name="clientSecret">The client secret of the extension</param>
        /// <param name="ownerID">The owner user ID of the extension</param>
        /// <param name="version">The version of the extension</param>
        /// <param name="channelID">The channel ID to broadcast to</param>
        /// <param name="message">The message to send</param>
        /// <returns>An awaitable task</returns>
        /// <exception cref="HttpRestRequestException">Throw in the event of a failed request</exception>
        public static async Task SendChatMessage(string clientID, string clientSecret, string ownerID, string version, string channelID, string message)
        {
            Validator.ValidateString(clientID, "clientID");
            Validator.ValidateString(clientSecret, "clientSecret");
            Validator.ValidateString(ownerID, "ownerID");
            Validator.ValidateString(version, "version");
            Validator.ValidateString(channelID, "channelID");
            Validator.ValidateString(message, "message");

            using (AdvancedHttpClient client = TwitchExtensionService.GetHttpClient(clientID, clientSecret, ownerID, channelID))
            {
                HttpResponseMessage response = await client.PostAsync($"https://api.twitch.tv/extensions/{clientID}/{version}/channels/{channelID}/chat",
                                                                      AdvancedHttpClient.CreateContentFromString(JSONSerializerHelper.SerializeToString(new ExtensionChatMessageModel(message))));

                if (!response.IsSuccessStatusCode)
                {
                    throw new HttpRestRequestException(response);
                }
            }
        }