mirror of
				https://github.com/brunofontes/Memsource-API.git
				synced 2025-10-31 12:11:06 -03:00 
			
		
		
		
	BilingualFile: added uploadBilingualFile
This commit is contained in:
		
							parent
							
								
									5d420d26f6
								
							
						
					
					
						commit
						404e128cfe
					
				| @ -9,7 +9,7 @@ namespace BrunoFontes\Memsource; | ||||
| 
 | ||||
| class BilingualFile extends \BrunoFontes\Memsource\BaseApi | ||||
| { | ||||
|     private $_url = '/bilingualFiles'; | ||||
|     private $_url = '/api2/v1/bilingualFiles'; | ||||
|      | ||||
|     /** | ||||
|      * Download one or more bilingual files | ||||
| @ -57,14 +57,32 @@ class BilingualFile extends \BrunoFontes\Memsource\BaseApi | ||||
|         return ['jobs' => $convertedArray]; | ||||
|     } | ||||
| 
 | ||||
|     private function _saveIntoFile(string $filename, string $filecontent) | ||||
|     private function _saveIntoFile(string $filename, string $filecontent): void | ||||
|     { | ||||
|         try { | ||||
|             $f = fopen($filename, 'w+'); | ||||
|             fwrite($f, $filecontent); | ||||
|             fclose($f); | ||||
|         } catch (\Exception $e) { | ||||
|             throw new Exception("File could not be saved: " . $e->error, 1); | ||||
|             throw new \Exception("File could not be saved: {$e->error}", 1); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Upload a bilingual file to Memsource | ||||
|      * | ||||
|      * @param string $filename The filename to be uploaded | ||||
|      * @param array  $params   Any API (HTTP GET) parameters as ['key' => 'value'] format | ||||
|      *  | ||||
|      * @return string The http request responde from API in json format | ||||
|      */ | ||||
|     public function uploadBilingualFile(string $filename, array $params): string | ||||
|     { | ||||
|         $urlParams = http_build_query($params); | ||||
|         $fileContent = file_get_contents($filename); | ||||
|         if ($fileContent === false) { | ||||
|             throw new \Exception('File for upload inexistent or invalid', 1); | ||||
|         } | ||||
|         return $this->fetchApi->fetch('put', $this->_url . "?{$urlParams}", [$fileContent]); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -42,9 +42,12 @@ class FetchApi | ||||
|             $parameters = http_build_query($parameters); | ||||
|             $url = $url . ($parameters ? '?'.$parameters : ''); | ||||
|             break; | ||||
|         case 'put': | ||||
|             $setopt = $this->getPutParam()+$this->getPostParam(implode("", $parameters)); | ||||
|             break; | ||||
|         case 'post': | ||||
|             $parameters = http_build_query($parameters); | ||||
|             $setopt = $this->getPostParam($parameters); | ||||
|             $setopt = $setopt + $this->getPostParam($parameters); | ||||
|             break; | ||||
|         case 'jsonPost': | ||||
|             $setopt = $this->getJsonPostParam($parameters); | ||||
| @ -67,7 +70,7 @@ class FetchApi | ||||
|         ]; | ||||
|     } | ||||
| 
 | ||||
|     private function getJsonPostParam(array $postFields) | ||||
|     private function getJsonPostParam(array $postFields = []) | ||||
|     { | ||||
|         return [ | ||||
|             CURLOPT_HTTPHEADER => ['Content-type: application/json'], | ||||
| @ -76,7 +79,7 @@ class FetchApi | ||||
|         ]; | ||||
|     } | ||||
| 
 | ||||
|     private function getPostParam(string $postFields) | ||||
|     private function getPostParam(string $postFields = '') | ||||
|     { | ||||
|         return [ | ||||
|             CURLOPT_POST => true, | ||||
| @ -84,6 +87,14 @@ class FetchApi | ||||
|         ]; | ||||
|     } | ||||
| 
 | ||||
|     private function getPutParam() | ||||
|     { | ||||
|         return [ | ||||
|             CURLOPT_HTTPHEADER => ['Content-type: application/octet-stream'], | ||||
|             CURLOPT_CUSTOMREQUEST => "PUT" | ||||
|         ]; | ||||
|     } | ||||
| 
 | ||||
|     protected function curl(string $url, array $curl_extra_setopt = []) | ||||
|     { | ||||
|         if (empty($url)) { | ||||
| @ -99,9 +110,9 @@ class FetchApi | ||||
|             CURLOPT_TIMEOUT => 500, | ||||
|             CURLOPT_FOLLOWLOCATION => true, | ||||
|             CURLOPT_RETURNTRANSFER => true | ||||
|         ]; | ||||
|         ] + $curl_extra_setopt; | ||||
|         $curl = curl_init(); | ||||
|         curl_setopt_array($curl, ($curl_setopt + $curl_extra_setopt)); | ||||
|         curl_setopt_array($curl, $curl_setopt); | ||||
|         $response = curl_exec($curl); | ||||
|         curl_close($curl); | ||||
|         return $response; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user