https://github.com/losizm/scurry
The Groovy-esque wrapper for Scamper
https://github.com/losizm/scurry
groovy http scamper
Last synced: 6 months ago
JSON representation
The Groovy-esque wrapper for Scamper
- Host: GitHub
- URL: https://github.com/losizm/scurry
- Owner: losizm
- License: apache-2.0
- Created: 2023-10-23T18:02:44.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-05T07:21:53.000Z (12 months ago)
- Last Synced: 2025-04-05T08:24:55.356Z (12 months ago)
- Topics: groovy, http, scamper
- Language: Scala
- Homepage:
- Size: 2.61 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Scurry
[](https://central.sonatype.com/search?q=g:com.github.losizm%20a:scurry_3)
The Groovy-_esque_ wrapper for [Scamper](https://github.com/losizm/scamper).
Although there are no references to Groovy in codebase, this library is intended
for Groovy developers. It defines an interface to Scamper using a mixture of
static and dynamic typing to provide a fluid programming experience.
## Getting Started
To get started, add **Scurry** to your sbt project:
```scala
libraryDependencies += "com.github.losizm" %% "scurry" % "1.0.0"
```
You'll need **Scamper** as well:
```scala
libraryDependencies += "com.github.losizm" %% "scamper" % "42.0.0"
```
### HTTP Server
Here's an example Groovy script to run an HTTP server:
```groovy
import scurry.http.HttpServer
import scurry.http.response.BadRequest
import scurry.http.response.Ok
// Define utility to log HTTP messages
def logHttpMessage(msg) {
println "[server] ${msg.startLine}"
msg.headers.each { println "[server] $it" }
println ''
msg
}
// Create HTTP server
def server = new HttpServer(host: 'localhost', port: 8080)
// At this point, the server hasn't been started.
// You'll want to add some endpoints first.
// Log incoming requests
server.incoming { logHttpMessage(it) }
// Handle GET requests
server.get('/greet') { req ->
def name = req.query.name
if (name == null) new Ok(body: 'Hello, stranger!')
else new Ok(body: "Hello, $name!")
}
// Handle POST requests
server.post('/echo') { req ->
def message = req.body.toString(8192)
if (message == '') new BadRequest(body: 'No message.')
else new Ok(body: message)
}
// Log outgoing responses
server.outgoing { logHttpMessage(it) }
Thread.start {
try {
// Run server for 30 seconds
server.start()
sleep(30000)
}
finally {
server.stop()
}
}
```
### HTTP Client
In this Groovy script example, the HTTP client talks to the server created in
previous section:
```groovy
import scurry.http.HttpClient
// Create HTTP client using custom settings
def client = new HttpClient(
resolveTo: [host: 'localhost', port: 8080, secure: false],
accept: '*/*',
acceptEncoding: ['deflate', 'gzip']
)
// Send GET request
client.get(target: '/greet', query: [name: 'Lupita']) { res ->
println "[client] ${res.body.toString(8192)}"
}
// Send POST request with message body
client.post(target: '/echo', body: 'Can you hear me?') { res ->
println "[client] ${res.body.toString(8192)}"
}
// Send empty POST request and handle client error
client.post(target: '/echo', body: null) { res ->
println "[client] ${res.statusCode} ${res.reasonPhrase}"
if (res.successful) println "[client] This won't print."
else println "[client] Oops! ${res.body.toString(8192)}"
}
```
## API Documentation
See [scaladoc](https://losizm.github.io/scurry/latest/api/index.html) for
additional details.
## License
**Scurry** is licensed under Apache License, Version 2. See [LICENSE](LICENSE)
for more information.