GithubHelp home page GithubHelp logo

api-request-response-formatter's Introduction

Waynabox Api Formatter

A bundle of tools to handle Waynabox API request and responses.

How to create a Waynabox API Response

 public function helloAction(Request $request)
 {
    try {
        $serviceResponseData = $this->myService($request->request->all());
        return ApiResponseFactory::buildAcceptedResponse($serviceResponse->response())->output();
    } catch (MyAuthenticationFailedException $e) {
        return ApiResponseFactory::buildAuthenticationFailedResponse($e->getMessage())->output();
    } catch (\Exception $e) {
        return ApiResponseFactory::buildBadRequestResponse($e->getMessage())->output();
    }
}

API Response types

Returning an accepted response (Code 200)
 public function helloAction(Request $request)
 {
        $response = ApiResponseFactory::buildAcceptedResponse('text on response data section');
        return $response->output();
 }
Returning a created response (Code 201)
 public function helloAction(Request $request)
 {
        $response = ApiResponseFactory::buildCreatedResponse('text on response data section');
        return $response->output();
 }
Returning a bad request response (Code 400)
 public function helloAction(Request $request)
 {
        $response = ApiResponseFactory::buildBadRequestResponse('text on response error section');
        return $response->output();
 }
Returning an authentication failed response (Code 401)
 public function helloAction(Request $request)
 {
        $response = ApiResponseFactory::buildAuthenticationFailedResponse('text on response error section');
        return $response->output();
 }
Returning a not found response (Code 404)
 public function helloAction(Request $request)
 {
        $response = ApiResponseFactory::buildNotFoundResponse('text on response error section');
        return $response->output();
 }
Returning a server error response (Code 500)
 public function helloAction(Request $request)
 {
        $response = ApiResponseFactory::buildServerErrorResponse('text on response error section');
        return $response->output();
 }

Output formatter types

OutputFormat::JSON_STRING (default value)

Use this to output a previously generated json as string on browser. It puts as header 'Content-type: json' This option will add additional data on json response for status (OK/KO), error response and timestamp.

$data = '{"param1":"value 1","param2":"value 2"}';
return ApiResponseFactory::buildAcceptedResponse(
            $data
        )->output();
//outputs '{"status":"OK","data":{"param1":"value 1","param2":"value 2"},"error":{},"date":"2017-01-01 01:01:01"}'
OutputFormat::HTML

Use this to output a string as plain text on browser. It puts as header 'Content-type: text/html'

$data = '<html>hello</html>';
return ApiResponseFactory::buildAcceptedResponse(
            $data,
            OutputFormat::HTML
        )->output();
//outputs '<html>hello</html>'
OutputFormat::PLAIN_TEXT

Use this to output a string as plain text on browser. It puts as header 'Content-type: text'

$data = 'my text';
return ApiResponseFactory::buildAcceptedResponse(
            $data,
            OutputFormat::PLAIN_TEXT
        )->output();
//outputs 'my text'
OutputFormat::JSON

Use this to output an array with data as a json encoded on browser. It puts as header 'Content-type: json'.

This option will add additional data on json response for status (OK/KO), error response and timestamp

$data = ['param1' => 'value 1', 'param2' => 'value 2'];
return ApiResponseFactory::buildAcceptedResponse(
            $data,
            OutputFormat::JSON
        )->output();
//outputs '{"status":"OK","data":{"param1":"value 1","param2":"value 2"},"error":{},"date":"2017-01-01 01:01:01"}'
OutputFormat::BINARY_PDF

Use this to output a specific PDF binary stream on browser as a direct download. It puts as header 'Content-type: application/pdf'

$data = #myBinaryStream;
return ApiResponseFactory::buildAcceptedResponse(
            $data,
            OutputFormat::BINARY_PDF
        )->output();
//outputs a pdf file to download on browser

api-request-response-formatter's People

Contributors

javicii avatar vargaf avatar

Watchers

James Cloos avatar Dani Jimenez avatar

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.