使用GitHub API 查询开源项目信息

news2024/9/23 15:32:17

一、GitHub API介绍

GitHub API 是一组 RESTful API 接口,用于与 GitHub 平台进行交互。通过使用 GitHub API,开发人员可以访问和操作 GitHub 平台上的各种资源,如仓库、提交记录、问题等。

GitHub API 提供了多种功能和端点,以满足开发人员的需求。一些常用的功能包括:

  1. 获取用户信息:可以通过 API 获取用户的基本信息、关注列表、仓库列表等。
  2. 管理仓库:可以通过 API 创建仓库、编辑仓库属性、获取仓库的提交记录等。
  3. 问题和讨论:可以通过 API 创建问题、获取问题列表、参与讨论等。
  4. 用户认证和授权:可以通过 API 进行用户的认证和授权,以获取访问私有资源的权限。
  5. 搜索:可以通过 API 进行代码搜索、问题搜索、用户搜索等。

GitHub API 使用标准的 HTTP 协议进行通信,并返回 JSON 格式的数据。开发人员可以使用任何支持 HTTP 请求的编程语言来与 GitHub API 进行交互。

二、使用java进行项目信息查询

2.1获取用户、仓库基础信息API说明

https://api.github.com/users/{user}
查询用户数据
https://api.github.com/users/{user}/repos
查询仓库数据
https://api.github.com/repos/{user}/{repos}
查询项目

以上接口可以填写具体参数,直接在浏览器进行访问。 

2.2主要用到的第三方工具库

工具名作用maven依赖
httpclienthttp查询
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.4</version>
</dependency>
jacksonjson反序列化
<dependency>
 <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.12.1</version>
</dependency>

2.3使用httpclient查询

 private  static <T> T queryBean(String url, Class<T> c) throws Exception {
        String json = httpGet(url);
        return JsonUtil.string2Object(json, c);
    }

    private static <C extends Collection<E>, E>  E queryList(String url, Class<E> c) throws Exception {
        String json = httpGet(url);
        return (E) JsonUtil.string2Collection(json, ArrayList.class, c);
    }

    private static String httpGet(String url) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = client.execute(new HttpGet(url));
        HttpEntity httpEntity = response.getEntity();
        String json = EntityUtils.toString(httpEntity);
        return json;
    }

上述代码主要有两个查询函数,一个查询单个实体,一个查询实体列表。

2.4jackson反序列化工具

需要注意的是,由于GitHub API返回的查询结果数据非常全面,但我们的代码可能只需要部分字段(需要什么字段自行添加),在使用jackson进行反序列化的时候,应该设置忽略没有申明的字段。

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.ArrayType;
import com.fasterxml.jackson.databind.type.TypeFactory;

import java.io.StringWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public final class JsonUtil {

    private static TypeFactory typeFactory = TypeFactory.defaultInstance();

    private static final ObjectMapper MAPPER = new ObjectMapper();


    static {
        MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        MAPPER.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
        MAPPER.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public static String object2String(Object object) {
        StringWriter writer = new StringWriter();
        try {
            MAPPER.writeValue(writer, object);
        }catch(Exception e) {
            return null;
        }
        return writer.toString();
    }

    @SuppressWarnings("unchecked")
    public static <T> T string2Object(String json, Class<T> clazz) {
        JavaType type = typeFactory.constructType(clazz);
        try {
            return (T) MAPPER.readValue(json, type);
        } catch(Exception e) {
            return null;
        }
    }

    public static String map2String(Map<?,?> map) {
        StringWriter writer = new StringWriter();
        try {
            MAPPER.writeValue(writer, map);
        } catch(Exception e) {
            return null;
        }
        return writer.toString();
    }

    public static Map<String, Object> string2Map(String json) {
        JavaType type = typeFactory.constructMapType(HashMap.class, String.class, Object.class);
        try {
            return MAPPER.readValue(json, type);
        } catch(Exception e) {
            return null;
        }
    }

    public static <K, V> Map<K, V> string2Map(String json, Class<K> keyClazz, Class<V> valueClazz) {
        JavaType type = typeFactory.constructMapType(HashMap.class, keyClazz, valueClazz);
        try {
            return MAPPER.readValue(json, type);
        } catch(Exception e) {
            return null;
        }
    }

    @SuppressWarnings("unchecked")
    public static <T> T[] string2Array(String json, Class<T> clazz) {
        ArrayType type = typeFactory.constructArrayType(clazz);
        try {
            return (T[]) MAPPER.readValue(json, type);
        } catch(Exception e) {
            return null;
        }
    }

    public static <C extends Collection<E>, E> C string2Collection(String json, Class<C> collectionType,
                                                                   Class<E> elemType) {
        JavaType type = typeFactory.constructCollectionType(collectionType, elemType);
        try {
            return MAPPER.readValue(json, type);
        } catch(Exception e) {
            return null;
        }
    }

}

2.5相关javabean定义

这里使用了lombok简化bean编写

import lombok.Data;

@Data
public class ProjectView {

    private String html_url;

    private boolean fork;

    private String description;

    // 开发语言
    private String language;

    // 点赞数
    private int stargazers_count;

    // fork数
    private int forks_count;

}
import lombok.Data;

@Data
public class UserView {

    // github主页
    private String html_url;

    // 公有仓库数量
    private int public_repos;

    private int followers;
}

2.6代码示例

public static void main(String[] args) throws Exception {
        String user = "kingston-csj";
        String project = "jforgame";
        // 查询用户数据
        String url1 = "https://api.github.com/users/{username}".replace("{username}", user);
        System.out.println(queryBean(url1, UserView.class));
        // 查询仓库数据
        String url2 = "https://api.github.com/users/{username}/repos".replace("{username}", user);
        System.out.println(queryList(url2, ProjectView.class));
        // 查询具体仓库
        String url3 = "https://api.github.com/repos/{username}/{reposname}".replace("{username}", user).replace("{reposname}",project);
        System.out.println(queryBean(url3, ProjectView.class));
    }

2.7运行截图 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1490318.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

gin gorm学习笔记

代码仓库 https://gitee.com/zhupeng911/go-advanced.git https://gitee.com/zhupeng911/go-project.git 1. gin介绍 Gin 是使用纯 Golang 语言实现的 HTTP Web框架&#xff0c;Gin接口设计简洁&#xff0c;提供类似Martini的API&#xff0c;性能极高&#xff0c;现在被广泛使用…

SpringBoot接口防抖(防重复提交)的一些实现方案

前言 啥是防抖 思路解析 分布式部署下如何做接口防抖&#xff1f; 具体实现 请求锁 唯一key生成 重复提交判断 前言 作为一名老码农&#xff0c;在开发后端Java业务系统&#xff0c;包括各种管理后台和小程序等。在这些项目中&#xff0c;我设计过单/多租户体系系统&a…

如何制作一个分销商城小程序_揭秘分销商城小程序的制作秘籍

打造赚钱神器&#xff01;揭秘分销商城小程序的制作秘籍 在这个数字化高速发展的时代&#xff0c;拥有一个属于自己的分销商城小程序&#xff0c;已成为众多商家和创业者的必备利器。它不仅能够快速搭建起自己的在线销售渠道&#xff0c;还能够利用分销模式&#xff0c;迅速裂…

机器学习专项课程03:Unsupervised Learning, Recommenders, Reinforcement Learning笔记 Week02

Week 02 of Unsupervised Learning, Recommenders, Reinforcement Learning 课程地址&#xff1a; https://www.coursera.org/learn/unsupervised-learning-recommenders-reinforcement-learning 本笔记包含字幕&#xff0c;quiz的答案以及作业的代码&#xff0c;仅供个人学习…

无人机遥感在农林信息提取中的实现方法与GIS融合应用

在新一轮互联网信息技术大发展的现今&#xff0c;无人机、大数据、人工智能、物联网等新兴技术在各行各业都处于大爆发的前夜。为了将人工智能方法引入农业生产领域。首先在种植、养护等生产作业环节&#xff0c;逐步摆脱人力依赖&#xff1b;在施肥灌溉环节构建智慧节能系统&a…

centos7安装kafka、zookeeper

安装jdk 安装jdk8 安装zookeeper 在指定目录执行下载命令 我是在/newdisk/zookeeper目录下 wget https://archive.apache.org/dist/zookeeper/zookeeper-3.5.8/apache-zookeeper-3.5.8-bin.tar.gz --no-check-certificate下载好后并解压 tar -zxvf apache-zookeeper-3.5…

Kali Linux 2024.1

Kali Linux 2024.1刚刚发布&#xff0c;标志着这个备受欢迎的安全重点Linux发行版在今年的首次重大更新。以其先进的渗透测试和安全审计功能而闻名&#xff0c;它是安全专业人员和爱好者的首选工具。 Kali 2024.1 亮点 本次发布由 Linux 内核 6.6 提供支持&#xff0c;突出了…

Git分布式管理-头歌实验本地版本库

一、本地版本库创建 任务描述 本地Git操作三部曲是“修改-添加-提交”&#xff0c;即先要在本地仓库进行添加、删除或编辑等修改&#xff0c;然后将本地所做的修改添加至暂存区。添加至暂存区的这些本地修改&#xff0c;并未提交到本地仓库&#xff0c;需要执行提交命令才能将暂…

详解Java中集合的List接口实现的ArrayList方法 | Set接口实现的HashSet方法

集合的概念 当我们需要保存一组一样&#xff08;类型相同&#xff09;的元素的时候&#xff0c;我们应该使用一个容器来存储&#xff0c;数组就是这样一个容器。 ● 数组的特点&#xff1a; 数组是一组数据类型相同的元素集合&#xff1b; 创建数组时&#xff0c;必须给定…

【CSP试题回顾】202212-1-现值计算

CSP-202212-1-现值计算 解题代码 #include <iostream> #include <vector> #include <cmath> using namespace std;int main() {int n;double k, sumPrice 0;cin >> n >> k;vector<double>priceList(n 1);for (int i 0; i < n; i){…

分享7款前端动画特效(附效果图及在线演示)

分享7款好玩的前端动画特效 其中有CSS动画、SVG动画、js小游戏等等 下方效果图可能不是特别的生动 那么你可以点击在线预览进行查看相应的动画特效 同时也是可以下载该资源的 canvas彩色画树特效 基于canvas实现的画树特效 同时还可选择树枝的初始数目进行彩色树生成 以下效果…

用docker部署后端项目

一、搭建局域网 1.1、介绍前后端项目搭建 需要4台服务器&#xff0c;在同一个局域网中 1.2、操作 # 搭建net-ry局域网&#xff0c;用于部署若依项目 net-ry&#xff1a;名字 docker network create net-ry --subnet172.68.0.0/16 --gateway172.68.0.1#查看 docker network ls…

【力扣精选算法100道】——存在重复元素 1 or 2 (哈希)

目录 &#x1f6a9;存在重复元素1 &#x1f388;了解题意 &#x1f388;算法原理 &#x1f388;实现代码 &#x1f6a9;存在重复元素2 &#x1f388;了解题意 &#x1f388;算法原理 &#x1f388;代码实现 217. 存在重复元素 - 力扣&#xff08;LeetCode&#xff09; …

Zynq—AD9238数据采集DDR3缓存千兆以太网发送实验(二)

Zynq—AD9238数据采集DDR3缓存千兆以太网发送实验&#xff08;前导&#xff09; Zynq—AD9238数据采集DDR3缓存千兆以太网发送实验&#xff08;一&#xff09; 五、实验目的 本次实验使用电脑上的网络调试助手&#xff0c;将命令帧通过以太网芯片RTL8211&#xff08;RGMII接口…

C#,最小代价多边形三角剖分MCPT(Minimum Cost Polygon Triangulation)算法与源代码

1 最小代价多边形三角剖分算法 凸多边形的三角剖分是通过在非相邻顶点&#xff08;角点&#xff09;之间绘制对角线来形成的&#xff0c;这样对角线就不会相交。问题是如何以最小的代价找到三角剖分的代价。三角剖分的代价是其组成三角形的权重之和。每个三角形的重量是其周长…

FPFH特征提取以及匹配(matlab代码免费)

FPFH特征提取时谁提出的&#xff0c;尊重一下原创&#xff1a; [1] Rusu, Radu Bogdan, Nico Blodow, and Michael Beetz. “Fast point feature histograms (FPFH) for 3D registration.” In 2009 IEEE International Conference on Robotics and Automation, pp. 3212-3217…

websocket在java中的使用教程

本文从websocket服务端和客户端两个方面简单介绍下websocket在java中的使用。 一、websocket服务端&#xff08;WebSocketServer&#xff09; websocket服务端是以本机作为消息的接受端&#xff0c;用于接受客户端websocket发送过来的消息&#xff0c;并可以通过客户端的webs…

06_netdev网卡设备内核模块

01_basicLinux内核模块-CSDN博客文章浏览阅读315次&#xff0c;点赞3次&#xff0c;收藏3次。环境IDubuntuMakefilemodules:clean:basic.creturn 0;运行效果。https://blog.csdn.net/m0_37132481/article/details/136157384my_netdev.c #include <linux/kernel.h> #incl…

Unity 使用AddListener监听事件与取消监听

在Unity中&#xff0c;有时候我们会动态监听组件中的某个事件。当我们使用代码动态加载多次&#xff0c;每次动态加载后我们会发现原来的和新的事件都会监听&#xff0c;如若我们只想取代原来的监听事件&#xff0c;那么就需要取消监听再添加监听了。 如实现如下需求&#xff…

yml代替properties文件进行springboot项目配置

任务&#xff1a;使用yml格式文件代替properties格式文件进行便捷有效的springboot项目配置。 原先&#xff1a; 在与application.properties文件同级目录下新建application.yml文件&#xff0c;以上配置内容修改为&#xff1a; 注&#xff1a;yml文件的一些编写规范