1+ package com .contentstack .gqlspring ;
2+
3+ import com .contentstack .gqlspring .models .ArchivedModel ;
4+ import com .contentstack .gqlspring .models .BlogListModel ;
5+ import com .fasterxml .jackson .core .JsonProcessingException ;
6+ import com .fasterxml .jackson .databind .JsonNode ;
7+ import com .fasterxml .jackson .databind .ObjectMapper ;
8+ import io .github .cdimascio .dotenv .Dotenv ;
9+ import org .jetbrains .annotations .NotNull ;
10+
11+ import java .util .Collections ;
12+
13+ public class Contentstack {
14+
15+
16+ private static String BASE_URL ;
17+ private static String deliverToken ;
18+
19+ // Loads everytime when new instance is created for the Contentstack class
20+ public Contentstack () {
21+ loadEnvVar ();
22+ }
23+
24+ public static <T > T convertToObject (Class <T > clazz , String jsonString ) {
25+ try {
26+ ObjectMapper mapper = new ObjectMapper ();
27+ return (T ) mapper .readValue (jsonString , clazz );
28+ } catch (Exception e ) {
29+ e .printStackTrace ();
30+ return null ;
31+ }
32+ }
33+
34+ // Loads credential from from .env
35+ private void loadEnvVar () {
36+ Dotenv dotenv = Dotenv .load ();
37+ deliverToken = dotenv .get ("_EVV_DELIVERY_TOKEN" );
38+ String _API_KEY = dotenv .get ("_ENV_API_KEY" );
39+ String _ENV = dotenv .get ("_ENV" );
40+ String _HOST = dotenv .get ("_HOST" );
41+ BASE_URL = "https://" + _HOST + "/stacks/" + _API_KEY + "?environment=" + _ENV ;
42+ }
43+
44+ public Object getQuery (@ NotNull String query , @ NotNull String nodeBy , Class <?> cls ) {
45+
46+ try {
47+ if (query .isEmpty () || nodeBy .isEmpty ()) {
48+ throw new Exception ("Please provide a valid node type" );
49+ }
50+
51+ GraphqlBuilder gqlInstance = GraphqlBuilder .Builder .newInstance ()
52+ .setTag (nodeBy )
53+ .setUrl (BASE_URL )
54+ .setQueryString (query )
55+ .setHeader (deliverToken )
56+ .build ();
57+
58+ if (cls .isAssignableFrom (BlogListModel [].class ) || cls .isAssignableFrom (ArchivedModel [].class )) {
59+ JsonNode jsonNode = gqlInstance .fetch ().get ("data" ).get (nodeBy ).get ("items" );
60+ return toListObject (cls , jsonNode .toString ());
61+ }
62+
63+ JsonNode jsonNode = gqlInstance .fetch ().get ("data" ).get (nodeBy ).get ("items" ).get (0 );
64+ return convertToObject (cls , jsonNode .toString ());
65+
66+ } catch (Exception e ) {
67+ e .printStackTrace ();
68+ }
69+ return null ;
70+ }
71+
72+
73+ private Object toListObject (Class <?> cls , String string ) {
74+ try {
75+ //return Arrays.asList(new ObjectMapper().readValue(string, cls)).get(0);
76+ return Collections .singletonList (new ObjectMapper ().readValue (string , cls )).get (0 );
77+ } catch (JsonProcessingException e ) {
78+ e .printStackTrace ();
79+ }
80+ return null ;
81+ }
82+
83+ public Object blogPostById (String id , Class <?> cls ) {
84+ try {
85+ GraphqlBuilder graphqlBuilderInstance = GraphqlBuilder .Builder .newInstance ()
86+ .setTag ("all_blog_post" )
87+ .setUrl (BASE_URL )
88+ .setQueryString ("{\n " +
89+ " all_blog_post(where: {url:" + "\" " + id + "\" " + "}) {\n " +
90+ " items {\n " +
91+ " title\n " +
92+ " url\n " +
93+ " body\n " +
94+ " date\n " +
95+ " seo {\n " +
96+ " enable_search_indexing\n " +
97+ " keywords\n " +
98+ " meta_description\n " +
99+ " meta_title\n " +
100+ " }\n " +
101+ " related_postConnection {\n " +
102+ " edges {\n " +
103+ " node {\n " +
104+ " ... on BlogPost {\n " +
105+ " title\n " +
106+ " url\n " +
107+ " body\n " +
108+ " authorConnection {\n " +
109+ " edges {\n " +
110+ " node {\n " +
111+ " ... on Author {\n " +
112+ " title\n " +
113+ " }\n " +
114+ " }\n " +
115+ " }\n " +
116+ " }\n " +
117+ " }\n " +
118+ " }\n " +
119+ " }\n " +
120+ " }\n " +
121+ " authorConnection {\n " +
122+ " edges {\n " +
123+ " node {\n " +
124+ " ... on Author {\n " +
125+ " title\n " +
126+ " }\n " +
127+ " }\n " +
128+ " }\n " +
129+ " }\n " +
130+ " }\n " +
131+ " }\n " +
132+ "}" )
133+ .setHeader (deliverToken ).build ();
134+ JsonNode strResponse = graphqlBuilderInstance .fetch ().get ("data" ).get ("all_blog_post" ).get ("items" ).get (0 );
135+ return convertToObject (cls , strResponse .toString ());
136+ } catch (Exception e ) {
137+ System .out .println (e .getMessage ());
138+ e .printStackTrace ();
139+ return null ;
140+ }
141+ }
142+
143+
144+ }
0 commit comments