Skip to content

Examples

AQaddora edited this page May 3, 2025 · 3 revisions

Examples

This guide provides practical examples of using REST Express in common scenarios. Each example includes both the Postman setup and the resulting Unity implementation.

Table of Contents

User Management

Postman Collection Setup

{
  "item": [
    {
      "name": "User Management",
      "item": [
        {
          "name": "Create User",
          "request": {
            "method": "POST",
            "url": "{{base_url}}/users",
            "body": {
              "mode": "formdata",
              "formdata": [
                {
                  "key": "username",
                  "value": "simple_yet_efficient",
                  "type": "text"
                },
                {
                  "key": "email",
                  "value": "[email protected]",
                  "type": "text"
                },
                {
                  "key": "password",
                  "value": "123456",
                  "type": "text"
              }
              ]
            }
          }
        },
        {
          "name": "Get User Profile",
          "request": {
            "method": "GET",
            "url": "{{base_url}}/users/{{userId}}"
          }
        }
      ]
    }
  ]
}

Unity Implementation

public class UserManager : MonoBehaviour
{
    private void Start()
    {
        // Create a new user
        ApiClient.Instance.CreateUser(
            onSuccess: (response) => {
                Debug.Log($"User created: {response}");
                // After successful creation, get the user profile
                GetUserProfile();
            },
            onError: (error) => {
                Debug.LogError($"User creation failed: {error}");
            }
        );
    }

    private void GetUserProfile()
    {
        ApiClient.Instance.GetUserProfile(
            onSuccess: (response) => {
                Debug.Log($"User profile: {response}");
                // Process user profile data
            },
            onError: (error) => {
                Debug.LogError($"Failed to get user profile: {error}");
            }
        );
    }
}

Game Progress

Postman Collection Setup

{
  "item": [
    {
      "name": "Game Progress",
      "item": [
        {
          "name": "Save Level Progress",
          "request": {
            "method": "POST",
            "url": "{{base_url}}/progress/level",
            "body": {
              "mode": "formdata",
              "formdata": [
                {
                  "key": "levelId",
                  "value": "{{levelId}}",
                  "type": "text"
                },
                {
                  "key": "score",
                  "value": "1000",
                  "type": "text"
                },
                {
                  "key": "stars",
                  "value": "3",
                  "type": "text"
                },
                {
                  "key": "timeSpent",
                  "value": "120",
                  "type": "text"
                }
              ]
            }
          }
        },
        {
          "name": "Get Level Status",
          "request": {
            "method": "GET",
            "url": "{{base_url}}/progress/level/{{levelId}}"
        }
    }
      ]
    }
  ]
}

Unity Implementation

public class GameProgressManager : MonoBehaviour
{
    public void SaveLevelProgress(int score, int stars, float timeSpent)
    {
        ApiClient.Instance.SaveLevelProgress(
            score: score.ToString(),
            stars: stars.ToString(),
            timeSpent: timeSpent.ToString(),
            onSuccess: (response) => {
                Debug.Log($"Level progress saved: {response}");
            },
            onError: (error) => {
                Debug.LogError($"Failed to save progress: {error}");
            }
        );
    }

    public void GetCurrentLevelStatus()
    {
        ApiClient.Instance.GetLevelStatus(
            onSuccess: (response) => {
                Debug.Log($"Level status: {response}");
                // Update UI with level status
            },
            onError: (error) => {
                Debug.LogError($"Failed to get level status: {error}");
            }
        );
    }
}

Media Upload

Postman Collection Setup

{
  "name": "Upload Media Files",
  "request": {
    "method": "POST",
    "url": "{{base_url}}/media/upload",
    "body": {
      "mode": "formdata",
      "formdata": [
        {
          "key": "audio",
          "type": "file",
          "src": "/path/to/audio.mp3"
        },
        {
          "key": "image",
          "type": "file",
          "src": "/path/to/image.jpg"
        },
        {
          "key": "description",
          "value": "Media upload description",
          "type": "text"
        }
      ]
    }
  }
}

Unity Implementation

public class MediaUploadManager : MonoBehaviour
{
    public void UploadMedia(string audioPath, string imagePath, string description)
        {
        ApiClient.Instance.UploadMediaFiles(
            audio: audioPath,
            image: imagePath,
            description: description,
            onSuccess: (response) => {
                Debug.Log($"Media uploaded successfully: {response}");
                // Handle successful upload
            },
            onError: (error) => {
                Debug.LogError($"Media upload failed: {error}");
                // Handle upload error
            }
        );
    }
}

Error Handling

Global Error Handler

public class ApiErrorHandler : MonoBehaviour
{
    public static void HandleError(string error)
    {
        // Parse the error response
        try
        {
            var errorJson = JsonUtility.FromJson<ErrorResponse>(error);
            switch (errorJson.status)
        {
            case 401:
                    // Handle unauthorized
                break;
            case 403:
                    // Handle forbidden
                break;
            case 404:
                    // Handle not found
                break;
            default:
                    // Handle other errors
                break;
            }
        }
        catch
        {
            // Handle parsing error
            Debug.LogError($"Error parsing response: {error}");
        }
    }
}

// Usage in API calls
ApiClient.Instance.GetUserProfile(
    onSuccess: (response) => {
        // Handle success
    },
    onError: (error) => {
        ApiErrorHandler.HandleError(error);
}
);

These examples demonstrate the core functionality available in REST Express. For more specific use cases or custom implementations, check our Best Practices guide or contact support. These examples demonstrate common use cases and best practices when using REST Express in a Unity project. For more specific examples or custom scenarios, check our Best Practices guide or contact support.

Clone this wiki locally