|
| 1 | +# API Clients |
| 2 | + |
| 3 | +This directory is for API clients. |
| 4 | + |
| 5 | +Jumpstart Pro includes an API client for working with JSON APIs. It handles several things for you: |
| 6 | + |
| 7 | +* The Authorization header with API token |
| 8 | +* Content-Type JSON header |
| 9 | +* JSON response bodies parsed automatically |
| 10 | +* Raises errors on 4XX or 5XX responses |
| 11 | + |
| 12 | +Why would you need this? |
| 13 | +Often times Rubygems for APIs fall out of date or go unmaintained. Managing your own integration allows you to only implement the endpoints you need and easily maintain your integration. |
| 14 | + |
| 15 | +### Usage |
| 16 | + |
| 17 | +Use the generator to create a new API client: |
| 18 | + |
| 19 | +```bash |
| 20 | +rails g api_client Sendfox |
| 21 | +``` |
| 22 | + |
| 23 | +This will generate `app/clients/sendfox_client.rb` |
| 24 | + |
| 25 | +Edit the API client file to add the base URI and define your API endpoints |
| 26 | + |
| 27 | +Here are some example API endpoints for the Sendfox API implemented with the API client: |
| 28 | + |
| 29 | +```ruby |
| 30 | +class SendfoxClient < ApplicationClient |
| 31 | + BASE_URI = "https://api.sendfox.com" |
| 32 | + |
| 33 | + def me |
| 34 | + get("/me") |
| 35 | + end |
| 36 | + |
| 37 | + def lists |
| 38 | + get("/lists") |
| 39 | + end |
| 40 | + |
| 41 | + def list(id) |
| 42 | + get("/lists/#{id}") |
| 43 | + end |
| 44 | + |
| 45 | + def create_list(name:) |
| 46 | + post("/lists", name: name) |
| 47 | + end |
| 48 | + |
| 49 | + def remove_contact(list_id:, contact_id:) |
| 50 | + delete("/lists/#{list_id}/contacts/#{contact_id}") |
| 51 | + end |
| 52 | +end |
| 53 | +``` |
| 54 | + |
| 55 | +### ApplicationClient |
| 56 | + |
| 57 | +`ApplicationClient` is the base class and defines helpers for making HTTP requests and handles authorization headers and parsing JSON responses. |
| 58 | + |
| 59 | +#### HTTP methods |
| 60 | + |
| 61 | +There are helper methods for the standard HTTP request types for APIs: |
| 62 | + |
| 63 | +`get` |
| 64 | +`post` |
| 65 | +`patch` |
| 66 | +`put` |
| 67 | +`delete` |
| 68 | + |
| 69 | +#### Overrides |
| 70 | + |
| 71 | +You can override the methods to match the APIs you're integrating with. |
| 72 | + |
| 73 | +For example, you can override the default headers and query params. This example could be used to connect to an XML API that uses a query parameter to authenticate. |
| 74 | + |
| 75 | +```ruby |
| 76 | +class XmlExampleClient < ApplicationClient |
| 77 | + def default_headers |
| 78 | + { |
| 79 | + "Content-Type" => "application/xml" |
| 80 | + } |
| 81 | + end |
| 82 | + |
| 83 | + def default_query_params |
| 84 | + { |
| 85 | + token: token |
| 86 | + } |
| 87 | + end |
| 88 | +end |
| 89 | +``` |
| 90 | + |
| 91 | +### Multipart Form Data |
| 92 | + |
| 93 | +When working with APIs that require file uploads or multipart form data, the `ApplicationClient` can also handle these types of requests. You can use the `Net::HTTPHeader#set_form` method to set form data, including file uploads. |
| 94 | + |
| 95 | +#### Usage |
| 96 | + |
| 97 | +To send a multipart form data request, you need to pass the `form_data` parameter when calling the `post`, `patch`, or `put` methods in your custom API client. The `form_data` parameter should be an Enumerable containing field names and their values. You can use a hash or an array for `form_data`. |
| 98 | + |
| 99 | +For example, to upload a file: |
| 100 | + |
| 101 | +```ruby |
| 102 | +class MyApiClient < ApplicationClient |
| 103 | + BASE_URI = "https://api.example.com" |
| 104 | + |
| 105 | + def upload_file(file) |
| 106 | + form_data = { |
| 107 | + "field1" => "value1", |
| 108 | + "file" => file |
| 109 | + } |
| 110 | + |
| 111 | + post("/upload", form_data: form_data) |
| 112 | + end |
| 113 | +end |
| 114 | +``` |
| 115 | + |
| 116 | +In this example, file should be a File or IO-like object. |
| 117 | + |
| 118 | +If you need to set additional options for the file upload, such as the filename or content type, you can include them in the form data like this: |
| 119 | + |
| 120 | + |
| 121 | +```rb |
| 122 | +form_data = { |
| 123 | + "field1" => "value1", |
| 124 | + "file" => [file, { filename: "custom_filename.txt", content_type: "text/plain" }] |
| 125 | +} |
| 126 | +``` |
0 commit comments