MCPcopy Create free account
hub / github.com/M66B/FairEmail / call

Method call

app/src/main/java/eu/faircode/email/Gemini.java:155–224  ·  view source on GitHub ↗
(Context context, String method, String path, JSONObject args)

Source from the content-addressed store, hash-verified

153 }
154
155 private static JSONObject call(Context context, String method, String path, JSONObject args) throws JSONException, IOException {
156 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
157 String apikey = prefs.getString("gemini_apikey", null);
158
159 // https://ai.google.dev/api/rest
160 // https://ai.google.dev/tutorials/rest_quickstart
161 Uri uri = Uri.parse(getUri(context)).buildUpon()
162 .appendEncodedPath(path)
163 .build();
164 Log.i("Gemini uri=" + uri);
165
166 long start = new Date().getTime();
167
168 URL url = new URL(uri.toString());
169 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
170
171 connection.setRequestMethod(method);
172 connection.setDoOutput(args != null);
173 connection.setDoInput(true);
174 connection.setReadTimeout(TIMEOUT * 1000);
175 connection.setConnectTimeout(TIMEOUT * 1000);
176 ConnectionHelper.setUserAgent(context, connection);
177 connection.setRequestProperty("Accept", "application/json");
178 connection.setRequestProperty("Content-Type", "application/json");
179 connection.setRequestProperty("x-goog-api-key", apikey);
180 connection.connect();
181
182 try {
183 if (args != null) {
184 String json = args.toString();
185 Log.i("Gemini request=" + json);
186 connection.getOutputStream().write(json.getBytes());
187 }
188
189 int status = connection.getResponseCode();
190 if (status != HttpURLConnection.HTTP_OK) {
191 String error = "Error " + status + ": " + connection.getResponseMessage();
192 String detail = null;
193 try {
194 InputStream is = connection.getErrorStream();
195 if (is != null)
196 detail = Helper.readStream(is);
197 } catch (Throwable ex) {
198 Log.w(ex);
199 }
200 Log.w("Gemini error=" + error + " detail=" + detail);
201 if (detail != null)
202 try {
203 JSONObject jroot = new JSONObject(detail);
204 JSONObject jerror = jroot.optJSONObject("error");
205 if (jerror != null) {
206 String msg = jerror.optString("message");
207 if (!TextUtils.isEmpty(msg))
208 detail = msg;
209 }
210 } catch (Throwable ignored) {
211 }
212 throw new IOException(TextUtils.isEmpty(detail) ? error : detail);

Callers 1

generateMethod · 0.95

Calls 15

getUriMethod · 0.95
iMethod · 0.95
setUserAgentMethod · 0.95
readStreamMethod · 0.95
wMethod · 0.95
getOutputStreamMethod · 0.65
getInputStreamMethod · 0.65
getStringMethod · 0.45
buildMethod · 0.45
parseMethod · 0.45
toStringMethod · 0.45
connectMethod · 0.45

Tested by

no test coverage detected