Skip to content

A simple framework for working with the pnut.io API.

License

Notifications You must be signed in to change notification settings

shawnthroop/Peanut

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Peanut

A simple framework for working with the pnut.io API.

Authorization

First, create a Client value with your Client ID and Password Grant Secret. These can be found in your account's Developer settings.

let client = Client(id: "client-id", passwordGrantSecret: "client-secret", scope: [.basic, .stream])

or configure the shared value:

Client.shared.id = "client-id"
Client.shared.passwordGrantSecret = "client-secret"
Client.shared.scope = [.basic, .stream]

Then request authentication using your username and password:

let request = Authorization(username: "username", password: "password", client: client).request()

URLSession.shared.data(for: request) { result in
    print(result.success)   // APIAuthorizationResponse(id: ...)
}

Note: if you've configured the shared client simply ommit the client parameter; it will be used by default.

Fetching Data

Fetching streams, updating objects, and performing actions are all done via the same protocol: APIRequestable. To fetch the posts from the global stream:

let request = Post.Stream.global.request(pagination: APIPagination(count: 10))

URLSession.shared.data(for: request) { result in
    print(result.success)   // APIObjectResponse<Post>(meta: ..., data: ...)
}

More complicated requests are possible. For example, it is possible to include request specific parameters, pagination, token and even an API (though only v0 exists at the moment):

let request = Post.Stream.mentions(4).request(for: .v0, parameters: [.includeRaw: true, .includeUser: false, .includeHTML: false], token: token, pagination: APIPagination(before: 379886, count: 2))

Note: Including [.includeUser: false] with User.Stream endpoints is not currently supported and an error will be thrown while parsing.

Composing Posts

Creating posts is similar to fetching data. For this example I’ve uploaded a photo to pnut and have a File value handy. Simply append a replacement value to your draft:

var draft = Post.Draft(text: "Terry Loves Yogurt!")
draft.raw.append(.oembedReplacement(id: file.id, token: file.token!))

Then create the request using a Post.Mutation endpoint:

let request = Post.Mutation.publish(draft, updatePersonalStreamMarker: true).request(parameters: [.includePostRaw: true], token: token)

URLSession.shared.data(for: request) { result in
    // ...
}

Uploading Files

Similar to posts, publishing a file starts with a draft:

let draft = File.Draft(type: "com.my-app.image", mimeType: .png, name: "terry-loves-yogurt.png")

Then use a File.Mutation endpoint to publish a draft with the accompanying image data:

let image = UIImage(named: "...")!
let request = File.Mutation.publish(draft, content: UIImagePNGRepresentation(image)!, sha256: nil).request(token: token)

URLSession.shared.data(for: request) { result in
    // ...
}

To-Do

This is very much a work-in-progress. Next on the list:

  • User editing
  • Avatar and cover image uploading
  • Better documentation

Contact

You can find me on pnut.io and Twitter

About

A simple framework for working with the pnut.io API.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages