2

我试图弄清楚如何使用 Refit 库来发出 GET 请求,但我不知道它为什么不起作用。我正在关注改装 github 页面上的示例。我错过了什么?似乎 GetUser("octocat") 方法不起作用。我尝试搜索有关如何使用 refit 的其他示例,但找不到任何东西。

    private static async void getUser()
    {

        var gitHubApi = RestService.For<IGitHubApi>("https://api.github.com");


        var octocat = await gitHubApi.GetUser("octocat");

}

public class User
{

    [JsonProperty("login")]
    public string Login { get; set; }

    [JsonProperty("id")]
    public long Id { get; set; }

    [JsonProperty("avatar_url")]
    public string AvatarUrl { get; set; }

    [JsonProperty("gravatar_id")]
    public string GravatarId { get; set; }

    [JsonProperty("url")]
    public string Url { get; set; }

    [JsonProperty("html_url")]
    public string HtmlUrl { get; set; }

    [JsonProperty("followers_url")]
    public string FollowersUrl { get; set; }

    [JsonProperty("following_url")]
    public string FollowingUrl { get; set; }

    [JsonProperty("gists_url")]
    public string GistsUrl { get; set; }

    [JsonProperty("starred_url")]
    public string StarredUrl { get; set; }

    [JsonProperty("subscriptions_url")]
    public string SubscriptionsUrl { get; set; }

    [JsonProperty("organizations_url")]
    public string OrganizationsUrl { get; set; }

    [JsonProperty("repos_url")]
    public string ReposUrl { get; set; }

    [JsonProperty("events_url")]
    public string EventsUrl { get; set; }

    [JsonProperty("received_events_url")]
    public string ReceivedEventsUrl { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("site_admin")]
    public bool SiteAdmin { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("company")]
    public string Company { get; set; }

    [JsonProperty("blog")]
    public string Blog { get; set; }

    [JsonProperty("location")]
    public string Location { get; set; }

    [JsonProperty("email")]
    public object Email { get; set; }

    [JsonProperty("hireable")]
    public object Hireable { get; set; }

    [JsonProperty("bio")]
    public object Bio { get; set; }

    [JsonProperty("public_repos")]
    public long PublicRepos { get; set; }

    [JsonProperty("public_gists")]
    public long PublicGists { get; set; }

    [JsonProperty("followers")]
    public long Followers { get; set; }

    [JsonProperty("following")]
    public long Following { get; set; }

    [JsonProperty("created_at")]
    public System.DateTimeOffset CreatedAt { get; set; }

    [JsonProperty("updated_at")]
    public System.DateTimeOffset UpdatedAt { get; set; }
}

interface IGitHubApi
{


    [Get("/users/{user}")]
    Task<User> GetUser(string user);
}
4

2 回答 2

3

If you want to query GitHub API you need to set a User-Agent

There's 2 way to do this with Refit:

1 - Set the headers on your contract

[Headers("User-Agent: My Favorite User Agent!")]
public interface IGitHubApi
{
    [Get("/users/{user}")]
    Task<User> GetUser(string user);
}

2 - Use a HttpClient and set it there

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("User-Agent", "My Favorite User Agent!");
httpClient.BaseAddress = new Uri("https://api.github.com");
var gitHubApi = RestService.For<IGitHubApi>(httpClient);
var user = await gitHubApi.GetUser("octocat");
于 2018-03-08T14:53:58.523 回答
0

如果你使用的是 Github API 则需要在 Header 中设置 UserAgent,如下所示,

            {
                BaseAddress = new Uri("https://api.github.com"),
                DefaultRequestHeaders = {UserAgent = { ProductInfoHeaderValue.Parse("NakWarsi")}}    //here I have put my username("NakWarsi") what you need to replace with your username 
            };
            _restApiService = RestService.For<IGitHubApi>(_client);

这里我使用 Github 创建了一个示例项目,查看详细信息

于 2019-12-06T04:02:46.647 回答