Building Swift things against the Write.as API

WriteFreely Swift Package v0.1.0 Released

Over the last few days, I've been working on wrapping the WriteFreely API in a Swift Package for use in your iOS and macOS apps. I'm happy to announce that an alpha version is now available!

Check it out here: https://github.com/writeas/writefreely-swift

It's still not quite complete —for example, it's lacking a test suite, and the code could use some polish— but if you're so inclined, give it a try and let me know if it works for you.

Some Technical Details

All methods on the WriteFreelyClient use completion blocks that leverage the Result type in Swift. So, for example, if you're trying to get a post by its ID, you'd get that info in a handler

func getPostHandler(result: (Result<Post, Error>)) {
	do {
		let post = try result.get()
		print("\(post.title): \(post.body)")
	} catch {
		print(error)
	}
}

let client = WriteFreelyClient(for: URL(string: "https://your.writefreely.instance/")!)
client.getPost(token: "myus3rt0k3n", byId: "th3p0st1d", completion: getPostHandler)

The getPostHandler is called when the server's response is received, and tries to get the Post object from the returned Result — if that fails, the catch block is executed, but if it succeeds, then you can do whatever you need to do with the Post.

This pattern follows throughout the client which, by the way, uses good ol' URLSession.

Next Steps

I intend on steadily improving this library, and supporting the WriteFreely community in building Swift clients that consume it. It's been a fun learning experience for me, and I can't wait to see what you all create with it.


Enter your email to subscribe to updates:

You can also subscribe via RSS or follow @angelo@write.as on Mastodon.