Connector

A single-connection web client that reads a page's html line by line.

A Connector holds at most one open connection at a time. Call open to establish it, read through it with next/hasNext (or by iterating the Connector itself), then release it with close. A second open call blocks until the current connection is closed, so a Connector is safe to share between threads but serializes them.

Stateless one-shot helpers (get, skrape, download) do not use the shared connection and can be called at any time.

Every operation that can fail returns a Result rather than throwing or returning null, so callers decide how to recover:

val connector = Connector()
connector.open("https://example.com")
.flatMap { connector.next() }
.onSuccess { line -> println(line) }
.onFailure { cause -> log.warn("could not read page", cause) }
connector.close()

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard
object Companion

Holds the logger and the private constants the instances share; it exposes no public member.

Properties

Link copied to clipboard

The most recent line read by next from the currently-open connection, or null if nothing has been read since the last open.

Functions

Link copied to clipboard
fun close(): Result<Unit>

Closes the currently established connection. Must be called before another connection can be established.

Link copied to clipboard
infix fun download(imageUrl: String): Result<BufferedImage>

Downloads and decodes an image from the given URL.

Link copied to clipboard
infix fun get(url: String): Result<String>

Performs a GET request on a given URL.

Link copied to clipboard
operator fun hasNext(): Boolean

Checks whether another line is available to read from the current connection.

Link copied to clipboard
operator fun iterator(): Connector

Allows a Connector to be iterated directly, e.g. in a for loop over its lines. Because next returns a Result, the loop variable is a Result<String>.

Link copied to clipboard
operator fun next(): Result<String>

Goes to the next line in the html data of the currently-open connection.

fun next(lines: Int): Result<Unit>

Skips the specified number of lines in the html data of the currently-open connection, stopping at the first line that could not be read.

Link copied to clipboard
infix fun open(urlName: String): Result<Unit>

Opens a new connection with the given URL.

Link copied to clipboard
infix fun skrape(url: String): Result<String>

Reads the given URL using the Skrape library.