-1

This is a file called TwitchAPIexample in folder Plugins under project MyFirstBot. The classes and code is below:

using System.Net;
using System.IO;
using Newtonsoft.Json;

namespace MyFirstBot.Plugins
{
    public class TwitchAPIexample
    {

        private const string url = "https://api.twitch.tv/kraken/streams/<channel>";

        public bool isTwitchLive;

        private static void BuildConnect()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Method = "Get";
            request.Timeout = 12000;
            request.ContentType = "application/json";
            request.Headers.Add("authorization", "<token>");

            using (System.IO.Stream s = request.GetResponse().GetResponseStream())
            {
                using (StreamReader sr = new System.IO.StreamReader(s))
                {
                    var jsonResponse = sr.ReadToEnd();
                    RootObject r = JsonConvert.DeserializeObject<RootObject>(jsonResponse);
                }
            }
        }


        public class Preview
        {
            public string small { get; set; }
            public string medium { get; set; }
            public string large { get; set; }
            public string template { get; set; }
        }

        public class Channel
        {
            public bool mature { get; set; }
            public string status { get; set; }
            public string broadcaster_language { get; set; }
            public string display_name { get; set; }
            public string game { get; set; }
            public string language { get; set; }
            public int _id { get; set; }
            public string name { get; set; }
            public string created_at { get; set; }
            public string updated_at { get; set; }
            public bool partner { get; set; }
            public string logo { get; set; }
            public string video_banner { get; set; }
            public string profile_banner { get; set; }
            public object profile_banner_background_color { get; set; }
            public string url { get; set; }
            public int views { get; set; }
            public int followers { get; set; }
        }

        public class Stream
        {
            public long _id { get; set; }
            public string game { get; set; }
            public int viewers { get; set; }
            public int video_height { get; set; }
            public int average_fps { get; set; }
            public int delay { get; set; }
            public string created_at { get; set; }
            public bool is_playlist { get; set; }
            public Preview preview { get; set; }
            public Channel channel { get; set; }
        }

        public class RootObject
        {
            public Stream stream { get; set; }
        }


    }

}

What I need to do is use the classes in the namespace MyfirstBot.Plugins in a different file under MyFirstBot project file. I have:

using namespace MyFirstBot.Plugins

but I'm not sure how to use the RootObject. I have tried using:

TwitchAPIexample.stream TwitchLive = new TwitchAPIexample.stream()

but I dont really know how to go from there to check the other strings in the JSON, set them equal to strings, basically just how to manipulate everything in the TwitchAPIexample class.

Again I'm a C# Noob so you don't have to write it for me, but if you could explain it or hit me up with a good resource. I have googled and still am confused. OOP isnt my strong suit.

This is as far as I have gotten:

namespace MyFirstBot
{
    public class DiscordBot
    {
        DiscordClient client;
        CommandService commands;
        TwitchClient TwitchClient;
        TwitchAPIexample.Stream TwitchLive = new TwitchAPIexample.Stream();
        public DiscordBot()
        {
            if(TwitchLive.equals(null))
            {
                //stream is offline
            }
        }
    }
}

I'm not sure this is the best method.

Zack Arnett
  • 239
  • 2
  • 6
  • 19
  • Your `TwitchAPIexample` class doesn't have a method named `Stream`, so this line of your code makes no sense: `new TwitchAPIexample.Stream()`. Which stream are you trying to access? – Isaac Kleinman Mar 15 '17 at 17:22
  • 1
    @IsaacKleinman - `Stream` is a nested class of `TwitchAPIexample`. In the context of the example, the usage makes sense as it is newing up a new instance of `Stream`. – Metro Smurf Mar 15 '17 at 17:24
  • To achieve what I'm guessing you're trying to accomplish: change the `BuildConnect` method to return the `rootObject`, then call `BuildConnect()` in your `DiscordBot` class to get at the details of the json response. – Isaac Kleinman Mar 15 '17 at 17:32

1 Answers1

0

For me it looks like you need to change your architecture a bit. You don't need static method, and you need to create property trough which you'd be able to access RootObject. And you don't really need to nest those classes.

public class TwitchAPIexample
{

    private const string url = "https://api.twitch.tv/kraken/streams/<channel>";

    public bool IsTwitchLive { get; set; }
    public RootObject Root { get; set; }

    public TwitchAPIexample()
    {
        BuildConnect();
    }

    private void BuildConnect()
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        request.Method = "Get";
        request.Timeout = 12000;
        request.ContentType = "application/json";
        request.Headers.Add("authorization", "<token>");

        using (System.IO.Stream s = request.GetResponse().GetResponseStream())
        {
            using (StreamReader sr = new System.IO.StreamReader(s))
            {
                var jsonResponse = sr.ReadToEnd();
                this.Root = JsonConvert.DeserializeObject<RootObject>(jsonResponse);
            }
        }
    }
}

public class Preview
{
    public string small { get; set; }
    public string medium { get; set; }
    public string large { get; set; }
    public string template { get; set; }
}

public class Channel
{
    public bool mature { get; set; }
    public string status { get; set; }
    public string broadcaster_language { get; set; }
    public string display_name { get; set; }
    public string game { get; set; }
    public string language { get; set; }
    public int _id { get; set; }
    public string name { get; set; }
    public string created_at { get; set; }
    public string updated_at { get; set; }
    public bool partner { get; set; }
    public string logo { get; set; }
    public string video_banner { get; set; }
    public string profile_banner { get; set; }
    public object profile_banner_background_color { get; set; }
    public string url { get; set; }
    public int views { get; set; }
    public int followers { get; set; }
}

public class Stream
{
    public long _id { get; set; }
    public string game { get; set; }
    public int viewers { get; set; }
    public int video_height { get; set; }
    public int average_fps { get; set; }
    public int delay { get; set; }
    public string created_at { get; set; }
    public bool is_playlist { get; set; }
    public Preview preview { get; set; }
    public Channel channel { get; set; }
}

public class RootObject
{
    public Stream stream { get; set; }
}

Now you can do next

namespace MyFirstBot
{
    public class DiscordBot
    {
        DiscordClient client;
        CommandService commands;
        TwitchClient TwitchClient;
        TwitchAPIexample twitchLive = new TwitchAPIexample();
        public DiscordBot()
        {
            if(twitchLive.Root == null || twitchLive.Root.Stream == null)
            {
                //stream is offline
            }
        }
    }
}

So you are accessing the root object using the twitchLive.Root and trough the root you can access your stream twitchLive.Root.Stream

artgdev
  • 184
  • 10