This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
FNCPro's profile picture

HTTP API checker

Started by FNCPro, 26 June 2013 - 09:02 AM
FNCPro #1
Posted 26 June 2013 - 11:02 AM
This is something you can put into your programs.
I would think a lot of people know how to check if HTTP API is enabled in the config using a program but this is for the people
who don't know.
First make a program or have a program that you want to check if HTTP API is enabled.
This is how I did it.
Place this code near the beginning so it checks right when the program is started…

if not http then
print("You need HTTP API enabled!")
else
--Place code here to run when HTTP API is enabled.
--KEEP THE END AT THE VERY LAST BIT OF CODE.




end
So it basically checks if the HTTP API is disabled or not.
the 'if not http then' statement goes to a global variable in an some file which I don't know(or its global) and it sees if it is false and it would change if the HTTP API was enabled or not. If it was it would change to true which would run the code after the else. If it wasn't enabled it would show on their screen "You need HTTP API enabled!"
nolongerexistant #2
Posted 26 June 2013 - 11:22 AM
You can just do error() to end the program if it's not enabled


if not http then error("HTTP API not enabled") end
ElvishJerricco #3
Posted 26 June 2013 - 11:46 AM
You can just do error() to end the program if it's not enabled


if not http then error("HTTP API not enabled") end

Better yet, use assert(boolean, errorMessage). If the boolean argument is nil or false, it calls error(errorMessage). It's really just a shorthand but it's really handy.


assert(http, "HTTP API not enabled")
FNCPro #4
Posted 26 June 2013 - 12:28 PM
You can just do error() to end the program if it's not enabled


if not http then error("HTTP API not enabled") end

Better yet, use assert(boolean, errorMessage). If the boolean argument is nil or false, it calls error(errorMessage). It's really just a shorthand but it's really handy.


assert(http, "HTTP API not enabled")
I just looked at the pastebin's code and other programs that make use of the HTTP API and see how they made the http checker, and I saw none that had the assert or error function in them so I just used that.
theoriginalbit #5
Posted 26 June 2013 - 12:40 PM
-snip-
-snip-
Or you do this

if not http then error("HTTP API not enabled",0) end
the level of 0 is important as it does not print the file name or the line number, it just prints the error message. much nicer imo!
sadly you cannot do the same with the assert function unless you use the assert override that I have made that adds levels to assert.
Engineer #6
Posted 26 June 2013 - 01:57 PM
Or you do this

if not http then error("HTTP API not enabled",0) end
the level of 0 is important as it does not print the file name or the line number, it just prints the error message. much nicer imo!
sadly you cannot do the same with the assert function unless you use the assert override that I have made that adds levels to assert.
There is another function in CC-Lua which does the same as error( "message", 0 ):

printError("Message! CC has a lot of functions! :3")
theoriginalbit #7
Posted 26 June 2013 - 02:06 PM
Or you do this

if not http then error("HTTP API not enabled",0) end
the level of 0 is important as it does not print the file name or the line number, it just prints the error message. much nicer imo!
sadly you cannot do the same with the assert function unless you use the assert override that I have made that adds levels to assert.
There is another function in CC-Lua which does the same as error( "message", 0 ):

printError("Message! CC has a lot of functions! :3")
Except that printError has only existed since colours were introduced, error with levels has existed since the beginning.
Engineer #8
Posted 26 June 2013 - 02:11 PM
Except that printError has only existed since colours were introduced, error with levels has existed since the beginning.

Im not since the beginning with CC! :P/>
ElvishJerricco #9
Posted 16 July 2013 - 06:42 PM
There is another function in CC-Lua which does the same as error( "message", 0 ):

printError("Message! CC has a lot of functions! :3")
Except that printError has only existed since colours were introduced, error with levels has existed since the beginning.

(sorry to be late) But there's another problem with printError which is that unlike what Engineer said, it doesn't do the same as error(). printError just prints in an error-like format. error() forces the coroutine to die. In fact, when a program uses error() to exit, the shell does a printError() to display the error message. Point being that error exits a coroutine and allows the error message to be handled dynamically, printError prints in red ink.
Zudo #10
Posted 17 July 2013 - 12:42 AM
This isn't really a tutorial though…
Bubba #11
Posted 17 July 2013 - 02:35 PM
This isn't really a tutorial though…

A tutorial is a method of transferring knowledge and may be used as a part of a learning process

I think this qualifies.
LBPHacker #12
Posted 18 July 2013 - 07:20 AM
But there's another problem with printError which is that unlike what Engineer said, it doesn't do the same as error().
He meant it does the same as error("crap", 0)
ElvishJerricco #13
Posted 18 July 2013 - 09:21 AM
He meant it does the same as error("crap", 0)

But… But it doesn't. My point still stands. error() forces the coroutine to exit even when the level is 0, printError() just prints.