Пример #1
0
 public function testMultipartFiles()
 {
     $fixture = __DIR__ . '/fixtures/upload.txt';
     $data = array('foo' => 'bar', 'bar' => 'baz');
     $files = array('test' => $fixture);
     $body = Body::Multipart($data, $files);
     // echo $body;
     $this->assertEquals($body, array('foo' => 'bar', 'bar' => 'baz', 'test' => Body::File($fixture)));
 }
Пример #2
0
 /**
  * Upload a file
  * @param  string     $filename     Required parameter: Example: 
  * @return string response from the API call*/
 public function uploadFile($filename)
 {
     //check that all required arguments are provided
     if (!isset($filename)) {
         throw new \InvalidArgumentException("One or more required arguments were NULL.");
     }
     //the base uri for api requests
     $_queryBuilder = Configuration::$BASEURI;
     //prepare query string for API call
     $_queryBuilder = $_queryBuilder . '/uploads';
     //validate and preprocess url
     $_queryUrl = APIHelper::cleanUrl($_queryBuilder);
     //prepare headers
     $_headers = array('user-agent' => 'ClickSendSDK');
     //prepare parameters
     $_parameters = array("file" => Request\Body::file($filename));
     //set HTTP basic auth parameters
     Request::auth(Configuration::$username, Configuration::$key);
     //and invoke the API call request to fetch the response
     $response = Request::post($_queryUrl, $_headers, APIHelper::httpBuildQueryDevelop($_parameters));
     //Error handling using HTTP status codes
     if ($response->code == 400) {
         throw new APIException('BAD_REQUEST', 400, $response->body);
     } else {
         if ($response->code == 401) {
             throw new APIException('UNAUTHORIZED', 401, $response->body);
         } else {
             if ($response->code == 403) {
                 throw new APIException('FORBIDDEN', 403, $response->body);
             } else {
                 if ($response->code == 404) {
                     throw new APIException('NOT_FOUND', 404, $response->body);
                 } else {
                     if ($response->code == 405) {
                         throw new APIException('METHOD_NOT_FOUND', 405, $response->body);
                     } else {
                         if ($response->code == 429) {
                             throw new APIException('TOO_MANY_REQUESTS', 429, $response->body);
                         } else {
                             if ($response->code == 500) {
                                 throw new APIException('INTERNAL_SERVER_ERROR', 500, $response->body);
                             } else {
                                 if ($response->code < 200 || $response->code > 206) {
                                     //[200,206] = HTTP OK
                                     throw new APIException("HTTP Response Not OK", $response->code, $response->body);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return $response->body;
 }
Пример #3
0
 /**
  * @throws ErrorException
  * @throws LengthException
  */
 protected function post()
 {
     // get arguments
     $argv = $_SERVER['argv'];
     // remove first argument (i.e. path)
     array_shift($argv);
     if (!count($argv)) {
         // topic ID is required argument
         throw new \LengthException('Not enough arguments');
     }
     // request topic page
     $response = $this->request(static::getUrl(['showtopic' => $argv[0]]));
     $document = new \DOMDocument();
     $document->loadHTML($response);
     $xpath = new \DomXpath($document);
     // find latest post(s)
     $deleteLinks = $xpath->query('//a[contains(@href, "javascript:delete_post")]');
     // any posts?
     // todo: delete ONLY "UP" post
     if ($deleteLinks->length) {
         /** @var $lastLink \DOMElement */
         $lastLink = $deleteLinks->item($deleteLinks->length - 1);
         preg_match('/https?:[^\']+/', $lastLink->getAttribute('href'), $matches);
         // delete last UP post
         if (!empty($matches)) {
             $this->output->writeln('<comment>Deleting last UP</comment>');
             $this->request($matches[0]);
         }
     }
     $replyParams = ['Post' => 'UP'];
     $replyForms = $xpath->query('//form[contains(@name, "REPLIER")]');
     $hiddenInputs = $xpath->query('.//input[contains(@type, "hidden")]', $replyForms->item(0));
     foreach ($hiddenInputs as $input) {
         /** @var $input \DOMElement */
         $replyParams[$input->getAttribute('name')] = $input->getAttribute('value');
     }
     $this->output->writeln('<info>Posting new UP</info>');
     // and reply new UP post
     $this->request(static::getUrl(), Method::POST, Body::form($replyParams));
 }
Пример #4
0
 /**
  * `Check if booking is possible` method request requires lesser parameters as compared to `Create a booking`.In fact before creating a booking we advise to run this request first:- it will check if booking can be made without writing any data in BMG database- it will provide a correct total amount for selected services- if partner is caching products this should be used just before creating a booking - so any differences in cached prices (on partner side) and current price for total amount can be discovered- if partner discover difference in provided total amount then he can act accordingly in his user/client interface (for example - ask customer to accept final changed price)The main difference is that no booking is created in the system and some fields in response JSON object have NULL values. If the product has timeslots, the `timeslotUUID` of the product is REQUIRED in `Check a Booking` and `Create a new booking` methods. Otherwise, it can be excluded.For `booking check` request you don't have to pass customer's data or `partnerReference`.Example JSON request:        {            "productTypeUuid": "9d4b4d76-5b54-5407-b2d9-e6c021cc472e",            "pax": 2,            "children": 3,            "timeSlotUuid": "b02ae425-421f-5edf-a880-4104c0245dac",            "addons": [                {                    "uuid": "37e0f490-d31d-521b-b1f0-95c6e4e38d4c",                    "quantity": "1"                },                {                    "uuid": "a50090e3-567d-5743-9d87-e9d261aad0a9",                    "quantity": "2"                }                ],            "arrivalDate": "2015-06-07",            "usePromotion": false        }Response example:        {          "data": {              "uuid": null,              "totalAmount": "123.98",              "currencyCode": "SGD",              "currencyUuid": "cd15153e-dfd1-5039-8aa3-115bec58e86e",              "totalAmountRequestCurrency": "369.46",              "requestCurrencyCode": "MYR",              "requestCurrencyUuid": "e98aaf89-ae5a-5c11-859a-b36f2c8e13c7",              "createdAt": null,              "updatedAt": null,              "arrivalDate": "2015-06-07",              "salutation": null,              "firstName": null,              "lastName": null,              "email": null,              "phone": null,              "guests": 2,              "children": 3,              "partnerReference": null,              "confirmationEmailSentAt": null,              "confirmationEmailFiles": [],              "status": null,              "productTypeTitle": null,              "productTypeTitleTranslated": null,              "productTypeUuid": "9d4b4d76-5b54-5407-b2d9-e6c021cc472e"          }        }
  * @param  Models\CheckABookingRequest $body     Required parameter: Example: 
  * @return mixed response from the API call
  * @throws APIException Thrown if API call fails
  */
 public function createCheckABooking($body)
 {
     //the base uri for api requests
     $_queryBuilder = Configuration::$BASEURI;
     //prepare query string for API call
     $_queryBuilder = $_queryBuilder . '/v1/bookings/check';
     //validate and preprocess url
     $_queryUrl = APIHelper::cleanUrl($_queryBuilder);
     //prepare headers
     $_headers = array('user-agent' => 'BeMyGuest.SDK.v1', 'Accept' => 'application/json', 'content-type' => 'application/json; charset=utf-8', 'X-Authorization' => Configuration::$xAuthorization);
     //call on-before Http callback
     $_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl);
     if ($this->getHttpCallBack() != null) {
         $this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
     }
     //and invoke the API call request to fetch the response
     $response = Request::post($_queryUrl, $_headers, Request\Body::Json($body));
     //call on-after Http callback
     if ($this->getHttpCallBack() != null) {
         $_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
         $_httpContext = new HttpContext($_httpRequest, $_httpResponse);
         $this->getHttpCallBack()->callOnAfterRequest($_httpContext);
     }
     //Error handling using HTTP status codes
     if ($response->code == 400) {
         throw new APIException('Wrong Arguments', $_httpContext);
     } else {
         if ($response->code == 401) {
             throw new APIException('Unauthorized', $_httpContext);
         } else {
             if ($response->code == 403) {
                 throw new APIException('Forbidden', $_httpContext);
             } else {
                 if ($response->code == 404) {
                     throw new APIException('Resource Not Found', $_httpContext);
                 } else {
                     if ($response->code == 405) {
                         throw new APIException('Method Not Allowed', $_httpContext);
                     } else {
                         if ($response->code == 410) {
                             throw new APIException('Resource No Longer Available', $_httpContext);
                         } else {
                             if ($response->code < 200 || $response->code > 208) {
                                 //[200,208] = HTTP OK
                                 throw new APIException("HTTP Response Not OK", $_httpContext);
                             }
                         }
                     }
                 }
             }
         }
     }
     $mapper = $this->getJsonMapper();
     return $mapper->map($response->body, new Models\CheckABookingResponse());
 }
Пример #5
0
 public function put($uri, $data = [], $files = [])
 {
     $headers = ['Accept' => 'application/json', 'Content-Type' => empty($files) ? 'application/json' : 'multipart/form-data', 'X-IFSISTEM-TOKEN' => $this->token, 'X-IFSISTEM-DIL-ID' => $this->dilId];
     $body = empty($files) ? Request\Body::Json($data) : Request\Body::multipart($data, $files);
     return Request::put($this->uri . $uri, $headers, $body);
 }
Пример #6
0
 public function testUploadIfFilePartOfData()
 {
     $fixture = __DIR__ . '/../fixtures/upload.txt';
     $response = Request::post('http://mockbin.com/request', array('Accept' => 'application/json'), array('name' => 'Mark', 'files[owl.gif]' => Request\Body::File($fixture)));
     $this->assertEquals(200, $response->code);
     $this->assertEquals('POST', $response->body->method);
     $this->assertEquals('Mark', $response->body->postData->params->name);
     $this->assertEquals('This is a test', $response->body->postData->params->{'files[owl.gif]'});
 }