├── src ├── .gitkeep ├── Action.php ├── Response.php └── Client.php ├── LICENSE.md ├── README.md ├── composer.json └── .gitignore /src/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # License 2 | 3 | Copyright (C) Infomaniak Network SA - All Rights Reserved 4 | 5 | Unauthorized copying or distribution of this program or part of it, 6 | via any medium, is strictly prohibited. 7 | 8 | Proprietary and confidential. 9 | -------------------------------------------------------------------------------- /src/Action.php: -------------------------------------------------------------------------------- 1 | =5.6.0", 20 | "guzzlehttp/guzzle": "~7.0" 21 | }, 22 | "require-dev": { 23 | "phpunit/phpunit": "^5.5" 24 | }, 25 | "autoload": { 26 | "psr-4": { 27 | "Infomaniak\\ClientApiNewsletter\\": "src/" 28 | } 29 | }, 30 | "autoload-dev": { 31 | "psr-4": { 32 | "Infomaniak\\ClientApiNewsletter\\Tests\\": "tests/" 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Response.php: -------------------------------------------------------------------------------- 1 | raw = $response; 25 | $this->status = $response->getStatusCode(); 26 | $this->body = json_decode($response->getBody(), true); 27 | $this->success = floor($this->status / 100) == 2 ? true : false; 28 | } 29 | } 30 | 31 | /** 32 | * Get status code 33 | * @return int 34 | */ 35 | public function status() 36 | { 37 | return $this->status; 38 | } 39 | 40 | /** 41 | * Get response success 42 | * @return bool 43 | */ 44 | public function success() 45 | { 46 | return $this->success; 47 | } 48 | 49 | /** 50 | * Get response body 51 | * @return mixed 52 | */ 53 | public function body() 54 | { 55 | return $this->body; 56 | } 57 | 58 | /** 59 | * Get response datas 60 | * @return mixed 61 | */ 62 | public function datas() 63 | { 64 | if (isset($this->body['data'])) 65 | { 66 | return $this->body['data']; 67 | } 68 | 69 | return $this->body; 70 | } 71 | 72 | /** 73 | * Get response errors 74 | * @return bool 75 | */ 76 | public function errors() 77 | { 78 | if (isset($this->body['error'])) 79 | { 80 | $json_error = current($this->body['error']); 81 | return json_decode($json_error, true); 82 | } 83 | 84 | return false; 85 | } 86 | 87 | /** 88 | * Get raw content 89 | * @return string 90 | */ 91 | public function rawContent() 92 | { 93 | return $this->raw->getBody()->getContents(); 94 | } 95 | 96 | /** 97 | * Get reason phrase 98 | * @return string 99 | */ 100 | public function reason() 101 | { 102 | return $this->raw->getReasonPhrase(); 103 | } 104 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | /vendor/ 3 | 4 | # ---------------------------------------------------------------------------- 5 | 6 | # Created by .ignore support plugin (hsz.mobi) 7 | 8 | ### JetBrains template 9 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 10 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 11 | 12 | # User-specific stuff: 13 | .idea 14 | 15 | ## File-based project format: 16 | *.iws 17 | 18 | ## Plugin-specific files: 19 | 20 | # IntelliJ 21 | /out/ 22 | 23 | # mpeltonen/sbt-idea plugin 24 | .idea_modules/ 25 | 26 | # JIRA plugin 27 | atlassian-ide-plugin.xml 28 | 29 | # Crashlytics plugin (for Android Studio and IntelliJ) 30 | com_crashlytics_export_strings.xml 31 | crashlytics.properties 32 | crashlytics-build.properties 33 | fabric.properties 34 | 35 | ### SublimeText template 36 | # cache files for sublime text 37 | *.tmlanguage.cache 38 | *.tmPreferences.cache 39 | *.stTheme.cache 40 | 41 | # workspace files are user-specific 42 | *.sublime-workspace 43 | 44 | # project files should be checked into the repository, unless a significant 45 | # proportion of contributors will probably not be using SublimeText 46 | # *.sublime-project 47 | 48 | # sftp configuration file 49 | sftp-config.json 50 | 51 | # Package control specific files 52 | Package Control.last-run 53 | Package Control.ca-list 54 | Package Control.ca-bundle 55 | Package Control.system-ca-bundle 56 | Package Control.cache/ 57 | Package Control.ca-certs/ 58 | bh_unicode_properties.cache 59 | 60 | # Sublime-github package stores a github token in this file 61 | # https://packagecontrol.io/packages/sublime-github 62 | GitHub.sublime-settings 63 | 64 | ### Vim template 65 | # swap 66 | [._]*.s[a-w][a-z] 67 | [._]s[a-w][a-z] 68 | # session 69 | Session.vim 70 | # temporary 71 | .netrwhist 72 | # auto-generated tag files 73 | tags 74 | 75 | ### Eclipse template 76 | .metadata 77 | tmp/ 78 | *.tmp 79 | *.bak 80 | *.swp 81 | *~.nib 82 | local.properties 83 | .settings/ 84 | .loadpath 85 | .recommenders 86 | 87 | # Eclipse Core 88 | .project 89 | 90 | # External tool builders 91 | .externalToolBuilders/ 92 | 93 | # Locally stored "Eclipse launch configurations" 94 | *.launch 95 | 96 | # PyDev specific (Python IDE for Eclipse) 97 | *.pydevproject 98 | 99 | # CDT-specific (C/C++ Development Tooling) 100 | .cproject 101 | 102 | # JDT-specific (Eclipse Java Development Tools) 103 | .classpath 104 | 105 | # Java annotation processor (APT) 106 | .factorypath 107 | 108 | # PDT-specific (PHP Development Tools) 109 | .buildpath 110 | 111 | # sbteclipse plugin 112 | .target 113 | 114 | # Tern plugin 115 | .tern-project 116 | 117 | # TeXlipse plugin 118 | .texlipse 119 | 120 | # STS (Spring Tool Suite) 121 | .springBeans 122 | 123 | # Code Recommenders 124 | .recommenders/ 125 | 126 | ### Windows template 127 | # Windows image file caches 128 | Thumbs.db 129 | ehthumbs.db 130 | 131 | # Folder config file 132 | Desktop.ini 133 | 134 | # Recycle Bin used on file shares 135 | $RECYCLE.BIN/ 136 | 137 | # Windows Installer files 138 | *.cab 139 | *.msi 140 | *.msm 141 | *.msp 142 | 143 | # Windows shortcuts 144 | *.lnk 145 | 146 | ### Linux template 147 | *~ 148 | 149 | # temporary files which can be created if a process still has a handle open of a deleted file 150 | .fuse_hidden* 151 | 152 | # KDE directory preferences 153 | .directory 154 | 155 | # Linux trash folder which might appear on any partition or disk 156 | .Trash-* 157 | 158 | ### OSX template 159 | 160 | *.DS_Store 161 | .AppleDouble 162 | .LSOverride 163 | 164 | # Thumbnails 165 | ._* 166 | 167 | # Files that might appear in the root of a volume 168 | .DocumentRevisions-V100 169 | .fseventsd 170 | .Spotlight-V100 171 | .TemporaryItems 172 | .Trashes 173 | .VolumeIcon.icns 174 | .com.apple.timemachine.donotpresent 175 | 176 | # Directories potentially created on remote AFP share 177 | .AppleDB 178 | .AppleDesktop 179 | Network Trash Folder 180 | Temporary Items 181 | .apdisk 182 | -------------------------------------------------------------------------------- /src/Client.php: -------------------------------------------------------------------------------- 1 | client = new \GuzzleHttp\Client([ 31 | 'base_uri' => $this->base_uri, 32 | RequestOptions::HEADERS => [ 33 | 'User-Agent' => 'PHP wrapper for API newsletter' 34 | ], 35 | RequestOptions::AUTH => [$client_api, $client_secret], 36 | ]); 37 | } 38 | 39 | /** 40 | * Perform request with curl. 41 | * @param $method 42 | * @param $resource 43 | * @param array $args 44 | * @return Response 45 | */ 46 | private function _request($method, $resource, $args=[]) 47 | { 48 | $uri = $this->_buildURL($resource, $args); 49 | 50 | $options = []; 51 | if (array_key_exists('params', $args)) { 52 | $g = ($method == 'GET') ? 'query' : 'json'; 53 | $options = [$g => $args['params']]; 54 | } 55 | 56 | try { 57 | $response = $this->client->request($method, $uri, $options); 58 | } 59 | catch (ClientException $e) { 60 | $response = $e->getResponse(); 61 | } 62 | catch (ServerException $e) { 63 | $response = $e->getResponse(); 64 | } 65 | 66 | return new Response($response); 67 | } 68 | 69 | /** 70 | * Build URL with parameters. 71 | * @param $resource 72 | * @param array $args 73 | * @return mixed 74 | */ 75 | private function _buildURL($resource, $args=[]) 76 | { 77 | $args = array_merge( 78 | [ 79 | 'id' => '', 80 | 'action' => '', 81 | 'action_id' => '', 82 | ], 83 | array_change_key_case($args) 84 | ); 85 | 86 | return join( 87 | '/', 88 | array_filter( 89 | array( 90 | $resource, $args['id'], $args['action'], $args['action_id'] 91 | ) 92 | ) 93 | ); 94 | } 95 | 96 | /** 97 | * Perform a GET request to server. 98 | * @param $resource 99 | * @param array $args 100 | * @return Response 101 | */ 102 | public function get($resource, $args=[]) 103 | { 104 | return $this->_request('GET', $resource, $args); 105 | } 106 | 107 | /** 108 | * Perform a POST request to server. 109 | * @param $resource 110 | * @param array $args 111 | * @return Response 112 | */ 113 | public function post($resource, $args=[]) 114 | { 115 | return $this->_request('POST', $resource, $args); 116 | } 117 | 118 | /** 119 | * Perform a PUT request to server. 120 | * @param $resource 121 | * @param array $args 122 | * @return Response 123 | */ 124 | public function put($resource, $args=[]) 125 | { 126 | return $this->_request('PUT', $resource, $args); 127 | } 128 | 129 | /** 130 | * Perform a DELETE request to server. 131 | * @param $resource 132 | * @param array $args 133 | * @return Response 134 | */ 135 | public function delete($resource, $args=[]) 136 | { 137 | return $this->_request('DELETE', $resource, $args); 138 | } 139 | 140 | /** 141 | * Perform a PING request to server. 142 | * @return Response 143 | */ 144 | public function ping() 145 | { 146 | return $this->get('ping'); 147 | } 148 | } 149 | --------------------------------------------------------------------------------