Official Python client for Whipcode API
Project description
whipcode-py
A convenient way to access Whipcode API from Python applications.
Compatible with self-hosted instances as well.
Installation
Get it from PyPI:
pip install whipcode
Usage
Here's an asynchronous snippet:
import asyncio
from whipcode import Whipcode, Langs
from whipcode.exceptions import RequestError, PayloadBuildError
async def main():
whip = Whipcode()
whip.rapid_key("YOUR_RAPIDAPI_KEY") # not needed if the RAPID_KEY environment variable is set
code = "echo 'Hello World!'"
execution = whip.run_async(code, Langs.BASH)
# Other tasks while the execution is in progress
try:
result = await execution
except (RequestError, PayloadBuildError) as e:
# Handle the error
asyncio.run(main())
And a synchronous one:
from whipcode import Whipcode, Langs
from whipcode.exceptions import RequestError, PayloadBuildError
def main():
whip = Whipcode()
code = '(println "Hello World!")'
try:
result = whip.run(code, Langs.CLOJURE)
except (RequestError, PayloadBuildError) as e:
# Handle the error
main()
The result object:
ExecutionResult(status=200, stdout='Hello World!\n', stderr='', container_age=0.338638005, timeout=False, detail='', rapid={'messages': '', 'message': '', 'info': ''})
Providers
Changing the provider is easy. Here's how the default provider is defined:
{
"endpoint": "https://whipcode.p.rapidapi.com/run",
"headers": {
"X-RapidAPI-Key": "",
"X-RapidAPI-Host": "whipcode.p.rapidapi.com"
},
"query_injects": [
{}
]
}
Just pass your custom provider to the constructor:
whip = Whipcode(custom_provider)
Or swap it in on the already initialized object:
whip.provider = custom_provider
An example custom provider:
{
"endpoint": "https://<host>/run",
"headers": {
"Authorization": "Bearer xxx"
},
"query_injects": []
}
Reference
Constructor
Whipcode(provider: dict = Whipcode.default_provider)
Parameters:
- provider - dict, optional
The provider configuration. See the Providers section.
rapid_key
rapid_key(key: str)
Sets the RapidAPI key to use when making requests.
Parameters:
- key - str
Your RapidAPI key.
run
run(code: str, language: str | int, args: list = [], timeout: int = 0) -> ExecutionResult
Makes a request to the endpoint synchronously.
Parameters:
- code - str
The code to execute. - language - str, int
Language ID of the submitted code. - args - list, optional
A list of compiler/interpreter args. - timeout - int, optional
Timeout in seconds for the code to run. - stdin - str, optional
Standard input passed to the execution. - env - dict, optional
Environment variables to set.
Returns:
run_async
run_async(code: str, language: str | int, args: list = [], timeout: int = 0) -> asyncio.Task
Makes a request to the endpoint asynchronously.
Parameters:
- code - str
The code to execute. - language - str, int
Language ID of the submitted code. - args - list, optional
A list of compiler/interpreter args. - timeout - int, optional
Timeout in seconds for the code to run. - stdin - str, optional
Standard input passed to the execution. - env - dict, optional
Environment variables to set.
Returns:
- A Task that returns ExecutionResult.
ExecutionResult
ExecutionResult(stdout: str, stderr: str, container_age: float, timeout: bool, status: int, detail: str, rapid: dict = {})
Returned as the result after a request.
Attributes
- stdout - str
All data captured from stdout. - stderr - str
All data captured from stderr. - container_age - float
Duration the container allocated for your code ran, in seconds. - timeout - bool
Boolean value depending on whether your container lived past the timeout period. - status - int
The status code of the request response. - detail - str
Details about why the request failed to complete. - rapid - dict
Various keys that RapidAPI uses when returning their own error messages.
Langs
Langs:
PYTHON = 1
JAVASCRIPT = 2
BASH = 3
PERL = 4
LUA = 5
RUBY = 6
C = 7
CPP = 8
RUST = 9
FORTRAN = 10
HASKELL = 11
JAVA = 12
GO = 13
TYPESCRIPT = 14
CLISP = 15
RACKET = 16
CRYSTAL = 17
CLOJURE = 18
X86 = 19
ZIG = 20
NIM = 21
DLANG = 22
CSHARP = 23
RSCRIPT = 24
DART = 25
VB = 26
FSHARP = 27
PHP = 28
A mapping of language IDs to their respective names.
Exceptions
- RequestError - Raised when an error occurs during the request
- PayloadBuildError - Raised when an error occurs while building the payload
Contributing
Please read the contributing guidelines before opening a pull request.
License
This library is licensed under the MIT License.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file whipcode-0.2.1.tar.gz.
File metadata
- Download URL: whipcode-0.2.1.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a5436289bb58d2c4414798fe3aa8fae941ba96c054a07b267b15adfff7b840c
|
|
| MD5 |
d4316386693cc220f8216737bda3b7eb
|
|
| BLAKE2b-256 |
79e21a6004e0ccfc8dee15237661b688f7033792cea8865e0e371722d0a74f7c
|
File details
Details for the file whipcode-0.2.1-py3-none-any.whl.
File metadata
- Download URL: whipcode-0.2.1-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3835b6cb0b7162aebaa4a8715d9cb1dbb1cd9f7d578eedba59ebc98275f90b39
|
|
| MD5 |
a5ce7fef71cd1bdc809d1446b1a60aba
|
|
| BLAKE2b-256 |
d4f2c8aafe917fc654455f2f49d081fe1450a2b930daa0fde6d19b9de2cf2fb9
|