Skip to content
This repository has been archived by the owner on Mar 25, 2023. It is now read-only.

Parse body message from http request #16

Open
SimonLab opened this issue May 20, 2022 · 2 comments
Open

Parse body message from http request #16

SimonLab opened this issue May 20, 2022 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed priority-2 Second highest priority, should be worked on as soon as the Priority-1 issues are finished T1h Time Estimate 1 Hour

Comments

@SimonLab
Copy link
Member

At the moment we return the http.post body response directly on some of the Gitea function (ex: remote_create_repo):

Gitea.Http.post(url, params)

To make it easier for the application using Gitea we could parse the message of the body, or the status code and return either {:ok, value} or {:error, reason}

the reason could be an atom (:existing_repo if we tried to create an existing repository) or we could create an Exception module and return this module. The idea is for the client application to be able to raise the exception.
see: https://elixir-lang.org/getting-started/try-catch-and-rescue.html#errors

@SimonLab SimonLab added the enhancement New feature or request label May 20, 2022
@nelsonic nelsonic added help wanted Extra attention is needed priority-2 Second highest priority, should be worked on as soon as the Priority-1 issues are finished T1h Time Estimate 1 Hour labels May 20, 2022
@SimonLab
Copy link
Member Author

SimonLab commented May 25, 2022

Now that we have a way to return an exception error (see #25 ) we can use it to make sure to return the {:ok, body} or {:error, Gitea.Error} tuples.

Currently if we tried to create a repository in a non existent organisation we have the following response:

{:ok,
 %{
   message: "org does not exist [id: 0, name: doesn't+exist]",
   url: "https://gitea-server/api/swagger"
 }}

http poison response when failing to create the repository:

%HTTPoison.Response{
  body: "{\"message\":\"org does not exist [id: 0, name: wrongorg]\",\"url\":\"https://gitea-server.fly.dev/api/swagger\"}\n",
  request: %HTTPoison.Request{
    body: "{\"auto_init\":true,\"description\":\"repo1\",\"name\":\"repo1\",\"private\":false}",
    headers: [
      {"Accept", "application/json"},
      {"Authorization", "token ---"},
      {"Content-Type", "application/json"}
    ],
    method: :post,
    options: [],
    params: %{},
    url: "https://gitea-server.fly.dev/api/v1/org/wrongorg/repos"
  },
  request_url: "https://gitea-server.fly.dev/api/v1/org/wrongorg/repos",
  status_code: 422
}

status code 422 Unprocessable Entity

on success:

%HTTPoison.Response{
  body: "{\"id\":122,\"owner\":{\"id...},
  request: %HTTPoison.Request{
    body: "{\"auto_init\":true,\"description\":\"repo1\",\"name\":\"repo1\",\"private\":false}",
    headers: [
      {"Accept", "application/json"},
      {"Authorization", "token ---"},
      {"Content-Type", "application/json"}
    ],
    method: :post,
    options: [],
    params: %{},
    url: "https://gitea-server.fly.dev/api/v1/org/debugapp/repos"
  },
  request_url: "https://gitea-server.fly.dev/api/v1/org/debugapp/repos",
  status_code: 201
}

status code 201 Created

Another example of "failed" request but where Gitea returns :ok, is when remote_repo_create is attempting to create a repository already created:

%HTTPoison.Response{
  body: "{\"message\":\"The repository with the same name already exists.\",\"url\":\"https://gitea-server.fly.dev/api/swagger\"}\n",
  headers: [],
  request: %HTTPoison.Request{
    body: "{\"auto_init\":true,\"description\":\"repo1\",\"name\":\"repo1\",\"private\":false}",
    headers: [
      {"Accept", "application/json"},
      {"Authorization", "token ---"},
      {"Content-Type", "application/json"}
    ],
    method: :post,
    options: [],
    params: %{},
    url: "https://gitea-server.fly.dev/api/v1/org/debugapp/repos"
  },
  request_url: "https://gitea-server.fly.dev/api/v1/org/debugapp/repos",
  status_code: 409
}

Status code 409 conflict

@nelsonic
Copy link
Member

The code for this was included in #27 and published to https://hex.pm/packages/gitea/1.0.7 📦 🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request help wanted Extra attention is needed priority-2 Second highest priority, should be worked on as soon as the Priority-1 issues are finished T1h Time Estimate 1 Hour
Projects
None yet
Development

No branches or pull requests

2 participants