Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问这博客其中的API是做什么用的? #16

Open
Mabbs opened this issue Feb 19, 2022 · 4 comments
Open

请问这博客其中的API是做什么用的? #16

Mabbs opened this issue Feb 19, 2022 · 4 comments

Comments

@Mabbs
Copy link

Mabbs commented Feb 19, 2022

https://github.com/imuncle/gitblog/blob/master/api.html

设计的很有意思,可是这API怎么想也调不了吧?博客里的API示例是真的有被证实过可以用的吗?

@imuncle
Copy link
Owner

imuncle commented Feb 20, 2022

@Mabbs 你的访问方式错了,应该是在博客链接的域名下访问,比如:
https://imuncle.github.io/gitblog/api.html?id=16
上面这个链接就展示的是这个issue的内容

API的设计初衷是方便二次开发,比如写一个手机APP访问博客,APP就可以访问这些API来获取博客的内容

@Mabbs
Copy link
Author

Mabbs commented Feb 20, 2022

@Mabbs 你的访问方式错了,应该是在博客链接的域名下访问,比如: https://imuncle.github.io/gitblog/api.html?id=16 上面这个链接就展示的是这个issue的内容

API的设计初衷是方便二次开发,比如写一个手机APP访问博客,APP就可以访问这些API来获取博客的内容

啊,不是,我的意思是这个调用只能在浏览器渲染完之后才有用啊,我用其他程序请求这个api得到的只是这个html啊,还是说有什么服务器端渲染技术?

@Mabbs
Copy link
Author

Mabbs commented Feb 20, 2022

你的访问方式错了,应该是在博客链接的域名下访问,比如: https://imuncle.github.io/gitblog/api.html?id=16 上面这个链接就展示的是这个issue的内容
API的设计初衷是方便二次开发,比如写一个手机APP访问博客,APP就可以访问这些API来获取博客的内容

啊,不是,我的意思是这个调用只能在浏览器渲染完之后才有用啊,我用其他程序请求这个api得到的只是这个html啊,还是说有什么服务器端渲染技术?

不是,这个跟用不用浏览器没关系啊,只需要在程序中使用HTTP GET请求就可以拿到json格式的数据了

我GET请求了没啥用啊

[root@vultr ~]# curl -v https://imuncle.github.io/gitblog/api.html?id=16
* About to connect() to imuncle.github.io port 443 (#0)
*   Trying 185.199.111.153...
* Connected to imuncle.github.io (185.199.111.153) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
*       subject: CN=www.github.com,O="GitHub, Inc.",L=San Francisco,ST=California,C=US
*       start date: May 06 00:00:00 2020 GMT
*       expire date: Apr 14 12:00:00 2022 GMT
*       common name: www.github.com
*       issuer: CN=DigiCert SHA2 High Assurance Server CA,OU=www.digicert.com,O=DigiCert Inc,C=US
> GET /gitblog/api.html?id=16 HTTP/1.1
> User-Agent: curl/7.29.0
> Host: imuncle.github.io
> Accept: */*
>
< HTTP/1.1 200 OK
< Connection: keep-alive
< Content-Length: 8493
< Server: GitHub.com
< Content-Type: text/html; charset=utf-8
< permissions-policy: interest-cohort=()
< Last-Modified: Thu, 22 Oct 2020 11:37:47 GMT
< Access-Control-Allow-Origin: *
< Strict-Transport-Security: max-age=31556952
< ETag: "5f916f0b-212d"
< expires: Sun, 20 Feb 2022 05:12:06 GMT
< Cache-Control: max-age=600
< x-proxy-cache: MISS
< X-GitHub-Request-Id: 3E56:4CAB:E02825:14D83B2:6211CB4E
< Accept-Ranges: bytes
< Date: Sun, 20 Feb 2022 05:02:06 GMT
< Via: 1.1 varnish
< Age: 0
< X-Served-By: cache-qpg1247-QPG
< X-Cache: MISS
< X-Cache-Hits: 0
< X-Timer: S1645333327.523335,VS0,VE236
< Vary: Accept-Encoding
< X-Fastly-Request-ID: db6cde1a65214e686213ba2b1bfd2bf242bde959
<
<!DOCTYPE HTML>
<html>
    <head>
        <title>API请求</title>
        <meta charset="utf-8">
    </head>
    <body>
        <pre id="data"></pre>
    </body>
</html>
<script src = "js/jquery.min.js"></script>
<script>
var api = function(config) {
    var self = this;

    self.info = new Object();

    self.getUrlParam = function(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]);
        return null;
    }

    self.utc2localTime = function(time) {
        var time_string_utc_epoch = Date.parse(time);
        var unixTimestamp = new Date(time_string_utc_epoch);
        var year = unixTimestamp.getFullYear();
        var month = unixTimestamp.getMonth() + 1;
        var date = unixTimestamp.getDate();
        var hour = unixTimestamp.getHours();
        var minute = unixTimestamp.getMinutes();
        var second = unixTimestamp.getSeconds();
        hour = (hour<10)?'0'+hour:hour;
        minute = (minute<10)?'0'+minute:minute;
        second = (second<10)?'0'+second:second;
        return year+'/'+month+'/'+date+' '+hour+':'+minute+':'+second;
    }

    self.options = {
        id: null,
        menu: null,
        label: null,
        q: null,
        page: 1
    }

    self.set = function() {
        if (self.getUrlParam('id') != undefined) {
            self.options.id = parseInt(self.getUrlParam('id'));
        }
        if (self.getUrlParam('label') != undefined) {
            self.options.label = self.getUrlParam('label');
        }
        if (self.getUrlParam('menu') != undefined) {
            self.options.menu = self.getUrlParam('menu');
        }
        if (self.getUrlParam('q') != undefined) {
            self.options.q = self.getUrlParam('q');
        }
        if (self.getUrlParam('page') != undefined) {
            self.options.page = parseInt(self.getUrlParam('page'));
        }
    }

    self.set();

    self.HandleInfo = function() {

        if(self.options.id != null) {
            $.ajax({
                type: 'get',
                async: false,
                headers: {
                    Accept: 'application/vnd.github.squirrel-girl-preview, application/vnd.github.html+json, application/x-www-form-urlencoded',
                },
                url: 'https://api.github.com/repos/' + config.name + '/' + config.repo + '/issues/' + self.options.id,
                success: function(data) {
                    self.info.title = data.title;
                    self.info.time = self.utc2localTime(data.created_at);
                    data.body_html = data.body_html.replace(/</g,"&lt;");
                    data.body_html = data.body_html.replace(/>/g,"&gt;");
                    self.info.content = data.body_html;
                    self.info.labels = new Array();
                    for (var i in data.labels) {
                        var label = new Object()
                        label.name = data.labels[i].name;
                        self.info.labels.push(label);
                    }
                }
            });
            $.ajax({
                type: 'get',
                async: false,
                headers: {
                    Accept: 'application/vnd.github.squirrel-girl-preview+json'
                },
                url: 'https://api.github.com/repos/' + config.name + '/' + config.repo + '/issues/' + self.options.id + '/reactions?content=heart',
                success: function(data) {
                    self.info.like = data.length;
                }
            });
            self.info = JSON.stringify(self.info, null, "\t");
            document.getElementById('data').innerHTML = self.info;
        } else if(self.options.menu != null) {
            $.ajax({
                type: 'get',
                headers: {
                    Accept: 'application/json',
                },
                url: 'config.json',
                success: function(data) {
                    self.info = new Array();
                    for (var i in data.menu) {
                        var label = new Object();
                        label.name = i;
                        label.url = data.menu[i];
                        console.log(label)
                        self.info.push(label);
                    }
                    self.info = JSON.stringify(self.info, null, "\t");
                    document.getElementById('data').innerHTML = self.info;
                }
            });
        } else {
            var issue_url;
            var issue_perpage_url;
            self.info.page = self.options.page;
            if (self.options.label == undefined) {
                if (self.options.q == undefined) {
                    issue_url = 'https://api.github.com/repos/' + config.name + '/' + config.repo;
                    issue_perpage_url = 'https://api.github.com/repos/' + config.name + '/' + config.repo + '/issues?creator=' + config.name + '&';
                } else {
                    var search = encodeURI(self.options.q);
                    issue_url = 'https://api.github.com/search/issues?q=' + search + ' author:' + config.name + '+in:title,body';
                    issue_perpage_url = 'https://api.github.com/search/issues?q=' + search + ' author:' + config.name + '+in:title,body&';
                }
            } else {
                issue_url = 'https://api.github.com/repos/' + config.name + '/' + config.repo + '/issues?labels=' + self.options.label;
                issue_perpage_url = 'https://api.github.com/repos/' + config.name + '/' + config.repo + '/issues?creator=' + config.name + '&labels=' + self.options.label + '&';

            }
            $.ajax({
                type: 'get',
                url: issue_url,
                async: false,
                success: function(data) {
                    var itemNum = 0;
                    if (self.options.label != null && self.options.label != undefined) {
                        if (self.options.q != null && self.options.q != undefined) {
                            itemNum = data.total_count;
                        } else {
                            itemNum = data.length;
                        }
                    } else if (self.options.id != null && self.options.id != undefined) {
                        itemNum = data.length;
                        document.getElementById('comments-num').innerHTML = itemNum;
                    } else {
                        itemNum = data.open_issues_count;
                    }
                    if (itemNum > 10) {
                        self.info.page_num = Math.ceil(itemNum / 10);
                    }
                }
            });
            $.ajax({
                type: 'get',
                async: false,
                url: issue_perpage_url + 'page=' + self.options.page + '&per_page=10',
                success: function(data) {
                    self.info.article = new Array();
                    if (self.options.q != undefined && self.options.q != null) {
                        data = data.items;
                    }
                    for(var i in data) {
                        var item = new Object();
                        item.id = data[i].number;
                        item.time = self.utc2localTime(data[i].created_at);
                        item.title = data[i].title;
                        item.author = data[i].user.login;
                        data[i].body = data[i].body.replace(/</g,"&lt;");
                        data[i].body = data[i].body.replace(/>/g,"&gt;");
                        item.content = data[i].body;
                        item.labels = new Array();
                        for(var j in data[i].labels) {
                            var label = new Object();
                            label.name = data[i].labels[j].name;
                            item.labels.push(label);
                        }
                        self.info.article.push(item);
                    }
                }
            });
            self.info = JSON.stringify(self.info, null, "\t");
            document.getElementById('data').innerHTML = self.info;
        }
    }

    self.HandleInfo();
}

$.ajax({
    type: 'get',
    headers: {
        Accept: 'application/json',
    },
    url: 'config.json',
    success: function(data) {
        new api(data);
    }
});
* Connection #0 to host imuncle.github.io left intact

@imuncle
Copy link
Owner

imuncle commented Feb 20, 2022

啊,不是,我的意思是这个调用只能在浏览器渲染完之后才有用啊,我用其他程序请求这个api得到的只是这个html啊,还是说有什么服务器端渲染技术?

明白你的意思了,确实是有问题哈,有时间再想想怎么做,感谢指正

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants