示例#1
0
        public IHttpActionResult PayCreateCSTaskAPI()
        {
            ServerManager     serverMgr     = null;
            APIRequestHandler handler       = null;
            APIResponseDTO    response      = new APIResponseDTO();
            string            clientAddress = null;

            try
            {
                clientAddress = HttpContext.Current.Request.UserHostAddress;
                serverMgr     = Global.GetCreateServerManager(HttpContext.Current.Server);
                handler       = new APIRequestHandler(HttpContext.Current.Request, serverMgr);

                if (!handler.PayCreateCSTask(out response))
                {
                    return(Content(HttpStatusCode.InternalServerError, response));
                }
                return(Content(HttpStatusCode.OK, response));
            }
            catch (Exception ex)
            {
                Global.Log.Error(ex.Message);
                response.SetResponse("Cannot pay and create the crowd-sensing task. Please try again!", APIResponseDTO.RESULT_TYPE.ERROR);
                return(Content(HttpStatusCode.InternalServerError, response));
            }
        }
示例#2
0
        public IHttpActionResult RegisterNewUser()
        {
            ServerManager     serverMgr = null;
            APIRequestHandler handler;
            APIResponseDTO    response      = new APIResponseDTO();
            string            clientAddress = null;

            try
            {
                clientAddress = HttpContext.Current.Request.UserHostAddress;
                serverMgr     = Global.GetCreateServerManager(HttpContext.Current.Server);
                handler       = new APIRequestHandler(HttpContext.Current.Request, serverMgr);
                if (!handler.RegisterUser(out response))
                {
                    return(Content(HttpStatusCode.InternalServerError, response));
                }
                return(Content(HttpStatusCode.OK, response));
            }
            catch (Exception ex)
            {
                Global.Log.Error(ex.Message);
                response.SetResponse("Registration failed. Please try again!", APIResponseDTO.RESULT_TYPE.ERROR);
                return(Content(HttpStatusCode.InternalServerError, response));
            }
        }
示例#3
0
        public IHttpActionResult GetCostCSTaskAPI()
        {
            ServerManager     serverMgr = null;
            APIRequestHandler handler   = null;
            APIResponseDTO    response  = new APIResponseDTO();
            string            clientAddress;

            try
            {
                clientAddress = HttpContext.Current.Request.UserHostAddress;
                serverMgr     = Global.GetCreateServerManager(HttpContext.Current.Server);
                handler       = new APIRequestHandler(HttpContext.Current.Request, serverMgr);

                Global.Log.DebugFormat("Client Ip Address: {0}", clientAddress);
                if (!handler.CalcCostCSTask(out response))
                {
                    return(Content(HttpStatusCode.InternalServerError, response));
                }
                return(Content(HttpStatusCode.OK, response));
            }
            catch (Exception ex)
            {
                Global.Log.Error(ex.Message);
                response.SetResponse("Cannot calculate the task cost. Please try again!", APIResponseDTO.RESULT_TYPE.ERROR);
                return(Content(HttpStatusCode.InternalServerError, response));
            }
        }
示例#4
0
        public IHttpActionResult GetTaskReward()
        {
            ServerManager     serverMgr = null;
            APIRequestHandler handler   = null;
            APIResponseDTO    response  = new APIResponseDTO();

            try
            {
                serverMgr = Global.GetCreateServerManager(HttpContext.Current.Server);
                handler   = new APIRequestHandler(HttpContext.Current.Request, serverMgr);
                if (!handler.GetTaskReward(out response))
                {
                    return(Content(HttpStatusCode.InternalServerError, response));
                }
                return(Content(HttpStatusCode.OK, response));
            }
            catch (Exception ex)
            {
                Global.Log.Error(ex.Message);
                return(Content(HttpStatusCode.InternalServerError, "ERROR"));
            }
        }
示例#5
0
        public IHttpActionResult GetTopics()
        {
            ServerManager     serverMgr = null;
            APIRequestHandler handler   = null;
            APIResponseDTO    response  = new APIResponseDTO();

            try
            {
                serverMgr = Global.GetCreateServerManager(HttpContext.Current.Server);
                handler   = new APIRequestHandler(HttpContext.Current.Request, serverMgr);
                if (!handler.GetTopics(out response))
                {
                    return(Content(HttpStatusCode.InternalServerError, response));
                }
                return(Content(HttpStatusCode.OK, response));
            }
            catch (Exception ex)
            {
                Global.Log.Error(ex.Message);
                response.SetResponse("Cannot obtain topics from server. Please try again!", APIResponseDTO.RESULT_TYPE.ERROR);
                return(Content(HttpStatusCode.InternalServerError, response));
            }
        }
示例#6
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();
        }