{"id":15798435,"url":"https://github.com/codeasashu/transport","last_synced_at":"2025-11-04T02:04:24.737Z","repository":{"id":148600624,"uuid":"208225455","full_name":"codeasashu/transport","owner":"codeasashu","description":"MyOperator transport","archived":false,"fork":false,"pushed_at":"2019-09-15T08:03:16.000Z","size":50,"stargazers_count":0,"open_issues_count":0,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-06T00:41:28.651Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codeasashu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-13T08:32:09.000Z","updated_at":"2019-09-15T08:03:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"ff5d736a-be4d-49ff-88ed-4dcb3c817114","html_url":"https://github.com/codeasashu/transport","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeasashu%2Ftransport","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeasashu%2Ftransport/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeasashu%2Ftransport/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeasashu%2Ftransport/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codeasashu","download_url":"https://codeload.github.com/codeasashu/transport/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229620158,"owners_count":18099922,"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":[],"created_at":"2024-10-05T00:41:29.706Z","updated_at":"2025-11-04T02:04:24.706Z","avatar_url":"https://github.com/codeasashu.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Travis (.org) branch](https://img.shields.io/codecov/c/github/codeasashu/transport?style=flat-square)\n![Travis (.org) branch](https://img.shields.io/travis/codeasashu/transport/master)\n[![Build Status](http://jenkins.voicetree.info:62126/buildStatus/icon?job=API+Pipeline)](http://jenkins.voicetree.info:62126/buildStatus/icon?job=API+Pipeline)\n\n\n# MyOperator Transport\n\nThis is a package that uses guzzle http transport and can be used to make network requests.\nThis internally uses [Guzzle](https://github.com/guzzle/guzzle) library to make requests.\n\n## Quick Start\n\nTo make a `GET` or `POST` requests:\n\n```php\n\ninclude_once 'vendor/autoload.php';\n\nuse MyOperator\\Transport;\n\n$transport = new Transport('http://localhost/api');\n\n// Making a simple GET request\n$response = $transport-\u003eget('/users?a=b\u0026c=d');\n\n// More better GET request\n$response = $transport-\u003eget('/users', ['a' =\u003e 'b', 'c' =\u003e 'd']); \n// Equivalent to curl -XGET -H 'application/json' http://localhost/api/users?a=b\u0026c=d\n\n// To make a POST\n$response = $transport-\u003epost('/users', ['a' =\u003e 'b']); \n// Equivalent to curl -XPOST -d a=b -H 'application/json' http://localhost/api/users\n\n// Response can be directly cast to string\necho (string) $response; // {\"a\" : \"b\"}\n\n//Or json if you like\nprint_r($response-\u003ejson()); // ['a' =\u003e 'b']\n\n// Or plaintext, same like casting\necho $response-\u003etext(); //{\"a\": \"b\"}\n```\n\n## Using Responses\n\nResponses are the main part of making any responses. Since responses can be of any type (i.e. json, xml, text etc)\nthis library takes cares of automatically converting any json encodeable responses.\n\nResponses are part of `\\MyOperator\\Transport\\Response`. Hence any response have three available methods:\n\n- `json()` which returns array if response is valid json. Else the response is returned as is\n- `text()` returns the text response as is\n- `getStatus()` returns the response HTTP Status code\n\n```php\n\n//Assuming webservice `/getjson` returns {'a': 'b'}\n// and '/gettext' returns 'Simple text response'\n\n$response = $transport-\u003eget('/getjson');\n// assertTrue $response-\u003ejson() == ['a' =\u003e 'b']\n// assertTrue $response-\u003etext() == '{\"a\": \"b\"}'\n\n$response = $transport-\u003eget('/gettext');\n// assertTrue $response-\u003ejson() == 'Simple text response'\n// assertTrue $response-\u003etext() == 'Simple text response'\n// assertTrue (string) $response == 'Simple text response'\n```\n\n## Setting headers\n\nSometime, you may wish to add headers, which can be easily done using `setHeaders` method.\n\n```php\nuse MyOperator\\Transport;\n\n$transport = new Transport('http://localhost');\n\n$transport-\u003esetHeaders(['X-Auth-key' =\u003e 'xyz']);\n\n$transport-\u003epost('/login');\n```\n\n### Mocking network requests\n\nThis library aims at making writing unit tests and mocks a breeze. This library provides a fluent [Guzzle mock](http://docs.guzzlephp.org/en/stable/testing.html) api to make mocking easy.\n\nTo mock a network request, you can easily create a mock using `MyOperator\\TransportMock`. Then can you queue some responses to it.\nYou can then call your apis and the mock will replay the queues responses in order.\n\nFor instance, to mock a `200 SUCCESS` response from any api, you can do:\n\n```php\nuse \\MyOperator\\TransportMock;\n\n// Inititalising a mocker\n$transport = new TransportMock();\n\n//Creating custom response. order is createResponse($body, $headers= [], $status_code=200);\n$mockResponse = $transport-\u003ecreateResponse(json_encode(['a' =\u003e 'b']));\n\n// Creating another GuzzleHttp\\Psr7\\Response responses\n$anotherResponse = new GuzzleHttp\\Psr7\\Response(201, [], json_encode(['c' =\u003e 'd']));\n\n// Queue a valid GuzzleHttp\\Psr7\\Response\n$transport-\u003equeue($mockResponse);\n$transport-\u003equeue($anotherResponse);\n\n// Finally we can start mock\n$transport-\u003emock();\n\n// This will return first queued response, doesn't matter whatever the request is\n$response = $transport-\u003eget('/get');\n//assertTrue $response-\u003ejson() == ['a' =\u003e 'b']\n//assertTrue $response-\u003egetStatus() == 200\n\n$response = $transport-\u003epost('/somethingelse');\n//assertTrue $response-\u003ejson() == ['c' =\u003e 'd']\n//assertTrue $response-\u003egetStatus() == 201\n\n// Since at this point we have exhaused our queued response,\n// this will throw an \\OutOfBoundsException\n$response = $transport-\u003eget('/status');\n```\n\n\n## TODO\n\n- Handling public and private APIs\n- Setting basic authentication process\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeasashu%2Ftransport","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeasashu%2Ftransport","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeasashu%2Ftransport/lists"}