Ejemplo n.º 1
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);
                }
            }
        }
 /// <summary>
 /// Creates an HttpContent object from the specified string.
 /// </summary>
 /// <param name="str">The string to serialize</param>
 /// <returns>The HttpContent containing the serialized string</returns>
 protected HttpContent CreateContentFromString(string str)
 {
     return(AdvancedHttpClient.CreateContentFromString(str));
 }