All Projects → jongacnik → Sirvy

jongacnik / Sirvy

Licence: mit
🔗 Kirby Services API

Projects that are alternatives of or similar to Sirvy

Spyne
A transport agnostic sync/async RPC library that focuses on exposing services with a well-defined API using popular protocols.
Stars: ✭ 992 (+1581.36%)
Mutual labels:  api, json
Http Prompt
An interactive command-line HTTP and API testing client built on top of HTTPie featuring autocomplete, syntax highlighting, and more. https://twitter.com/httpie
Stars: ✭ 8,329 (+14016.95%)
Mutual labels:  api, json
Pantry
🥑 Free data storage as a service that allows devs to store JSON for multiple apps & users. A good resource when building personal projects, apps for hackathons, and prototypes alike.
Stars: ✭ 42 (-28.81%)
Mutual labels:  api, json
Apicache
Simple API-caching middleware for Express/Node.
Stars: ✭ 957 (+1522.03%)
Mutual labels:  api, json
Json Api Dart
JSON:API client for Dart/Flutter
Stars: ✭ 53 (-10.17%)
Mutual labels:  api, json
Node Quick Mock
🌞 基于Express的mock接口平台
Stars: ✭ 33 (-44.07%)
Mutual labels:  api, json
Generator Http Fake Backend
Yeoman generator for building a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 49 (-16.95%)
Mutual labels:  api, json
Movement
Movement is an easier, simpler way to explore and use NIEM. Want to join the Movement and contribute to it? Start here.
Stars: ✭ 19 (-67.8%)
Mutual labels:  api, json
Cucumber Api
API validator in BBD style with Cucumber
Stars: ✭ 50 (-15.25%)
Mutual labels:  api, json
Flask Restx
Fork of Flask-RESTPlus: Fully featured framework for fast, easy and documented API development with Flask
Stars: ✭ 1,050 (+1679.66%)
Mutual labels:  api, json
Fastapi
FastAPI framework, high performance, easy to learn, fast to code, ready for production
Stars: ✭ 39,588 (+66998.31%)
Mutual labels:  api, json
Easyjson
Provides an unified JSON access API, you can adapter any JSON library to Gson, Jackson, FastJson with easyjson。 提供了一个JSON门面库,就像slf4j一样。easyjson本身不做json的操作,完全依赖于底层实现库。可以直接使用Easyjson的API,底层的JSON库随时可切换。也可以使用其中某个json的API,然后通过easyjson适配给其他的json库
Stars: ✭ 54 (-8.47%)
Mutual labels:  api, json
Bandcamp Api
API wrapper for querying band, album, and track data from bandcamp.com
Stars: ✭ 20 (-66.1%)
Mutual labels:  api, json
Realtime Newsapi
Financial News Aggregator - Real Time & Query API for Financial News
Stars: ✭ 34 (-42.37%)
Mutual labels:  api, json
Geochile
Esta es una api de Geocodificación, para que, con las coordenadas Latitud y Longitud se entregue una lista de ciudades cercanas.
Stars: ✭ 13 (-77.97%)
Mutual labels:  api, json
Dito
Dito.js is a declarative and modern web framework with a focus on API driven development, based on Objection.js, Koa.js and Vue.js – Released in 2018 under the MIT license, with support by Lineto.com
Stars: ✭ 44 (-25.42%)
Mutual labels:  api, json
Api
姬长信API For Docker 一个基于多种编程语言开源免费不限制提供生活常用,出行服务,开发工具,金融服务,通讯服务和公益大数据的平台.
Stars: ✭ 743 (+1159.32%)
Mutual labels:  api, json
Telize
High performance JSON IP and GeoIP REST API (IP Geolocation)
Stars: ✭ 774 (+1211.86%)
Mutual labels:  api, json
Jsonapi parameters
Rails-way to consume JSON:API input
Stars: ✭ 50 (-15.25%)
Mutual labels:  api, json
Lumen Api Starter
Quickstarter for Lumen
Stars: ✭ 54 (-8.47%)
Mutual labels:  api, json

Sirvy: Kirby Services API

This is a plugin for Kirby that introduces an extensible URL based API for performing services such as resizing images or returning page contents as json.

Installation

Put the sirvy folder in /site/plugins.

What does this do?

Sirvy injects a /sirvy/(:all?) route into your site. This route passes data to PHP functions (we're calling them services).

For example, after you've added Sirvy to your project, you can access your homepage contents as json via this url:

http://yourkirby.com/sirvy/home?service=json

In fact, you could access any page's contents as json:

http://yourkirby.com/sirvy/some/uri/here?service=json

The /sirvy route grabs the page object based on the uri and passes it into a service function. In this case it is being passed into the json service function. The json service takes a page object and returns the response as json.

Services

Sirvy comes with 4 services built-in, but you can add as many services as you need!

tree

/sirvy/home?service=tree

returns page contents, files, and children recursively as json

json

/sirvy/home?service=json

returns page contents as json

resize

/sirvy/home?service=resize&image=image.jpg&width=400

returns a resized thumbnail

crop

/sirvy/home?service=crop&image=image.jpg&width=200&height=300

returns a cropped thumbnail

Sirvy Page Method

Instead of manually building up the URLs for services, a Sirvy page method is exposed to make this super simple:

page('home')->sirvy('json');
// returns
// -> http://yourkirby.com/sirvy/home?service=json

The first parameter is always the name of the service, the second optional parameter is for passing in an array of additional data:

page('home')->sirvy('crop', [
  'image'  => page('home')->image()->filename(),
  'width'  => 200,
  'height' => 300
]);
// returns
// -> .../sirvy/home?service=crop&image=image.jpg&width=200&height=300

This is great for spitting out many thumbnails without blocking initial page render:

<? foreach ($hundredsOfImages as $i) :
  $thumb = $page->sirvy('resize', [
    'image' => $i->filename(),
    'width' => 500
  ]) ?>
  <img src="<?= $thumb ?>">
<? endforeach ?>

Custom Services

The real power of Sirvy is in defining your own services. You want an API to expose not just the current page's content as json, but all of its children and files as well? You can make a service for that! You want an API to expose the page's contents as a PHP array? You can make a service for that!

Services are registered 2 ways:

  1. From PHP files in the services folder inside of the Sirvy plugin folder
  2. By registering them as an option in your config.php

The 3 included services (json, resize, and crop) are all defined as files in the services folder. The name of the file becomes the name of the service (json.php -> json). To add your own services, just add more files to this folder. The file needs to return a function. Unless you are returning media files, you will typically want this function to return a Kirby response object. Take a peek at the included services as examples.

You can also register services via an option in your config.php. The key of the array becomes the name of the service. Here is how you could add a toArray service that exposes the page's contents as a PHP array:

c::set('sirvy.services', [
  'toArray' => function ($page, $data) {
    return new Response(print_r($page->toArray()));
  }
]);

// you can now use
page('home')->sirvy('toArray')

// which returns
// -> http://yourkirby.com/sirvy/home?service=toArray

// That url would respond with the page object as a plain PHP array!

Caching

Caching is enabled on a per-service basis using the Kirby file cache. This is great if you want to cache partials like json or html snippets:

/sirvy/home?service=json&cache=1

This json response will be cached in the Kirby cache directory.

By default the cached items will never expire, but you can change this with an option. The cache is always flushed when making content changes via the panel.

Caching will only work when your service returns a Kirby response object. I also wouldn't recommend trying to cache a non-text response (like image or video). I haven't put in catches so weirdness will likely happen.

Options

// change the sirvy path
c::set('sirvy.path', 's');
// -> http://yourkirby.com/s/home?service=json

// expire cached responses after an hour
c::set('sirvy.cache.duration', 60);

// register additional services
c::set('sirvy.services', [
  'serviceName' => function ($page, $data) {
    return new Response('content');
  }
]);

// enable cors on sirvy requests
c::set('sirvy.cors', '*');

Errors

If Sirvy encounters an error, such as being unable to find a page uri or service, a json error response will be returned (status code 400).

Security

As brought up in the early 2.3 discussions, an API like this could potentially lead to misuse when considering image manipulation. I might look into adding some built-in options for rate limiting or auth, but in the meantime if this is a concern for you, nothing stopping you from adding the functionality into the services yourself!

Todo

  • Rate Limiting? See Security

Author

Jon Gacnik http://jongacnik.com

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].