Exemplo 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);
                }
            }
        }
Exemplo n.º 2
0
        private async Task PutAsync(string endpoint, object data)
        {
            try
            {
                using (AdvancedHttpClient client = new AdvancedHttpClient(MixItUpAPIEndpoint))
                {
                    string content = JSONSerializerHelper.SerializeToString(data);
                    HttpResponseMessage response = await client.PutAsync(endpoint, new StringContent(content, Encoding.UTF8, "application/json"));

                    await this.ProcessResponseIfError(response);
                }
            }
            catch (Exception ex) { Logger.Log(ex); }
        }
 /// <summary>
 /// Performs a PUT REST request using the provided request URI.
 /// </summary>
 /// <param name="requestUri">The request URI to use</param>
 /// <param name="content">The content to send</param>
 /// <returns>A type-casted object of the contents of the response</returns>
 protected async Task <T> PutAsync <T>(string requestUri, HttpContent content)
 {
     using (AdvancedHttpClient client = await this.GetHttpClient())
     {
         try
         {
             client.RateLimitUpdateOccurred += Client_RateLimitUpdateOccurred;
             return(await client.PutAsync <T>(requestUri, content));
         }
         finally
         {
             client.RateLimitUpdateOccurred -= Client_RateLimitUpdateOccurred;
         }
     }
 }
Exemplo n.º 4
0
        public static async Task <T> PutAsync <T>(string path, object body)
        {
            AdvancedHttpClient client = new AdvancedHttpClient
            {
                BaseAddress = new Uri(BaseUri)
            };

            var         json    = JsonConvert.SerializeObject(body);
            HttpContent content = new StringContent(json, UnicodeEncoding.UTF8, "application/json");

            HttpResponseMessage message = await client.PutAsync(path, content);

            if (message.IsSuccessStatusCode)
            {
                return(JsonConvert.DeserializeObject <T>(await message.Content.ReadAsStringAsync()));
            }

            return(default(T));
        }