1

I have a couple of general questions which I can't find the answer online regarding internal api.

I understand how an external API works, for example, if I have an endpoint in my application, then an external source can make an API call to that endpoint e.g. GET, and JSON is returned.

However I don't understand the difference between if I were to call the same endpoint within my own application (through a button click which would just be a regular get request). Is this considered an internal API call, or is it just a regular request and it changes to external based on the context?

Is the returned value the only difference? e.g. external API is JSON, and internal would return code back to a view page?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
b.herring
  • 563
  • 2
  • 18
  • The response format depends on the `Content-Type` of the request. If you want more insight you can search for how `respond_to` works. – Jan Vítek Mar 06 '23 at 15:19

2 Answers2

1

When you click a button/link it usually sends an html request, which is defined by Accept header:

def show
  puts request.headers["Accept"]
  # => "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
  #     ^^^^^^^^^
  #     it's an html request. it's up to you (and rails) to respond with html
  #     or ignore it and render something else.
end

By default, Rails will render show.html.erb template and set Content-Type header to text/html.

Then you add show.json.jbuilder. Now you can ask for a json response instead of an html:

// with javascript
fetch("/models/1" , { headers: { Accept: "application/json" } })
  .then(response => response.json())
  .then(json => console.log(json)) // do something with the response

// and in controller you can see
// request.headers["Accept"] # => "application/json"

It's a json request so rails will render a json template.

Rails also gives you a way to render a particular response without using headers. Just add .json to the url. Accept: text/html in this case is ignored by rails and it will render a json template.

I don't remember any internal/external api concepts in rails, just a request and response.

If you need to add more logic in the controller to handle different types of requests use respond_to method:

def show
  respond_to do |format|
    format.html { render :different_template }
    format.json { render json: {error: "i'm not an api."} }
  end
end
Alex
  • 16,409
  • 6
  • 40
  • 56
0

According to the Rails Guides, the internal API consists mainly of private methods, although there is a way to make otherwise public methods internal. This is intended to be temporary, however:

When writing documentation for Rails, it's important to understand the difference between public user-facing API vs internal API.

Rails, like most libraries, uses the private keyword from Ruby for defining internal API. However, public API follows a slightly different convention. Instead of assuming all public methods are designed for user consumption, Rails uses the :nodoc: directive to annotate these kinds of methods as internal API.

I recommend consulting the Method Visibility section of the API Documentation Guidelines for further clarification and an example.

therealrodk
  • 386
  • 2
  • 10