编程调用 GitHub API 搜索开源项目示例:
import web.rest.github;
var http = web.rest.github();
var result = http.api().search.repositories.get(
sort = "stars";order = "desc";q = "aardio NOT xra stars:>50";
)
很简单吧,只需要 3 句 aardio 代码。
实际上 web.rest.github
下面我们再添加几句代码,将返回结果输出到控制台:
import web.rest.github;
var http = web.rest.github();
var result = http.api().search.repositories.get(
sort = "stars";order = "desc";q = "aardio NOT xra stars:>50";
)
//在控制台显示
import console;
console.dumpJson(result);
console.pause(true);
下面我们再改进一下,用 aardio 编写一个简单的窗口程序显示数据:
源代码:
import win.ui;
/*DSG{{*/
var winform = win.form(text="调用 GitHub API";right=873;bottom=603)
winform.add(
button={cls="button";text="点这里搜索 aardio 开源项目";left=562;top=554;right=855;bottom=596;color=14120960;db=1;dr=1;font=LOGFONT(h=-14);z=2};
richedit={cls="richedit";left=6;top=6;right=868;bottom=545;autohscroll=false;db=1;dl=1;dr=1;dt=1;edge=1;link=1;multiline=1;vscroll=1;z=1}
)
/*}}*/
winform.button.oncommand = function(id,event){
winform.button.disabledText = {"✶";"✸";"✹";"✺";"✹";"✷"}
thread.invoke(
function(winform){
import web.rest.github;
var http = web.rest.github()
var github = http.api();
var result = github.search.repositories.get(
sort = "stars";
order = "desc";
q = "aardio NOT xra stars:>50";
)
var items = result.items;
for i,v in table.eachIndex(items){
winform.richedit.print(v.html_url);
winform.richedit.print(v.description);
winform.richedit.print();
}
winform.button.disabledText = null;
},winform
)
}
winform.richedit.onHyperlink =function(message,href){
if( message = 0x202/*_WM_LBUTTONUP*/ ) {
import process;
process.openUrl(href);
}
}
winform.show();
win.loopMessage();
下面我们再改进一下,调用表格控件 win.ui.grid 显示数据,源码如下:
import win.ui;
/*DSG{{*/
var winform = win.form(text="简单数据视图";right=1031;bottom=712)
winform.add(
listview={cls="listview";left=24;top=27;right=996;bottom=679;edge=1;z=1}
)
/*}}*/
import win.ui.grid;
var grid = win.ui.grid(winform.listview);
grid.setColumns({"名称";"网址";"描述"});
import web.rest.github;
var http = web.rest.github();
var dataTable = http.api().search.repositories.get(
sort = "stars";order = "desc";q = "aardio NOT xra stars:>50";
).items;
//设置要显示的数据列
dataTable.fields = {"name","html_url","description"};
//直接让 listview 控件显示数据库的查询结果
grid.setTable(dataTable)
winform.show()
win.loopMessage();
下面再改为 aardio 调用 JavaScript ( React,antd ) 表格组件显示数据:
import win.ui;
/*DSG{{*/
var winform = win.form(text="搜索开源项目";right=1111;bottom=709)
winform.add()
/*}}*/
import web.view;
var wb = web.view(winform);
wb.export(
getData = function(){
import web.rest.github;
var http = web.rest.github()
var github = http.api();
var result = github.search.repositories.get(
sort = "stars";
order = "desc";
q = "aardio NOT xra stars:>50";
)
return result.items;
};
)
wb.html = /**
<!DOCTYPE html><html>
<head>
<meta charset="utf-8" />
<script src="https://lib.baomitu.com/react/18.2.0/umd/react.production.min.js"></script>
<script src="https://lib.baomitu.com/react-dom/18.2.0/umd/react-dom.production.min.js"></script>
<script src="https://lib.baomitu.com/moment.js/2.29.3/moment.min.js"></script>
<script src="https://lib.baomitu.com/antd/4.22.8/antd.min.js"></script>
<link rel="stylesheet" href="https://lib.baomitu.com/antd/4.22.8/antd.min.css">
<script src="https://lib.baomitu.com/babel-standalone/7.0.0-beta.3//babel.min.js"></script>
<style type="text/css">
</style>
</head>
<body>
<script type="text/babel">
const { useState,useEffect,useCallback,useRef } = React;
const { Button,Table,DatePicker,Alert,Tooltip } = antd;
const App = () => {
const [data,setData] = useState([]);
return (
<div style={{ width: '100%', margin: '0 auto', padding:'10px' }}>
<Button onClick={
async ()=> {
var ds = await getData();
setData(ds);
}
}>搜索开源项目</Button>
<br/><br/>
<Table size="middle" pagination={{pageSize:20}} dataSource={data} columns={[
{
title: '开源项目',
dataIndex: 'name',
width: 300,
key: 'name',
render: (_, record) => (
<a href={record.html_url} target="_blank"> {record.name}</a>
),
},
{
title: '描述',
dataIndex: 'description',
key: 'description',
}
]} />
</div>
);
};
ReactDOM.render(<App />, document.querySelector('#app'));
</script>
<div id="app"></div>
</body>
**/
winform.show(3/*_SW_MAXIMIZE*/);
win.loopMessage();
下面我们再改用 aardio 调用操作系统自带的 .NET 表格控件显示数据:
import win.ui;
/*DSG{{*/
var winform = win.form(text="嵌入 .Net 控件";right=860;bottom=519)
winform.add(
custom={cls="custom";left=25;top=25;right=840;bottom=497;db=1;dl=1;dr=1;dt=1;z=1}
)
/*}}*/
import System.Type;
import System.Data;
import System.Windows.Forms;
var Forms = System.Windows.Forms;
//嵌入 .Net 表格控件
var dataGridView = Forms.CreateEmbed("DataGridView",winform.custom);
dataGridView.ColumnHeadersHeightSizeMode = 2; //避免在高分屏下错乱
dataGridView.AutoSizeRowsMode = 11/*DisplayedCells*/ //修正部分高分屏下不显示复选框
//添加数据列
var dataTable = System.Data.DataTable("DT");
dataTable.Columns.Add("名称");
dataTable.Columns.Add("网址");
dataTable.Columns.Add("描述");
//绑定数据源到视图
var dataView = System.Data.DataView(dataTable);
dataGridView.DataSource = dataView;
dataGridView.EditMode = 2;
//描述列改为自适应宽度
dataGridView.Columns[3].AutoSizeMode = Forms.DataGridViewAutoSizeColumnMode.AllCells;
//先移除自动生成的列
dataGridView.Columns.Remove("网址");
//添加一个列用于显示超链接
var links = Forms.DataGridViewLinkColumn();
links.DataPropertyName = "FlightLink";
links.ActiveLinkColor = 0xFF00FFFF;
links.LinkBehavior = Forms.LinkBehavior.SystemDefault;
links.LinkColor = 0xFF0000FF;
links.TrackVisitedState = true;
links.VisitedLinkColor = 0xFFFFFF00;
links.Width = 250;
links.DataPropertyName = "网址";
links.HeaderText = "网址";
links.DisplayIndex = 1;
dataGridView.Columns.Add(links);
//响应.NET 控件点击表格列事件
dataGridView.CellContentClick = function(sender,e){
//如果是第 2 列
if (e.ColumnIndex == 2){
//取出网址
var url = dataGridView.Rows[e.RowIndex+1].Cells[e.ColumnIndex+1].Value;
//打开网址
process.openUrl(url);
}
}
import process;
import web.rest.github;
var http = web.rest.github()
var github = http.api();
//调用 GitHub API
var result = github.search.repositories.get(
sort = "stars";
order = "desc";
q = "aardio NOT xra stars:>50";
)
//获取搜索到的开源项目列表
var items = result.items;
for i,v in table.eachIndex(items){
//添加到表格中
var row = dataTable.NewRow();
row.ItemArray = {v.name,v.html_url, v.description}
dataTable.Rows.Add(row);
}
winform.show();
win.loopMessage();
联系客服