forked from finos/legend-engine-ide-lsp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract out Legend Engine server client into a service
- Loading branch information
1 parent
2d3fc72
commit d0e4405
Showing
3 changed files
with
140 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
.../main/java/org/finos/legend/engine/ide/lsp/extension/DefaultLegendEngineServerClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2023 Goldman Sachs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.finos.legend.engine.ide.lsp.extension; | ||
|
||
import org.apache.http.HttpEntity; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.entity.StringEntity; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.apache.http.util.EntityUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.UncheckedIOException; | ||
|
||
class DefaultLegendEngineServerClient implements LegendEngineServerClient | ||
{ | ||
private final String engineServerUrl = System.getProperty("legend.engine.server.url"); | ||
|
||
@Override | ||
public boolean isServerConfigured() | ||
{ | ||
return this.engineServerUrl != null; | ||
} | ||
|
||
@Override | ||
public InputStream post(String path, String payload, String contentType) | ||
{ | ||
return post(path, new StringEntity(payload, contentType)); | ||
} | ||
|
||
private InputStream post(String path, HttpEntity payload) | ||
{ | ||
if (!isServerConfigured()) | ||
{ | ||
throw new IllegalStateException("Engine server is not configured"); | ||
} | ||
String url = buildUrl(path); | ||
try | ||
{ | ||
HttpPost httpPost = new HttpPost(url); | ||
httpPost.setEntity(payload); | ||
|
||
try (CloseableHttpClient client = newHttpClient(); | ||
CloseableHttpResponse response = client.execute(httpPost)) | ||
{ | ||
HttpEntity entity = response.getEntity(); | ||
if ((response.getStatusLine().getStatusCode() / 100) != 2) | ||
{ | ||
String responseString = EntityUtils.toString(entity); | ||
throw new RuntimeException("Engine server responded to " + url + " with status: " + response.getStatusLine().getStatusCode() + "\nresponse: " + responseString); | ||
} | ||
return entity.getContent(); | ||
} | ||
} | ||
catch (IOException e) | ||
{ | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
private String buildUrl(String path) | ||
{ | ||
return this.engineServerUrl + | ||
(this.engineServerUrl.endsWith("/") ? | ||
(path.startsWith("/") ? path.substring(1) : path) : | ||
(path.startsWith("/") ? path : ("/" + path))); | ||
} | ||
|
||
private CloseableHttpClient newHttpClient() | ||
{ | ||
// TODO configure the client properly | ||
return HttpClientBuilder.create().build(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...ons/src/main/java/org/finos/legend/engine/ide/lsp/extension/LegendEngineServerClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2023 Goldman Sachs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.finos.legend.engine.ide.lsp.extension; | ||
|
||
import java.io.InputStream; | ||
|
||
public interface LegendEngineServerClient | ||
{ | ||
default boolean isServerConfigured() | ||
{ | ||
return false; | ||
} | ||
|
||
default InputStream post(String path, String payload) | ||
{ | ||
return post(path, payload, "application/json"); | ||
} | ||
|
||
InputStream post(String path, String payload, String contentType); | ||
} |