Exemplo n.º 1
0
        static void ConsoleOnlyTest() {
            string testUser = "******";

            Console.WriteLine("Creating Object!");

            var serverAPIURL = "http://callumcarmicheal.com/LCAPI/"; // LINK TO WHERE YOU UPLOADED AND SETUP THE PHP SCRIPT
            Request apiRequest = new Request(new ServResources(serverAPIURL));

            Console.WriteLine("Requesting a GUID!");
            var guidReqResponse = apiRequest.getNewGUID();

            // Print our information from the guidReqResponse
            Console.WriteLine("Recieved Object Back!");
            Console.WriteLine("Guid:   " + guidReqResponse.GUID);
            Console.WriteLine("Url:    " + guidReqResponse.URL);
            Console.WriteLine("Err:    " + guidReqResponse.Error);
            Console.WriteLine("ErrMSG: " + guidReqResponse.Error_Message);

            Console.WriteLine("");

            Console.WriteLine("Requesting a Authorization GUID and URL");
            var apiSetupResponse = apiRequest.SetupAPIRequest(false); // When using our own checking method, we just check to see if its "RETURN"

            if (apiSetupResponse.getState() != ResponseState.RETURN) {
                Console.WriteLine("Unexpected Response (" + apiSetupResponse.getState().ToString() + "): " + apiSetupResponse.getMessage());
                Console.ReadKey(); return;
            }

            // Get our token state
            GUID_Status guidStatus;
            Console.WriteLine("Calling API To Check Token!");

            int i = 0;
            while (true) {
                // Now we play the waiting game!
                //Console.WriteLine("State: " + state.ToString() + "\n");

                // Check our GUID State
                guidStatus = apiRequest.getGUIDState();

                if (guidStatus == GUID_Status.STATE_ID_VALID) {
                    ConColF(ConsoleColor.Green);
                    Console.WriteLine("Token recieved.");
                    ConColF(ConsoleColor.Black);
                    break;
                }

                // Wait 0.5 seconds before checking
                // for a token!
                System.Threading.Thread.Sleep(500);

                i++;

                if (i <= 5) {
                    i = 0;

                    ConColF(ConsoleColor.Yellow);
                    Console.WriteLine("Still waiting for token.");
                    ConColF(ConsoleColor.Black);
                }
            }

            ConColF(ConsoleColor.Cyan);
            Console.Write("Requesting Token: ");

            ConColF(ConsoleColor.Green);

            var apiAccess = new APIAccess(apiRequest);
            var apiToken = apiAccess.getBearerCode();

            if (apiToken.Error) {
                ConColF(ConsoleColor.Red);
                Console.WriteLine("Failed.");

                Console.WriteLine("EMsg: " + apiToken.Error_Message);
                Console.ReadKey(); return;
            }

            Console.WriteLine("Success.");
            ConColF(ConsoleColor.White);

            Console.WriteLine("Bearer Code: " + apiToken.Token);

            // Call the API
            Console.Write("Test Calling the API: ");

            oAuthAuth oaCreds = new oAuthAuth(apiToken.Token);
            var aReq = new APIRequestHandler();
            var jsonString = aReq.getAPIJson(APIResources.getUser(testUser), oaCreds, true);
            
            Console.WriteLine("JSON STRING: ");
                Console.WriteLine(jsonString + "\n\n");

            Console.Write("Attempting to serialize json: "); {
                var eng = new Engine(oaCreds);
                var ser = new LiveCodingTV.API.Wrappers.Serializer();

                User user = new User();
                
                try {
                    // Surround with a try and catch!
                    user = eng.User.getUser(testUser);
                } catch(Exception ex) {
                    ConColF(ConsoleColor.Red);
                    Console.WriteLine("Error.");
                    Console.WriteLine("EMsg: " + ex.Message);
                    Console.ReadKey(); return;
                }

                // Set the console color and print Success
                ConColF(ConsoleColor.Green);
                    Console.WriteLine("Success");
                ConColF(ConsoleColor.DarkMagenta);

                Console.WriteLine("============================\tUser Information");

                // Print some information about the user
                ConPrintCol("Username",     user.Username);
                ConPrintCol("Country",      user.Country);
                ConPrintCol("Fav Line",     user.FavoriteLineOfCode);
            } 

            Console.ReadKey();
        }
Exemplo n.º 2
0
        private static void ConnectToApi(object sender, DoWorkEventArgs e) {
            /* Initiate connection to API */ {
                // You could add this into another thread
                // to have a animation while the application is connecting
                // etc.
                splash.SetText("Creating Object!");

                // Setup our sided api
                var serverAPIURL = "http://callumcarmicheal.com/LCAPI/"; // LINK TO WHERE YOU UPLOADED AND SETUP THE PHP SCRIPT
                apiRequest = new Request(servResources = new ServResources(serverAPIURL));

                // Get a new GUID for our API Access
                var guidReqResponse = apiRequest.getNewGUID();

                // Print our information from the guidReqResponse
                splash.SetText("Requesting a Authorization GUID and URL");
                var apiSetupResponse = apiRequest.SetupAPIRequest(false); // When using our own checking method, we just check to see if its "RETURN"

                // Check if our state == return (We will handle our own checking of the token)
                if (apiSetupResponse.getState() != ResponseState.RETURN) {
                    splash.SetText("Unexpected Response (" + apiSetupResponse.getState().ToString() + "): " + apiSetupResponse.getMessage());
                }

                // Get our token state
                GUID_Status guidStatus;
                splash.SetText("Waiting for user to accept POPUP");

                while (true) {
                    // Now we play the waiting game!
                    //splash.SetText("State: " + state.ToString() + "\n");

                    // Check our GUID State
                    guidStatus = apiRequest.getGUIDState();

                    if (guidStatus == GUID_Status.STATE_ID_VALID) {
                        splash.SetText("Server recieved token.");
                        break;
                    }

                    // Wait 0.5 seconds before checking
                    // for a token! NOTE: since this is a splashscreen
                    // we dont need to do Application.DoEvents to redraw
                    // the form since it is static.
                    Thread.Sleep(500);
                }


                splash.SetText("Requesting Token");

                apiAccess = new APIAccess(apiRequest);
                var apiToken = apiAccess.getBearerCode();

                if (apiToken.Error) {
                    // TODO: Handle Error
                }

                splash.SetText("Creating extra objects");
                oAuthCreds = new oAuthAuth(apiToken.Token);
                apiEngine  = new Engine(oAuthCreds);


                splash.SetText("Done.");
            }
        }