如何从Azure DevOps扩展中调用预发布的Rest Apis?

时间:2019-12-30 22:39:48

标签: azure-devops azure-devops-rest-api azure-devops-extensions

Azure DevOps REST API的预览版的seems REST客户端未发布。因此,API已存在,works,但没有REST客户端。

那么我该如何访问Azure DevOps扩展中的6.0(到目前为止:预览)功能pagesBatch

1 个答案:

答案 0 :(得分:0)

由于它是预览API,并且尚未包含在SDK中,因此您可以使用Typescript进行纯HTTP Get / Post Rest API调用,例如:

安装:

public class JavaScriptInterface {
private Context context;
    private NotificationManager nm;
    public JavaScriptInterface(Context context) {
        this.context = context;
    }

    @JavascriptInterface
    public void getBase64FromBlobData(String base64Data) throws IOException {
        convertBase64StringToPdfAndStoreIt(base64Data);
    }
    public static String getBase64StringFromBlobUrl(String blobUrl){
        if(blobUrl.startsWith("blob")){

            return "javascript: var xhr=new XMLHttpRequest();" +
                    "xhr.open('GET', '"+blobUrl+"', true);" +
                    "xhr.setRequestHeader('Content-type','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8');" +
                    "xhr.responseType = 'blob';" +
                    "xhr.onload = function(e) {" +
                    "    if (this.status == 200) {" +
                    "        var blobPdf = this.response;" +
                    "        var reader = new FileReader();" +
                    "        reader.readAsDataURL(blobPdf);" +
                    "        reader.onloadend = function() {" +
                    "            base64data = reader.result;" +
                    "            Android.getBase64FromBlobData(base64data);" +
                    "        }" +
                    "    }" +
                    "};" +
                    "xhr.send();";


        }
        return "javascript: console.log('It is not a Blob URL');";
    }
    private void convertBase64StringToPdfAndStoreIt(String base64PDf) throws IOException {

        Log.e("base64PDf",base64PDf);
        String currentDateTime = DateFormat.getDateTimeInstance().format(new Date());
        Calendar calendar=Calendar.getInstance();
      ;
        final File dwldsPath = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS) + "/Report" +   calendar.getTimeInMillis() + "_.xlsx");
        byte[] pdfAsBytes = Base64.decode(base64PDf.replaceFirst("data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,", ""), 0);
            Log.e("bytearrya",""+pdfAsBytes);
        FileOutputStream os;
        os = new FileOutputStream(dwldsPath, false);
        os.write(pdfAsBytes);
        os.flush();
        os.close();

        if(dwldsPath.exists()) {
            sendNotification();


            File dir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOWNLOADS) + "/Report" +   
calendar.getTimeInMillis() + "_.xlsx");
            Intent sendIntent = new Intent(Intent.ACTION_VIEW);
         String path=   dir.getAbsolutePath();

            Uri uri;
            if (Build.VERSION.SDK_INT < 24) {
                uri = Uri.fromFile(dir);
            } else {
                File file = new File(path);
                uri = FileProvider.getUriForFile((MainActivity)this.context, 
this.context.getApplicationContext().getPackageName() + ".provider", file);
//                    uri = Uri.parse("file://" + dir); 
            }
            sendIntent.setDataAndType(uri, "application/vnd.ms-excel");
            sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            try{
                context.startActivity(sendIntent);

            }catch (Exception e){
                Toast.makeText(context, "Np app found to view file", Toast.LENGTH_SHORT).show();
            }

        }

    }
}

然后在扩展名中:

npm install --save request
npm install --save request-promise-native

或者使用其他Rest API方法,有很多方法可以在Typescript上实现。