> For the complete documentation index, see [llms.txt](https://walkinthepark.gitbook.io/microservice-api/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://walkinthepark.gitbook.io/microservice-api/reference/intro-making-requests/send-response-microservice.md).

# Send response: Microservice

## Example for making your first request

In this class, I will demonstrate the usage of all four HTTP methods. If you have previous experience with microservices, the material should be easily comprehensible.

{% tabs %}
{% tab title="service/player.service.ts" %}
{% hint style="info" %}
**Information**

This code fetches player data from MySQL for Spigot via a GET request with UUID as parameter. The SQL query selects the player record and returns first result as player data. Additional functionality such as inserting records recommended.
{% endhint %}

Service files are usually nested in the **/service(s)/\*** folder in your app. Files that end \
with **.service.ts**, hold the logic for when they get requested from our Spigot server.

<pre class="language-typescript" data-title="service/player.service.ts"><code class="lang-typescript">import { Router } from '@spigotmc/microservice';
import { connection } from '..';

/**
 * Create a new router for this service.
 */
const service = new Router();

/**
 * This is only one example of four methods
 */
service.get('/load', (query) => {
  /**
   * The UUID of the player gets passed through
   * the query because this is a GET request.
   */
  const uuid: string = query.uuid;

  /**
   * This is the query that gets executed.
   */
  const sql = `SELECT * FROM players WHERE uuid='${<a data-footnote-ref href="#user-content-fn-1">uuid</a>}'`;

  /**
   * Execute the query and wait for the result.
   */
  return new Promise((resolve, reject) => {
    connection.query(sql, (error, results) => {
      /**
       * Reject the promise if an error occured.
       */
      if (error) return reject(error);
      /**
       * Resolve the promise with player's data.
       */
      resolve(<a data-footnote-ref href="#user-content-fn-2">results[0]</a>);
    });
  });
});

/**
 * Other endpoints like `/coin_update`, etc.,
 * should be implemented in this same file.
 */
export { service as playerService };
</code></pre>

{% endtab %}

{% tab title="PlayerRepository.java" %}
{% code title="PlayerRepository.java" %}

```java
import micro.api.service.*;
import micro.api.payload.*;

public class PlayerRepository extends ServiceEndpoint {
   /**
    * This points to `http://localhost:5000/api/player`
    */
    public PlayerRepository() {
       super("/player");
    }

    /**
     * Example of GET request, get player's data.
     * @return a new PlayerClient object.
     */
    public PlayerClient loadPlayer(UUID uuid) {
        Query query = new Query();
        query.add("uuid", uuid.toString());

        return super.get("/load", PlayerClient.class, query);
    }
    
    /**
     * Example of POST request, update player's coins.
     * @return The updated PlayerClient object.
     */
    public PlayerClient addFriend(UUID uuid, UUID target) {
        Body body = new Body();
        body.add("uuid", uuid.toString());
        body.add("target", target.toString());

        return super.post("/friend_add", PlayerClient.class, body);
    }
    
    /**
     * Example of PUT request, update player's coins.
     * @return The updated PlayerClient object.
     */
    public PlayerClient updateCoins(UUID uuid, int amount) {
        Body body = new Body();
        body.add("uuid", uuid.toString());
        body.add("coins", amount);

        return super.put("/coin_update", PlayerClient.class, body);
    }
    
    /**
     * Example of DELETE request, delete player data.
     * @return The deleted PlayerClient object.
     */
    public PlayerClient deletePlayer(UUID uuid) {
        Query query = new Query();
        query.add("uuid", uuid.toString());
        
        return super.delete("/delete", PlayerClient.class, query);
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="PlayerClient.java" %}

<pre class="language-java" data-title="PlayerClient.java"><code class="lang-java">public class PlayerClient {
   /**
    * These fields will get initialized when
    * loading the player from the service.
    */
   private String uuid;
   private int level;
   private <a data-footnote-ref href="#user-content-fn-3">Rank</a> rank;
   private int coins;
   private List&#x3C;String> friends;

   public UUID getUUID() { 
      return UUID.fromString(uuid); 
   }

   public int getLevel() {
       return level;
   }

   public <a data-footnote-ref href="#user-content-fn-4">Rank</a> getRank() {
      return rank;
   }

   public int getCoins() {
      return coins;
   }

   public List&#x3C;String> getFriends() {
       return friends;
   }
} 
</code></pre>

{% endtab %}
{% endtabs %}

Once you made your first service file, you should import this in the `/api` router.

{% code title="routes.ts" %}

```typescript
import { Router } from '@spigotmc/microservice';
import { playerService } from './services/player.service';

/**
 * Create a new Router from the Microservice library.
 */ 
const router = new Router();

/**
 * Add any sub-routes to the main router.
 */
router.use('/player', playerService);

/**
 * Export the router so that we can use
 * it in the main class where you decl-
 * ared the microservice.
 */ 
export default router;
```

{% endcode %}

[^1]: `584fb027-57c8-456c-84d2-552e1d508a6a`

[^2]: `{` \
    &#x20;  `"id": 1,`

    &#x20;  `"uuid": "584fb027-57c8-456c-84d2-552e1d508a6a",`

    &#x20;  `"level": 10,`

    &#x20;  `"rank": "ADMIN",`

    &#x20;  `"coins": 500`

    `}`

[^3]: <pre class="language-java"><code class="lang-java"><strong>public enum Rank {
    </strong>    PLAYER,
        HELPER,
        MODERATOR,
        ADMIN,
        OWNER,
    }
    </code></pre>

[^4]: ```java
    public enum Rank {
        PLAYER,
        HELPER,
        MODERATOR,
        ADMIN,
        OWNER,
    }
    ```
