|
1 |
| -<?php |
| 1 | +<?php |
2 | 2 | /**
|
3 | 3 | * Copyright 2014 Wish.com, ContextLogic or its affiliates. All Rights Reserved.
|
4 | 4 | *
|
5 | 5 | * Licensed under the Apache License, Version 2.0 (the "License").
|
6 | 6 | * You may not use this file except in compliance with the License.
|
7 |
| - * You may obtain a copy of the License at |
8 |
| - * |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
9 | 9 | * http://www.apache.org/licenses/LICENSE-2.0
|
10 | 10 | *
|
11 | 11 | * Unless required by applicable law or agreed to in writing, software
|
|
14 | 14 | * See the License for the specific language governing permissions and
|
15 | 15 | * limitations under the License.
|
16 | 16 | */
|
| 17 | + |
17 | 18 | namespace Wish;
|
18 | 19 |
|
19 | 20 | use Wish\Exception\ConnectionException;
|
20 | 21 |
|
21 |
| -class WishRequest{ |
22 |
| - const VERSION = "v2/"; |
23 |
| - const BASE_PROD_PATH = "https://merchant.wish.com/api/"; |
24 |
| - const BASE_SANDBOX_PATH = "https://sandbox.merchant.wish.com/api/"; |
25 |
| - const BASE_STAGE_PATH = "https://merch.corp.contextlogic.com/api/"; |
26 |
| - private $session; |
27 |
| - private $method; |
28 |
| - private $path; |
29 |
| - private $params; |
| 22 | +class WishRequest |
| 23 | +{ |
| 24 | + const VERSION = "v2/"; |
| 25 | + const BASE_PROD_PATH = "https://merchant.wish.com/api/"; |
| 26 | + const BASE_SANDBOX_PATH = "https://sandbox.merchant.wish.com/api/"; |
| 27 | + const BASE_STAGE_PATH = "https://merch.corp.contextlogic.com/api/"; |
| 28 | + private $session; |
| 29 | + private $method; |
| 30 | + private $path; |
| 31 | + private $params; |
30 | 32 |
|
31 |
| - public function __construct($session,$method,$path,$params = array()) { |
32 |
| - $this->session = $session; |
33 |
| - $this->method = $method; |
34 |
| - $this->path = $path; |
35 |
| - $params['access_token'] = $session->getAccessToken(); |
36 |
| - if($session->getMerchantId())$params['merchant_id']=$session->getMerchantId(); |
37 |
| - $this->params = $params; |
38 |
| - } |
| 33 | + public function __construct($session, $method, $path, $params = array()) |
| 34 | + { |
| 35 | + $this->session = $session; |
| 36 | + $this->method = $method; |
| 37 | + $this->path = $path; |
| 38 | + $params['access_token'] = $session->getAccessToken(); |
| 39 | + if ($session->getMerchantId()) $params['merchant_id'] = $session->getMerchantId(); |
| 40 | + $this->params = $params; |
| 41 | + } |
39 | 42 |
|
40 |
| - public function getVersion(){ |
41 |
| - return static::VERSION; |
42 |
| - } |
| 43 | + public function getVersion() |
| 44 | + { |
| 45 | + return static::VERSION; |
| 46 | + } |
43 | 47 |
|
44 |
| - public function getRequestURL(){ |
45 |
| - switch($this->session->getSessionType()){ |
46 |
| - case WishSession::SESSION_PROD: return static::BASE_PROD_PATH; |
47 |
| - case WishSession::SESSION_SANDBOX: return static::BASE_SANDBOX_PATH; |
48 |
| - case WishSession::SESSION_STAGE: return static::BASE_STAGE_PATH; |
49 |
| - default: throw new InvalidArgumentException("Invalid session type"); |
| 48 | + public function getRequestURL() |
| 49 | + { |
| 50 | + switch ($this->session->getSessionType()) { |
| 51 | + case WishSession::SESSION_PROD: |
| 52 | + return static::BASE_PROD_PATH; |
| 53 | + case WishSession::SESSION_SANDBOX: |
| 54 | + return static::BASE_SANDBOX_PATH; |
| 55 | + case WishSession::SESSION_STAGE: |
| 56 | + return static::BASE_STAGE_PATH; |
| 57 | + default: |
| 58 | + throw new InvalidArgumentException("Invalid session type"); |
| 59 | + } |
50 | 60 | }
|
51 |
| - } |
52 | 61 |
|
53 |
| - public function execute(){ |
54 |
| - $url = $this->getRequestURL().$this->getVersion().$this->path; |
55 |
| - $curl = curl_init(); |
56 |
| - $params = $this->params; |
| 62 | + public function execute() |
| 63 | + { |
| 64 | + $url = $this->getRequestURL(); |
57 | 65 |
|
58 |
| - $options = array( |
59 |
| - CURLOPT_CONNECTTIMEOUT => 10, |
60 |
| - CURLOPT_RETURNTRANSFER => true, |
61 |
| - CURLOPT_ENCODING => '', |
62 |
| - CURLOPT_USERAGENT => 'wish-php-sdk', |
63 |
| - CURLOPT_HEADER => 'true' |
64 |
| - ); |
| 66 | + // Use v3 for auth, v2 for all other calls, because Wish is INCREDIBLY asinie |
| 67 | + if ($this->path == 'oauth/access_token' || $this->path == 'oauth/refresh_token') { |
| 68 | + $url .= 'v3/'; |
| 69 | + } else { |
| 70 | + $url .= 'v2/'; |
| 71 | + } |
65 | 72 |
|
66 |
| - if($this->method === "GET"){ |
67 |
| - $url = $url."?".http_build_query($params); |
68 |
| - }else { |
69 |
| - $options[CURLOPT_POSTFIELDS] = $params; |
70 |
| - } |
71 |
| - $options[CURLOPT_URL] = $url; |
72 |
| - curl_setopt_array($curl, $options); |
| 73 | + $url .= $this->path; |
| 74 | + $curl = curl_init(); |
| 75 | + $params = $this->params; |
73 | 76 |
|
74 |
| - $result = curl_exec($curl); |
75 |
| - $error = curl_errno($curl); |
76 |
| - |
77 |
| - $error_message = curl_error($curl); |
| 77 | + $options = array( |
| 78 | + CURLOPT_CONNECTTIMEOUT => 10, |
| 79 | + CURLOPT_RETURNTRANSFER => true, |
| 80 | + CURLOPT_ENCODING => '', |
| 81 | + CURLOPT_USERAGENT => 'wish-php-sdk', |
| 82 | + CURLOPT_HEADER => 'true' |
| 83 | + ); |
78 | 84 |
|
79 |
| - if($error){ |
80 |
| - throw new ConnectionException($error_message); |
81 |
| - } |
82 |
| - $httpStatus = curl_getinfo($curl,CURLINFO_HTTP_CODE); |
83 |
| - $headerSize = curl_getinfo($curl,CURLINFO_HEADER_SIZE); |
84 |
| - curl_close($curl); |
85 |
| - |
86 |
| - $decoded_result = json_decode($result); |
87 |
| - if($decoded_result === null){ |
88 |
| - $out = array(); |
89 |
| - parse_str($result,$out); |
90 |
| - return new WishResponse($this,$out,$result); |
91 |
| - } |
92 |
| - return new WishResponse($this,$decoded_result,$result); |
93 |
| - } |
| 85 | + if ($this->method === "GET") { |
| 86 | + $url = $url . "?" . http_build_query($params); |
| 87 | + } else { |
| 88 | + $options[CURLOPT_POSTFIELDS] = $params; |
| 89 | + } |
| 90 | + $options[CURLOPT_URL] = $url; |
| 91 | + curl_setopt_array($curl, $options); |
94 | 92 |
|
| 93 | + $result = curl_exec($curl); |
| 94 | + $error = curl_errno($curl); |
95 | 95 |
|
| 96 | + $error_message = curl_error($curl); |
96 | 97 |
|
| 98 | + if ($error) { |
| 99 | + throw new ConnectionException($error_message); |
| 100 | + } |
| 101 | + $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
| 102 | + $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
| 103 | + curl_close($curl); |
97 | 104 |
|
| 105 | + $decoded_result = json_decode($result); |
| 106 | + if ($decoded_result === null) { |
| 107 | + $out = array(); |
| 108 | + parse_str($result, $out); |
| 109 | + return new WishResponse($this, $out, $result); |
| 110 | + } |
| 111 | + return new WishResponse($this, $decoded_result, $result); |
| 112 | + } |
98 | 113 | }
|
0 commit comments