APL类

可以定义类,用面向对象编程的的思想编写自定义类和自定义函数,编写的APL类的方法可以被其他函数调用

配置方式

8-1-11APL类1

注:1、命名类时,大写字母开头,中间允许字母数字,必须以__c结尾,不能超过50个字符

​ 2、APL类的类名是函数的apiName除去__c的部分,不能修改

​ 3、函数中不能直接通过new关键字实例化对象,增加一个Fx.klass.newInstance实例化APL类,Object object = Fx.klass.newInstance(String str)

​ 4、static方法可以直接通过类名+方法名直接调用,不必实例化;而普通方法需要实例化后才能调用,通过 Fx.klass.newInstance 实例化对象

​ 5、 编写APL类时可以通过main函数进行debug

函数编写模板

Groovy:

class HtttpUtils {

  private static String CONYTENT_TYPE = 'Content-Type'
  private static String JSON = 'application/json'
  private static String MULTIPART = 'multipart/form-data'

  //application/json的post请求
  public static Map post(String url, Map header, Map body) {
    header.put(CONYTENT_TYPE, JSON)
    def (Boolean error, HttpResult data, String errorMessage) = Fx.http.post(url, header, body)
    if (error) {
      log.info(errorMessage)
      return [:]
    }
    log.info('HtttpUtils execute success!')
    return data['content']
  }

  //multipart/form-data请求,byte文件上传
  public Map post(String url, byte[] data, String name, String filename, String contentType) {
    String boundary = Fx.random.randomUUID() 
    Map header = [
      CONYTENT_TYPE: MULTIPART + '; boundary=' + boundary
    ]
    boundary = '--' + boundary
    String param = boundary + '\n' +
                   'Content-Disposition: form-data; name=' + name  + '; filename=' + filename +'\n' +
                   'Content-Type: ' + contentType + '\n' +
                   '\n' +
                   data + '\n' +
                   boundary + '--\n'
    log.info(param)
    def (Boolean error, HttpResult ret, String errorMessage) =  Fx.http.post(url, header, param)
    if (error) {
      log.info(errorMessage)
      return [:]
    }
    log.info('HtttpUtils execute success!')
    return ret['content']
  }

  public static void main(String[] args) {
    String url = "http://httpbin.org/post"
    Map body = ["key": "value"]
    Map header = [:]

    // static方法可以直接通过类名+方法名直接调用,不必实例化
    Map ret = HtttpUtils.post(url, header, body);
    log.info(ret)

    String name = 'file'
    String filename = 'test.png'
    byte[] data = [1, 2, 3]
    String contentType = 'image/png'

    // 普通方法需要实例化后才能调用
    // 需要通过 Fx.klass.newInstance 实例化对象
    HtttpUtils utils = Fx.klass.newInstance('HtttpUtils')
    Map ret1 = utils.post(url, data, name, filename, contentType);
    log.info(ret1)

  }
}

Java:

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

public class JavaUtil {


  private String CONYTENT_TYPE = "Content-Type";
  private String JSON = "application/json";
  private String MULTIPART = "multipart/form-data";

  //application/json的post请求
  public Map post(String url, Map header, Map body) {
    header.put(CONYTENT_TYPE, JSON);
    APIResult data = Fx.http.post(url, header, body); 

    if ((boolean)data.get(0)) {
      log.info(data.get(2));
      return null;
    }
    Fx.log.info("HtttpUtils execute success!");

    HttpResult result = (HttpResult)data.get(1);
    return (Map)result.getContent();
  }

  //multipart/form-data请求,byte文件上传
  public Map post(String url, byte[] data, String name, String filename, String contentType) {
    String boundary = Fx.random.randomUUID();
    Map header = Maps.newHashMap();
    header.put(CONYTENT_TYPE, JSON);

    boundary = "--" + boundary;
    String param = boundary + "\n" +
                  "Content-Disposition: form-data; name=" + name  + "; filename=" + filename +"\n" +
                  "Content-Type: " + contentType + "\n" +
                  "\n" +
                  data + "\n" +
                  boundary + "--\n";
    Fx.log.info(param);
    APIResult apiResult = Fx.http.post(url, header, param); 

    if ((boolean)apiResult.get(0)) {
      log.info(apiResult.get(2));
      return null;
    }
    Fx.log.info("HtttpUtils execute success!");
    HttpResult result = (HttpResult)apiResult.get(1);
    return (Map)result.getContent();
  }


  //函数调试方法
  public void debug() {
    String url = "http://httpbin.org/post";
    Map body = Maps.of("key", "value");
    Map header = Maps.newHashMap();

    // static方法可以直接通过类名+方法名直接调用,不必实例化
    Map data = post(url, header, body);
    Fx.log.info(data);

    String name = "file";
    String filename = "test.png";
    byte[] data1 = new byte[]{1, 2, 3};
    String contentType = "image/png";

    // 普通方法需要实例化后才能调用
    // 需要通过 Fx.klass.newInstance 实例化对象
    JavaUtil utils = Fx.klass.newInstance(JavaUtil.class);

    Map ret1 = utils.post(url, data1, name, filename, contentType);
    log.info(ret1);

  }
}

results matching ""

    No results matching ""