<commit_before>
package tools.haxelib;
import tools.haxelib.Common;
import tools.haxelib.ClientCommon;
import tools.haxelib.ClientCore;

class ClientRestful extends ClientCore {

  public function new() {
    super();
    RemoteRepos.init(this);
  }

  override public function
  url(u:String,c:String) {
    return "http://"+u+"/index.php?method="+c;
  }
  
  override public function
  request(r:String,prms:Dynamic,fn:Dynamic->Void){
    var parameters = "";
    if (prms != null) {
      var
        flds = Reflect.fields(prms),
        sb = new StringBuf();
      
      for (f in flds) {
        var v = Reflect.field(prms,f);
        if (v == null) continue;
        sb.add(f);
        sb.add("=");
        sb.add(StringTools.urlEncode(v));
        sb.add("&");
      }
      parameters = sb.toString().substr(0,-1);
    }

    trace("request is "+r+"&"+parameters);
    var h = new haxe.Http(r+"&"+parameters);
    if (fn != null) {
      h.onData = function(d) {
        fn(hxjson2.JSON.decode(d));
      };
    }
    h.request(false);
  }

  static function
  getStatus(d:Dynamic):Status {
    var e;
    if (Reflect.field(d,"PAYLOAD") != null)
      e = Type.createEnum(Status,d.ERR,[d.PAYLOAD]);
    else
      e = Type.createEnum(Status,d.ERR);
    return e;
  }

  /*
    make one request to the given -R repo, or iterate over all repos
  */
  function
  requestDispatch(options:Options,cmd:String,prms:Dynamic,fn:String->Status->Bool) {
    
    if (options.repo != null) {
      request(url(options.repo,cmd),prms,function(d) {
          fn(options.repo,getStatus(d)) ;
      });
    } else {
      RemoteRepos.each(cmd,prms,function(repo:String,d:Dynamic) {
          return fn(repo,getStatus(d)) ;
        });
    }
  }
  
  override public function
  install(options:Options,prj:String,ver:String) {
    var me = this;
    info(options,prj,function(repoUrl:String,s:Status) {
        return switch(s) {
        case OK_PROJECT(j):
          if (j.curversion == null)
            return false;

          var found = true;
          
          if (ver != null) {
            trace("given version is "+ver);
            found = false;
            for( v in j.versions )
              if( v.name == ver ) {
                found = true;
                break;
              }
          } else
            ver = j.curversion;

          if (found) {
            me.doInstall(options,repoUrl,prj,ver);
          }<commit_msg>check for http errors, e.g. repo not available<commit_after>
package tools.haxelib;
import tools.haxelib.Common;
import tools.haxelib.ClientCommon;
import tools.haxelib.ClientCore;

class ClientRestful extends ClientCore {

  public function new() {
    super();
    RemoteRepos.init(this);
  }

  override public function
  url(u:String,c:String) {
    return "http://"+u+"/index.php?method="+c;
  }
  
  override public function
  request(r:String,prms:Dynamic,fn:Dynamic->Void){
    var parameters = "";
    if (prms != null) {
      var
        flds = Reflect.fields(prms),
        sb = new StringBuf();
      
      for (f in flds) {
        var v = Reflect.field(prms,f);
        if (v == null) continue;
        sb.add(f);
        sb.add("=");
        sb.add(StringTools.urlEncode(v));
        sb.add("&");
      }
      parameters = sb.toString().substr(0,-1);
    }

    trace("request is "+r+"&"+parameters);
    var h = new haxe.Http(r+"&"+parameters);
    if (fn != null) {
      h.onData = function(d) {
        fn(hxjson2.JSON.decode(d));
      };
    }
    h.onError = function(e:String) {
      Os.print("Error requesting "+ r + "&" + parameters);
      Os.print("Server not available? Checking next repo ...");
      fn(null);
    };
    
    h.request(false);
  }

  static function
  getStatus(d:Dynamic):Status {
    var e;
    if (Reflect.field(d,"PAYLOAD") != null)
      e = Type.createEnum(Status,d.ERR,[d.PAYLOAD]);
    else
      e = Type.createEnum(Status,d.ERR);
    return e;
  }

  /*
    make one request to the given -R repo, or iterate over all repos
  */
  function
  requestDispatch(options:Options,cmd:String,prms:Dynamic,fn:String->Status->Bool) {
    
    if (options.repo != null) {
      request(url(options.repo,cmd),prms,function(d) {
          if (d != null)
            fn(options.repo,getStatus(d)) ;
            
      });
    } else {
      RemoteRepos.each(cmd,prms,function(repo:String,d:Dynamic) {
          if (d != null)
            return fn(repo,getStatus(d)) ;
          return false ;// take next repo
        });
    }
  }
  
  override public function
  install(options:Options,prj:String,ver:String) {
    var me = this;
    info(options,prj,function(repoUrl:String,s:Status) {
        return switch(s) {
        case OK_PROJECT(j):
          if (j.curversion == null)
            return false;

          var found = true;
          
          if (ver != null) {
            trace("given version is "+ver);
            found = false;
            for( v in j.versions )
              if( v.name == ver ) {
                found = true;
                break;
              }
          } else
            ver = j.curversion;

          trace("license is "+j.license);
          
          if (found) {
            me.doInstall(options,repoUrl,prj,ver,j.license);
          }
{'fraction_non_alphanumeric': 0.14418516410548282, 'fraction_numerical': 0.0011383039271485487, 'mean_word_length': 2.0142938822184107, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3727474', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1718, 'n_words': 342}