# Service

All services are contained at `rsc/services/*.lua`

I also recommend to check [promise documentation](https://github.com/dankmolot/gm_promise) since services heavily use promises

## RSC.RECOMMENDED\_SERVICE

```lua
RSC.RECOMMENDED_SERVICE = "gmod.express"
```

Constant that have name of recommended service. Used as fallback in [CaptureRequest](https://pika-soft.gitbook.io/rsc/api/classes/capturerequest) if specified service wans't found.

## RSC.Service.New(...)

<pre class="language-rust"><code class="lang-rust"><strong>Service RSC.Service.New( name: str )
</strong></code></pre>

Creates new `Service` object

```lua
local service = RSC.Service.New("my_service")

-- Change service functions here
function service:Upload(data, quality, preparedata)
    -- ...
end
service.Upload = promise.Async(service.Upload)

-- And register service
service:Register() -- After this we can get service by calling RSC.Service.Get("my_service")
```

## RSC.Service.Get(...)

```rust
Service/nil RSC.Service.Get( name: str )
```

Returns registered service by name if it is exists

## :GetName()

```rust
str Service:GetName()
```

Returns service name specified in [RSC.Service.New](#rsc.service.new).

You shouldn't overwrite this function.

## :GetDownloadURL(...)

```rust
Promise<str> / str Service:GetDownloadURL( data: str, prepareData?: str )
```

This functions must return url to download uploaded image. `data` is given by [Service:Upload(...)](#upload-...) and `prepareData` is given by [Service:Prepare(...)](#prepare)

## :Ping()

```rust
Promise<bool> Service:Ping()
```

This function checks if service available at current moment.

```lua
-- Example of implementing this function
function service:Ping()
    local ok, req = promise.HTTP({ url = "https://yourservice.com", timeout = 5 }):SafeAwait()
    return ok and req.code == 200
end
service.Ping = promise.Async(service.Ping)
```

## :Prepare()

```rust
Promise<str/nil> Service:Prepare()
```

This function called on serverside before initiating capture process on victim's side. Must return string that contains prepare data for upload and download functions. If prepare data isn't needed, then just return `Promise<nil>` or don't overwrite this function.

## :Upload(...)

```rust
Promise<str> Service:Upload( imageData: str, quality: number, prepareData?: str )
```

This function must upload `imageData`. `quality` argument can be parsed with [RSC.ParseQuality](https://pika-soft.gitbook.io/rsc/utilities#rsc.parsequality). `prepareData` argument given by [Service:Prepare](#service-prepare)

{% hint style="warning" %}
This function must be implemented
{% endhint %}

```lua
-- Example of implementing Service:Upload
function service:Upload(imageData, quality, prepareData)
    local ok, res = promise.HTTP({
        url = "https://myservice.com/upload",
        method = "POST",
        body = imageData,
    }):SafeAwait() -- Safely await our function
    
    -- Check if upload was successful
    if not ok or res.code ~= 200 then 
        return promise.Reject("failed to upload") -- do not use error("...")
    end
    
    -- Return service response (like file id)
    return res.body
end
service.Upload = promise.Async(service.Upload) -- Make this function async
```

## :Download(...)

<pre class="language-rust"><code class="lang-rust"><strong>Promise&#x3C;str> Service:Download( uploadData: str, quality: number, prepareData?: str )
</strong></code></pre>

This function must download image. Argument `uploadData` given by [Service:Upload](#service-upload). `quality` argument can be parsed with [RSC.ParseQuality](https://pika-soft.gitbook.io/rsc/utilities#rsc.parsequality). `prepareData` argument given by [Service:Prepare](#service-prepare)

{% hint style="warning" %}
This function must be implemented
{% endhint %}

```lua
-- Example of implementing Service:Download
function service:Download(uploadData, quality, prepareData)
    local ok, res = promise.HTTP({
        -- Our Service:GetDownloadURL(...) must return our download url
        -- Like: "https://myservice.com/download/" .. uploadData
        url = self:GetDownloadURL(uploadData, prepareData),
        method = "GET",
    }):SafeAwait() -- Safely await our function
    
    -- Check if download was successful
    if not ok or res.code ~= 200 then 
        return promise.Reject("failed to download") -- do not use error("...")
    end
    
    -- Return downloaded image
    return res.body
end
service.Download = promise.Async(service.Download) -- Make this function async
```

## :Register()

```rust
Service:Register()
```

Registers your service by the name you specified in [RSC.Service.New](#rsc.service.new). Registered service can accessed by [RSC.Service.Get](#rsc.service.get) or directly by accessing `RSC.Service.Services[name]`.&#x20;

You shouldn't overwrite this function
