{"id":21376066,"url":"https://github.com/lessmore92/api-consumer","last_synced_at":"2025-04-15T15:56:24.967Z","repository":{"id":57014143,"uuid":"237826545","full_name":"lessmore92/api-consumer","owner":"lessmore92","description":"Build REST API consumer (client) easier than ever","archived":false,"fork":false,"pushed_at":"2020-02-13T12:22:48.000Z","size":31,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-15T15:56:15.235Z","etag":null,"topics":["api-client","api-consumer","api-rest","client","consumer","json","json-api","json-parser","jsonapi","php","receive-data","rest","rest-api","rest-client","restful-api"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lessmore92.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-02T19:46:10.000Z","updated_at":"2024-04-25T09:56:00.000Z","dependencies_parsed_at":"2022-08-21T14:50:35.741Z","dependency_job_id":null,"html_url":"https://github.com/lessmore92/api-consumer","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lessmore92%2Fapi-consumer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lessmore92%2Fapi-consumer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lessmore92%2Fapi-consumer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lessmore92%2Fapi-consumer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lessmore92","download_url":"https://codeload.github.com/lessmore92/api-consumer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249105471,"owners_count":21213534,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api-client","api-consumer","api-rest","client","consumer","json","json-api","json-parser","jsonapi","php","receive-data","rest","rest-api","rest-client","restful-api"],"created_at":"2024-11-22T09:13:39.190Z","updated_at":"2025-04-15T15:56:24.951Z","avatar_url":"https://github.com/lessmore92.png","language":"PHP","readme":"# api-consumer\nBuild REST API consumer (client) easier than ever\n\n### Installing\n\nEasily install it through Composer:\n\n```\ncomposer require lessmore92/api-consumer\n```\n\n### Support\n\n```\nPHP \u003e=5.5\n```\n\n## Usage\n\nEasily extends your class from ```ApiConsumer``` and impalements ```ConfigApi``` method, and builds your awesome API Client.\n\n## Example:\n#### Below is the minimum requirement to start building API client: **_ConfigApi_**\n```\nuse Lessmore92\\ApiConsumer\\ApiConsumer;\nuse Lessmore92\\ApiConsumer\\Builders\\ApiBuilder;\n\nclass MyApi extends ApiConsumer\n{\n    /**\n     * @return ApiBuilder\n     */\n    protected function ConfigApi()\n    {\n        $api = new ApiBuilder();\n        $api-\u003esetHeaderApiKey('API-TOKEN','X-API-Key');\n        $api-\u003esetBaseUrl('https://MY-API-BASE-URL.COM/');\n        return $api;\n    }\n}\n```\nIf the api key needs to be placed in the query string use  `setQueryApiKey` instead of `setHeaderApiKey`\nfor example `$api-\u003esetQueryApiKey('API-TOKEN','TOKEN');`\n___\n\n### The Magic of `$this-\u003eRequest()`\nBy inheriting ```ApiConsumer``` your class will be able to utilize ```$this-\u003eRequest()``` method, which supports chaining.\nWith ```$this-\u003eRequest()``` you will be able to access all the features and functionalities to make your request.\n\n\n#### Define your first method to receive data from api: **_Simple GET Request_**\nTo specify an endpoint to be called, you must use `-\u003eEndpoint()` method. After that by chaining `-\u003eGet()` method at the end, REQUEST METHOD is specified as `GET`.\n\n```\nuse Lessmore92\\ApiConsumer\\ApiConsumer;\nuse Lessmore92\\ApiConsumer\\Builders\\ApiBuilder;\n\nclass MyApi extends ApiConsumer\n{\n    /**\n     * @return ApiBuilder\n     */\n    protected function ConfigApi()\n    {\n        $api = new ApiBuilder();\n        $api-\u003esetHeaderApiKey('API-TOKEN','X-API-Key');\n        $api-\u003esetBaseUrl('https://MY-API-BASE-URL.COM/');\n        return $api;\n    }\n\n    public function Users()\n    {\n        $users = $this-\u003eRequest()\n                      -\u003eEndpoint('users')\n                      -\u003eGet()\n        ;\n\n        return $users-\u003ebody;\n    }\n}\n```\nIn the above example we defined a method to `GET` `Users` list from server.\n\n_By calling `Users()` method, in fact we are getting `https://MY-API-BASE-URL.COM/users`_\n___\n\n#### Make another request: Add **_Query String_**\n\nTo pass data in query string (e.g to search, order or filter) you can use `-\u003eAddQueryString()` method.\n\n```\npublic function SearchUsers($search)\n{\n    $users = $this-\u003eRequest()\n                  -\u003eEndpoint('users')\n                  -\u003eAddQueryString('search', $search)\n                  -\u003eGet()\n    ;\n\n    return $users-\u003ejson_body;\n}\n```\nIn the above example we defined a method to search in users.\n\n_By calling `SearchUsers('alex')` method, in fact we are getting `https://MY-API-BASE-URL.COM/users?search=alex`_\n\n---\n\n#### Make another request: Get result in **_json_** format\nTo receive data as `json`, you must use `-\u003eAcceptJson()` method.\n\n```\npublic function SearchUsers($search)\n{\n    $users = $this-\u003eRequest()\n                  -\u003eEndpoint('users')\n                  -\u003eAcceptJson()\n                  -\u003eGet()\n    ;\n\n    return $users-\u003ejson_body;\n}\n```\n\nAs you can see in the code above, by chaining `-\u003eAcceptJson()` in request we are telling to api server that we accept `json`, then in the `return` line we are returning a json formatted search result.\n\n_For `json` data format, your api server must be able to provide json formatted response and support HEADER `'accept : application/json'`_\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flessmore92%2Fapi-consumer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flessmore92%2Fapi-consumer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flessmore92%2Fapi-consumer/lists"}