<commit_before>package com.jasonette.seed.Action;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Base64;
import android.util.Log;

import com.jasonette.seed.Core.JasonViewActivity;
import com.jasonette.seed.Helper.JasonHelper;
import com.jasonette.seed.Launcher.Launcher;

import org.json.JSONObject;
import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
import java.util.UUID;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import timber.log.Timber;

public class JasonNetworkAction {
    private void _request(final JSONObject callback, final JSONObject action, final JSONObject data, final JSONObject event, final Context context){
        try{
            final JSONObject options = action.getJSONObject("options");
            if(options.has("url")){
                String url = options.getString("url");

                // method
                String method = "GET";
                if(options.has("method")) {
                    method = options.getString("method").toUpperCase();
                }

                // Attach session if it exists
                SharedPreferences pref = context.getSharedPreferences("session", 0);
                JSONObject session = null;

                URI uri_for_session = new URI(url.toLowerCase());
                String session_domain = uri_for_session.getHost();

                if(pref.contains(session_domain)){
                    String str = pref.getString(session_domain, null);
                    session = new JSONObject(str);
                }


                Request request;
                Request.Builder builder = new Request.Builder();
                // Attach Header from Session
                if(session != null && session.has("header")) {
                    Iterator<?> keys = session.getJSONObject("header").keys();
                    while (keys.hasNext()) {
                        String key = (String) keys.next();
                        String val = session.getJSONObject("header").getString(key);
                        builder.addHeader(key, val);
                    }
                }

                // Attach header passed in as options
                if (options.has("header")) {
                    JSONObject header = options.getJSONObject("header");
                    Iterator<String> keys = header.keys();
                    try {
                        while (keys.hasNext()) {
                            String key = (String) keys.next();
                            String val = header.getString(key);
                            builder.addHeader(key, val);
                        }
                    } catch (Exception e) {

                    }
                }

                if(method.equalsIgnoreCase("get")) {
                    Uri.Builder b = Uri.parse(url).buildUpon();

                    // Attach Params from Session
                    if(session != null && session.has("body")) {
                        Iterator<?> keys = session.getJSONObject("body").keys();
                        while (keys.hasNext()) {
                            String key = (String) keys.next();
                            String val = session.getJSONObject("body").getString(key);

                            b.appendQueryParameter(key, val);

                        }
                    }

                    // params
                    if (options.has("data")) {
                        JSONObject d = options.getJSONObject("data");
                        Iterator<String> keysIterator = d.keys();
                        try {
                            while (keysIterator.hasNext()) {
                                String key = (String) keysIterator.next();
                                String val = d.getString(key);
                                b.appendQueryParameter(key, val);
                            }
                        } catch (Exception e) {

                        }
                    }

                    Uri uri = b.build();
                    url = uri.toString();

                    request = builder
                            .url(url)
                            .build();
                } else {

                    if(options.has("header") && options.getJSONObject("header").has("content_type")) {
                        String content_type = options.getJSONObject("header").getString("content_type");
                        MediaType mediaType;
                        if (content_type.equalsIgnoreCase("json")) {
                            mediaType = MediaType.parse("application/json; charset=utf-8");
                        } else {
                            mediaType = MediaType.parse(content_type);
                        }
                        Request.Builder requestBuilder = new Request.Builder();
                        byte[] d = Base64.decode(options.getString("data"), Base64.DEFAULT);
                        request = requestBuilder
                                .url(url)
                                .put(RequestBody.create(mediaType, d))<commit_msg>Fix network request action POST method with JSON data to work correctly (#189)<commit_after>package com.jasonette.seed.Action;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Base64;
import android.util.Log;

import com.jasonette.seed.Core.JasonViewActivity;
import com.jasonette.seed.Helper.JasonHelper;
import com.jasonette.seed.Launcher.Launcher;

import org.json.JSONObject;
import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
import java.util.UUID;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import timber.log.Timber;

public class JasonNetworkAction {
    private void _request(final JSONObject callback, final JSONObject action, final JSONObject data, final JSONObject event, final Context context){
        try{
            final JSONObject options = action.getJSONObject("options");
            if(options.has("url")){
                String url = options.getString("url");

                // method
                String method = "GET";
                if(options.has("method")) {
                    method = options.getString("method").toUpperCase();
                }

                // Attach session if it exists
                SharedPreferences pref = context.getSharedPreferences("session", 0);
                JSONObject session = null;

                URI uri_for_session = new URI(url.toLowerCase());
                String session_domain = uri_for_session.getHost();

                if(pref.contains(session_domain)){
                    String str = pref.getString(session_domain, null);
                    session = new JSONObject(str);
                }


                Request request;
                Request.Builder builder = new Request.Builder();
                // Attach Header from Session
                if(session != null && session.has("header")) {
                    Iterator<?> keys = session.getJSONObject("header").keys();
                    while (keys.hasNext()) {
                        String key = (String) keys.next();
                        String val = session.getJSONObject("header").getString(key);
                        builder.addHeader(key, val);
                    }
                }

                // Attach header passed in as options
                if (options.has("header")) {
                    JSONObject header = options.getJSONObject("header");
                    Iterator<String> keys = header.keys();
                    try {
                        while (keys.hasNext()) {
                            String key = (String) keys.next();
                            String val = header.getString(key);
                            builder.addHeader(key, val);
                        }
                    } catch (Exception e) {

                    }
                }

                if(method.equalsIgnoreCase("get")) {
                    Uri.Builder b = Uri.parse(url).buildUpon();

                    // Attach Params from Session
                    if(session != null && session.has("body")) {
                        Iterator<?> keys = session.getJSONObject("body").keys();
                        while (keys.hasNext()) {
                            String key = (String) keys.next();
                            String val = session.getJSONObject("body").getString(key);

                            b.appendQueryParameter(key, val);

                        }
                    }

                    // params
                    if (options.has("data")) {
                        JSONObject d = options.getJSONObject("data");
                        Iterator<String> keysIterator = d.keys();
                        try {
                            while (keysIterator.hasNext()) {
                                String key = (String) keysIterator.next();
                                String val = d.getString(key);
                                b.appendQueryParameter(key, val);
                            }
                        } catch (Exception e) {

                        }
                    }

                    Uri uri = b.build();
                    url = uri.toString();

                    request = builder
                            .url(url)
                            .build();
                } else {

                    if(options.has("header") && options.getJSONObject("header").has("content_type")) {
                        String content_type = options.getJSONObject("header").getString("content_type");
                        MediaType mediaType;
                        byte[] d;
                        if (content_type.equalsIgnoreCase("json")) {
                            mediaType = MediaType.parse("application/json; charset=utf-8");
                            d = options.getString("data").getBytes();
                        } else {
                            mediaType = MediaType.parse(content_type);
                            d = Base64.decode(options.getString("data"), Base64.DEFAULT);
                        }
                        Request.Builder requestBuilder = new Request.Builder();

                        request = requestBuilder
                                .url(url)
                                .method(method, RequestBody.create(mediaType, d))
{'fraction_non_alphanumeric': 0.09893244984354868, 'fraction_numerical': 0.003221056506534143, 'mean_word_length': 1.31065277482458, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1694242', 'n_tokens_mistral': 2561, 'n_tokens_neox': 2360, 'n_words': 523}