Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,31 @@ public class Main {
}
```

## GraphQL API

This library also provides possibility to use [GraphQL API](https://support.crowdin.com/developer/graphql-api/)

```java
import com.crowdin.client.Client;
import com.crowdin.client.core.model.Credentials;
import com.crowdin.client.core.model.GraphQLRequest;

import java.util.Map;

public class GraphQLExample {

public static void main(String[] args) {
Credentials credentials = new Credentials("token", "organization");
Client client = new Client(credentials);
String query = "query { viewer { projects(first: 2) { edges { node { name } } } } }";
GraphQLRequest request = new GraphQLRequest(query);
Map<String, Object> response = client.graphql(request).getData();
System.out.println(response);
}

}
```

## Seeking Assistance

If you find any problems or would like to suggest a feature, please read the [How can I contribute](/CONTRIBUTING.md#how-can-i-contribute) section in our contributing guidelines.
Expand Down
38 changes: 33 additions & 5 deletions src/main/java/com/crowdin/client/core/CrowdinApi.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.crowdin.client.core;

import com.crowdin.client.core.http.HttpClient;
import com.crowdin.client.core.http.HttpRequestConfig;
import com.crowdin.client.core.http.JsonTransformer;
import com.crowdin.client.core.http.impl.http.ApacheHttpClient;
import com.crowdin.client.core.http.impl.json.JacksonJsonTransformer;
import com.crowdin.client.core.model.ClientConfig;
import com.crowdin.client.core.model.Credentials;
import com.crowdin.client.core.model.*;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
Expand All @@ -19,6 +19,7 @@ public abstract class CrowdinApi {
protected final HttpClient httpClient;
protected final ClientConfig clientConfig;
protected final String url;
protected final String graphqlUrl;

public CrowdinApi(Credentials credentials) {
this(credentials, ClientConfig.builder()
Expand All @@ -38,23 +39,50 @@ public CrowdinApi(Credentials credentials, ClientConfig clientConfig) {
defaultHeaders.put("X-Crowdin-Integrations-User-Agent", clientConfig.getIntegrationUserAgent());
}
JsonTransformer jsonTransformer = (clientConfig.getJsonTransformer() != null)
? clientConfig.getJsonTransformer() : new JacksonJsonTransformer();
? clientConfig.getJsonTransformer() : new JacksonJsonTransformer();
this.httpClient = (clientConfig.getHttpClient() != null)
? clientConfig.getHttpClient()
: new ApacheHttpClient(credentials, jsonTransformer, defaultHeaders, clientConfig.getProxy(), clientConfig.getProxyCreds(), clientConfig.getHttpTimeoutMs());
? clientConfig.getHttpClient()
: new ApacheHttpClient(credentials, jsonTransformer, defaultHeaders, clientConfig.getProxy(), clientConfig.getProxyCreds(), clientConfig.getHttpTimeoutMs());
this.clientConfig = clientConfig;
if (credentials.getBaseUrl() != null) {
if (credentials.getBaseUrl().endsWith("/")) {
this.url = credentials.getBaseUrl() + "api/v2";
this.graphqlUrl = credentials.getBaseUrl() + "api/graphql";
} else {
this.url = credentials.getBaseUrl() + "/api/v2";
this.graphqlUrl = credentials.getBaseUrl() + "/api/graphql";
}
} else {
if (credentials.getOrganization() != null) {
this.url = "https://" + credentials.getOrganization() + ".api.crowdin.com/api/v2";
this.graphqlUrl = "https://" + credentials.getOrganization() + ".api.crowdin.com/api/graphql";
} else {
this.url = "https://api.crowdin.com/api/v2";
this.graphqlUrl = "https://api.crowdin.com/api/graphql";
}
}
}

/**
* @param request request object
* @return newly created bundle
* @see <ul>
* <li><a href="https://support.crowdin.com/developer/graphql-api/" target="_blank"><b>API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<Map<String, Object>> graphql(GraphQLRequest request) {
GraphQLResponse response = this.graphql(request, GraphQLResponse.class);
return ResponseObject.of(response.getData());
}

/**
* @param request request object
* @return newly created bundle
* @see <ul>
* <li><a href="https://support.crowdin.com/developer/graphql-api/" target="_blank"><b>API Documentation</b></a></li>
* </ul>
*/
public <T> T graphql(GraphQLRequest request, Class<T> clazz) {
return this.httpClient.post(this.graphqlUrl, request, new HttpRequestConfig(), clazz);
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/crowdin/client/core/model/GraphQLRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.crowdin.client.core.model;

import lombok.Data;

import java.util.Map;

@Data
public class GraphQLRequest {

private String query;
private Map<String, Object> variables;
private String operationName;

public GraphQLRequest() {
}

public GraphQLRequest(String query) {
this();
this.query = query;
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/crowdin/client/core/model/GraphQLResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.crowdin.client.core.model;

import lombok.Data;

import java.util.Map;

@Data
public class GraphQLResponse {

private Map<String, Object> data;
}
74 changes: 74 additions & 0 deletions src/test/java/com/crowdin/client/core/CrowdinApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.crowdin.client.core;

import com.crowdin.client.core.model.GraphQLRequest;
import com.crowdin.client.framework.RequestMock;
import com.crowdin.client.framework.TestClient;
import lombok.Data;
import lombok.var;
import org.apache.http.client.methods.HttpPost;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class CrowdinApiTest extends TestClient {

@Override
public List<RequestMock> getMocks() {
return Arrays.asList(
RequestMock.build(this.graphqlUrl, HttpPost.METHOD_NAME, "api/core/graphQLRequest.json",
"api/core/graphQLResponse.json")
);
}

@Test
public void graphQLTest() {
var res = this.graphql(new GraphQLRequest("query { viewer { projects(first: 2) { edges { node { name } } } } }"));
var edges = List.class.cast(Map.class.cast(Map.class.cast(Map.class.cast(res.getData().get("viewer")).get("projects"))).get("edges"));
assertEquals(2, edges.size());
assertEquals("test1", Map.class.cast(Map.class.cast(edges.get(0)).get("node")).get("name").toString());
assertEquals("test2", Map.class.cast(Map.class.cast(edges.get(1)).get("node")).get("name").toString());
}

@Test
public void graphQLCustomTypeTest() {
var res = this.graphql(new GraphQLRequest("query { viewer { projects(first: 2) { edges { node { name } } } } }"), GraphQLRes.class);
assertEquals(2, res.getData().getViewer().getProjects().getEdges().size());
assertEquals("test1", res.getData().getViewer().getProjects().getEdges().get(0).getNode().getName());
assertEquals("test2", res.getData().getViewer().getProjects().getEdges().get(1).getNode().getName());
}

@Data
public static class GraphQLRes {

private GraphQLResData data;

@Data
public static class GraphQLResData {
private GraphQLResViewer viewer;
}

@Data
public static class GraphQLResViewer {
private GraphQLResProjects projects;
}

@Data
public static class GraphQLResProjects {
private List<GraphQLResEdge> edges;
}

@Data
public static class GraphQLResEdge {
private GraphQLResNode node;
}

@Data
public static class GraphQLResNode {
private String name;
}
}
}
3 changes: 3 additions & 0 deletions src/test/resources/api/core/graphQLRequest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"query": "query { viewer { projects(first: 2) { edges { node { name } } } } }"
}
20 changes: 20 additions & 0 deletions src/test/resources/api/core/graphQLResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"data": {
"viewer": {
"projects": {
"edges": [
{
"node": {
"name": "test1"
}
},
{
"node": {
"name": "test2"
}
}
]
}
}
}
}
Loading