33
33
34
34
class DeeplClient implements DeeplClientInterface
35
35
{
36
+ private const DEEPL_PAID_BASE_URI = 'https://api.deepl.com ' ;
37
+ private const DEEPL_FREE_BASE_URI = 'https://api-free.deepl.com ' ;
38
+
39
+ private string $ apiKey ;
36
40
private DeeplRequestFactoryInterface $ deeplRequestFactory ;
37
41
38
42
private ClientInterface $ httpClient ;
39
43
40
44
private RequestFactoryInterface $ requestFactory ;
41
45
42
46
public function __construct (
47
+ string $ apiKey ,
43
48
DeeplRequestFactoryInterface $ deeplRequestFactory ,
44
49
ClientInterface $ httpClient ,
45
- RequestFactoryInterface $ requestFactory
50
+ RequestFactoryInterface $ requestFactory,
46
51
) {
52
+ $ this ->apiKey = $ apiKey ;
47
53
$ this ->deeplRequestFactory = $ deeplRequestFactory ;
48
54
$ this ->httpClient = $ httpClient ;
49
55
$ this ->requestFactory = $ requestFactory ;
50
56
}
51
57
52
58
/**
53
- * Return Usage of API- Key
59
+ * Return Usage of API-Key
54
60
* Possible Return:.
55
61
*
56
62
* Usage
@@ -62,7 +68,7 @@ public function __construct(
62
68
public function getUsage (): ResponseModelInterface
63
69
{
64
70
return (new Usage ())->hydrate (
65
- $ this ->executeRequest ($ this ->deeplRequestFactory ->createDeeplUsageRequestHandler ())
71
+ $ this ->executeRequest ($ this ->deeplRequestFactory ->createDeeplUsageRequestHandler ()),
66
72
);
67
73
}
68
74
@@ -79,7 +85,7 @@ public function getUsage(): ResponseModelInterface
79
85
public function getTranslation (TranslationConfigInterface $ translation ): ResponseModelInterface
80
86
{
81
87
return (new Translation ())->hydrate ($ this ->executeRequest (
82
- $ this ->deeplRequestFactory ->createDeeplTranslationRequestHandler ($ translation )
88
+ $ this ->deeplRequestFactory ->createDeeplTranslationRequestHandler ($ translation ),
83
89
));
84
90
}
85
91
@@ -98,38 +104,38 @@ public function translate(string $text, string $target_language): ResponseModelI
98
104
public function translateFile (FileTranslationConfigInterface $ fileTranslation ): ResponseModelInterface
99
105
{
100
106
return (new FileSubmission ())->hydrate ($ this ->executeRequest (
101
- $ this ->deeplRequestFactory ->createDeeplFileSubmissionRequestHandler ($ fileTranslation )
107
+ $ this ->deeplRequestFactory ->createDeeplFileSubmissionRequestHandler ($ fileTranslation ),
102
108
));
103
109
}
104
110
105
111
public function translateBatch (array $ text , string $ targetLanguage ): ResponseModelInterface
106
112
{
107
113
return (new BatchTranslation ())->hydrate ($ this ->executeRequest (
108
114
$ this ->deeplRequestFactory ->createDeeplBatchTranslationRequestHandler (
109
- new BatchTranslationConfig ($ text , $ targetLanguage )
115
+ new BatchTranslationConfig ($ text , $ targetLanguage ),
110
116
)
111
117
));
112
118
}
113
119
114
120
public function getFileTranslationStatus (FileSubmissionInterface $ fileSubmission ): ResponseModelInterface
115
121
{
116
122
return (new FileTranslationStatus ())->hydrate ($ this ->executeRequest (
117
- $ this ->deeplRequestFactory ->createDeeplFileTranslationStatusRequestHandler ($ fileSubmission )
123
+ $ this ->deeplRequestFactory ->createDeeplFileTranslationStatusRequestHandler ($ fileSubmission ),
118
124
));
119
125
}
120
126
121
127
public function getFileTranslation (FileSubmissionInterface $ fileSubmission ): ResponseModelInterface
122
128
{
123
129
return (new FileTranslation ())->hydrate ($ this ->executeRequest (
124
- $ this ->deeplRequestFactory ->createDeeplFileTranslationRequestHandler ($ fileSubmission )
130
+ $ this ->deeplRequestFactory ->createDeeplFileTranslationRequestHandler ($ fileSubmission ),
125
131
));
126
132
}
127
133
128
134
public function getSupportedLanguages (): ResponseModelInterface
129
135
{
130
136
return (new SupportedLanguages ())->hydrate (
131
137
$ this ->executeRequest (
132
- $ this ->deeplRequestFactory ->createDeeplSupportedLanguageRetrievalRequestHandler ()
138
+ $ this ->deeplRequestFactory ->createDeeplSupportedLanguageRetrievalRequestHandler (),
133
139
)
134
140
);
135
141
}
@@ -138,7 +144,7 @@ public function getGlossariesSupportedLanguagesPairs(): ResponseModelInterface
138
144
{
139
145
return (new GlossariesSupportedLanguagesPairs ())->hydrate (
140
146
$ this ->executeRequest (
141
- $ this ->deeplRequestFactory ->createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler ()
147
+ $ this ->deeplRequestFactory ->createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler (),
142
148
)
143
149
);
144
150
}
@@ -147,7 +153,7 @@ public function getGlossariesList(): ResponseModelInterface
147
153
{
148
154
return (new Glossaries ())->hydrate (
149
155
$ this ->executeRequest (
150
- $ this ->deeplRequestFactory ->createDeeplGlossariesListRetrievalRequestHandler ()
156
+ $ this ->deeplRequestFactory ->createDeeplGlossariesListRetrievalRequestHandler (),
151
157
)
152
158
);
153
159
}
@@ -194,20 +200,23 @@ private function executeRequest(DeeplRequestHandlerInterface $requestHandler): s
194
200
$ request = $ this ->requestFactory
195
201
->createRequest (
196
202
$ requestHandler ->getMethod (),
197
- sprintf ('%s%s ' , $ this ->deeplRequestFactory ->getDeeplBaseUri (), $ requestHandler ->getPath ())
203
+ sprintf ('%s%s ' , $ this ->getDeeplBaseUri (), $ requestHandler ->getPath ()),
204
+ )
205
+ ->withHeader (
206
+ 'Authorization ' ,
207
+ $ this ->getAuthHeader (),
198
208
)
199
209
->withHeader (
200
210
'Content-Type ' ,
201
- $ requestHandler ->getContentType ()
211
+ $ requestHandler ->getContentType (),
202
212
)
203
- ->withBody ($ requestHandler ->getBody ());
204
-
205
- if ($ requestHandler ->getAuthHeader () !== null ) {
206
- $ request = $ request ->withHeader ('Authorization ' , $ requestHandler ->getAuthHeader ());
207
- }
213
+ ->withBody (
214
+ $ requestHandler ->getBody (),
215
+ );
208
216
209
- if ($ requestHandler ->getAcceptHeader () !== null ) {
210
- $ request = $ request ->withHeader ('Accept ' , $ requestHandler ->getAcceptHeader ());
217
+ $ acceptHeader = $ requestHandler ->getAcceptHeader ();
218
+ if ($ acceptHeader !== null ) {
219
+ $ request = $ request ->withHeader ('Accept ' , $ acceptHeader );
211
220
}
212
221
213
222
try {
@@ -216,7 +225,7 @@ private function executeRequest(DeeplRequestHandlerInterface $requestHandler): s
216
225
throw new RequestException (
217
226
$ exception ->getMessage (),
218
227
$ exception ->getCode (),
219
- $ exception
228
+ $ exception,
220
229
);
221
230
}
222
231
@@ -253,4 +262,18 @@ private function executeRequest(DeeplRequestHandlerInterface $requestHandler): s
253
262
/** @var stdClass $result */
254
263
return $ result ;
255
264
}
265
+
266
+ private function getAuthHeader (): string
267
+ {
268
+ return sprintf ('DeepL-Auth-Key %s ' , $ this ->apiKey );
269
+ }
270
+
271
+ private function getDeeplBaseUri (): string
272
+ {
273
+ if (str_contains ($ this ->apiKey , ':fx ' )) {
274
+ return self ::DEEPL_FREE_BASE_URI ;
275
+ }
276
+
277
+ return self ::DEEPL_PAID_BASE_URI ;
278
+ }
256
279
}
0 commit comments