GithubHelp home page GithubHelp logo

shawnthroop / peanut Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 114 KB

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

License: MIT License

Ruby 0.35% Swift 99.46% Objective-C 0.20%
pnut swift framework

peanut's Introduction

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

peanut's People

Contributors

shawnthroop avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

peanut's Issues

Create Kind enum

Different Kinds can have different sub-types:

enum TextKind {
    case text(String)
    case msWord
    // ... etc 
    case json
    case javascript
}

enum AudioKind {
    case mpeg, mp4, wave, flac
}

enum ImageKind {
    case png, jpg, gif
}

Or have them all be subtypes of Kind:

enum Kind {
    case text(Text)
    case audio(Audio)
    // ... etc
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.