All Projects → Crell → Apiproblem

Crell / Apiproblem

Licence: mit
A simple implementation of the api-problem specification. Includes PSR-15 support.

Labels

Projects that are alternatives of or similar to Apiproblem

Node Js2xmlparser
Popular Node.js module for parsing JavaScript objects into XML
Stars: ✭ 171 (-24%)
Mutual labels:  json, xml
Validation
validation api extracted from play
Stars: ✭ 194 (-13.78%)
Mutual labels:  json, xml
Qxorm
QxOrm library - C++ Qt ORM (Object Relational Mapping) and ODM (Object Document Mapper) library - Official repository
Stars: ✭ 176 (-21.78%)
Mutual labels:  json, xml
Acl
Server framework and network components written by C/C++ for Linux, Mac, FreeBSD, Solaris(x86), Windows, Android, IOS
Stars: ✭ 2,113 (+839.11%)
Mutual labels:  json, xml
Python Benedict
dict subclass with keylist/keypath support, I/O shortcuts (base64, csv, json, pickle, plist, query-string, toml, xml, yaml) and many utilities. 📘
Stars: ✭ 204 (-9.33%)
Mutual labels:  json, xml
Exportsheetdata
Add-on for Google Sheets that allows sheets to be exported as JSON or XML.
Stars: ✭ 170 (-24.44%)
Mutual labels:  json, xml
Libxo
The libxo library allows an application to generate text, XML, JSON, and HTML output using a common set of function calls. The application decides at run time which output style should be produced.
Stars: ✭ 185 (-17.78%)
Mutual labels:  json, xml
Gelatin
Transform text files to XML, JSON, or YAML
Stars: ✭ 150 (-33.33%)
Mutual labels:  json, xml
Unbescape
Advanced yet easy to use escaping library for Java
Stars: ✭ 207 (-8%)
Mutual labels:  json, xml
Iran
Administrative divisions of Iran in json and xml formats - تقسیمات کشوری ایران با فرمت جی‌سان و ایکس ام ال
Stars: ✭ 201 (-10.67%)
Mutual labels:  json, xml
Xresloader
跨平台Excel导表工具(Excel=>protobuf/msgpack/lua/javascript/json/xml)
Stars: ✭ 161 (-28.44%)
Mutual labels:  json, xml
Renderer
Simple, lightweight and faster response (JSON, JSONP, XML, YAML, HTML, File) rendering package for Go
Stars: ✭ 220 (-2.22%)
Mutual labels:  json, xml
Uxdm
🔀 UXDM helps developers migrate data from one system or format to another.
Stars: ✭ 159 (-29.33%)
Mutual labels:  json, xml
Goxml2json
XML to JSON converter written in Go (no schema, no structs)
Stars: ✭ 170 (-24.44%)
Mutual labels:  json, xml
Serializer
With the Serializer component it's possible to handle serializing data structures, including object graphs, into array structures or other formats like XML and JSON. It can also handle deserializing XML and JSON back to object graphs.
Stars: ✭ 2,021 (+798.22%)
Mutual labels:  json, xml
Bancosbrasileiros
Lista de bancos brasileiros | Brazilian banks list
Stars: ✭ 178 (-20.89%)
Mutual labels:  json, xml
Omniparser
omniparser: a native Golang ETL streaming parser and transform library for CSV, JSON, XML, EDI, text, etc.
Stars: ✭ 148 (-34.22%)
Mutual labels:  json, xml
Configurate
A simple configuration library for Java applications providing a node structure, a variety of formats, and tools for transformation
Stars: ✭ 148 (-34.22%)
Mutual labels:  json, xml
Xlog
Android logger, pretty, powerful and flexible, log to everywhere, save to file, all you want is here.
Stars: ✭ 2,468 (+996.89%)
Mutual labels:  json, xml
Biblia
Bíblia: XML + SQL + JSON
Stars: ✭ 211 (-6.22%)
Mutual labels:  json, xml

ApiProblem

Build Status

This library provides a simple and straightforward implementation of the IETF Problem Details for HTTP APIs, RFC 7807.

RFC 7807 is a simple specification for formatting error responses from RESTful APIs on the web. This library provides a simple and convenient way to interact with that specification. It supports generating and parsing RFC 7807 messages, in both JSON and XML variants.

Generating responses

What's that you say? Someone sent your API a bad request? Tell them it's a problem!

use Crell\ApiProblem\ApiProblem;

$problem = new ApiProblem("You do not have enough credit.", "http://example.com/probs/out-of-credit");
// Defined properties in the API have their own setter methods.
$problem
  ->setDetail("Your current balance is 30, but that costs 50.")
  ->setInstance("http://example.net/account/12345/msgs/abc");
// But you can also support any arbitrary extended properties!
$problem['balance'] = 30;
$problem['accounts'] = [
  "http://example.net/account/12345",
  "http://example.net/account/67890"
];

$json_string = $problem->asJson();

// Now send that JSON string as a response along with the appropriate HTTP error
// code and content type which is available via ApiProblem::CONTENT_TYPE_JSON.
// Also check out asXml() and ApiProblem::CONTENT_TYPE_XML for the angle-bracket fans in the room.

Or, even better, you can subclass ApiProblem for a specific problem type (since the type and title are supposed to go together and be relatively fixed), then just populate your own error-specific data. Just like extending an exception!

If you're using a library or framework that wants to do its own JSON serialization, that's also fully supported. ApiProblem implements\JsonSerializable, so you can pass it directly to json_encode() as if it were a naked array.

$response = new MyFrameworksJsonResponse($problem);

// Or do it yourself
$body = json_encode($problem);

Sending Responses

You're probably using PSR-7 for your responses. That's why this library includes a utility to convert your ApiProblem object to a PSR-7 ResponseInterface object, using a PSR-17 factory of your choice. Like so:

use Crell\ApiProblem\HttpConverter;

$factory = getResponseFactoryFromSomewhere();

// The second parameter says whether to pretty-print the output.
$converter = new HttpConverter($factory, true);

$response = $converter->toJsonResponse($problem);
// or
$response = $converter->toXmlResponse($problem);

That gives back a fully-functional and marked Response object, ready to send back to the client.

Receiving responses

Are you sending messages to an API that is responding with API-Problem errors? No problem! You can easily handle that response like so:

use Crell\ApiProblem\ApiProblem;

$problem = ApiProblem::fromJson($some_json_string);
$title = $problem->getTitle();
$type = $problem->getType();
// Great, now we know what went wrong, so we can figure out what to do about it.

(It works for fromXml(), too!)

Installation

Install ApiProblem like any other Composer package:

composer require crell/api-problem

See the Composer documentation for more details.

License

This library is released under the MIT license. In short, "leave the copyright statement intact, otherwise have fun." See LICENSE for more information.

Contributing

Pull requests accepted! The goal is complete conformance with the IETF spec.

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].