{
  "info": {
    "name": "MEGOGO Partner API",
    "description": "Requests of the MEGOGO Content API and Billing API.\n\nPut your values (partner_id, public_key, private_key, salt, service_id) in a Postman environment. Do not put keys in the collection and do not share an environment with keys.\n\nGuide: https://megogo-partner-docs.pages.dev/reference/postman/",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "type": "text/javascript",
        "exec": [
          "// MEGOGO partner collection: signature of the Content API requests.",
          "// sign = md5(all \"name=value\" pairs in the URL order + private key) + public key",
          "// The Billing API requests are not signed.",
          "const cryptoJs = require('crypto-js');",
          "",
          "// The authorization token for /auth/by_partners: md5(user_id + partner_id + salt).",
          "const userId = pm.variables.get('user_id');",
          "const partnerId = pm.variables.get('partner_id');",
          "const salt = pm.variables.get('salt');",
          "if (userId && partnerId && salt) {",
          "    pm.variables.set('auth_token', cryptoJs.MD5(userId + partnerId + salt).toString());",
          "}",
          "",
          "const url = pm.request.url;",
          "if (pm.variables.replaceIn(url.toString()).startsWith(pm.variables.get('content_api'))) {",
          "    url.query.remove(q => q.key === 'sign');",
          "    const joined = url.query.all()",
          "        .filter(q => !q.disabled)",
          "        .map(q => q.key + '=' + pm.variables.replaceIn(q.value || ''))",
          "        .join('');",
          "    const sign = cryptoJs.MD5(joined + pm.variables.get('private_key')).toString()",
          "        + pm.variables.get('public_key');",
          "    url.query.add({ key: 'sign', value: sign });",
          "}"
        ]
      }
    }
  ],
  "variable": [
    {
      "key": "content_api",
      "value": "https://api.megogo.net/v1",
      "description": "Base URL of the Content API"
    },
    {
      "key": "billing_api",
      "value": "https://billing.megogo.net",
      "description": "Base URL of the Billing API"
    },
    {
      "key": "partner_id",
      "value": "",
      "description": "Your partner ID. Put it in your environment"
    },
    {
      "key": "public_key",
      "value": "",
      "description": "Public key of the platform. Put it in your environment"
    },
    {
      "key": "private_key",
      "value": "",
      "description": "Private key of the platform. Put it only in your environment, as a secret"
    },
    {
      "key": "salt",
      "value": "",
      "description": "Salt for /auth/by_partners. Put it only in your environment, as a secret"
    },
    {
      "key": "service_id",
      "value": "",
      "description": "Service ID of the subscription"
    },
    {
      "key": "user_id",
      "value": "postman-test-001",
      "description": "User identifier in your system. Use a test identifier"
    },
    {
      "key": "lang",
      "value": "en",
      "description": "Language of the texts: en, uk, ru and others"
    },
    {
      "key": "video_id",
      "value": "6680",
      "description": "ID of a video"
    },
    {
      "key": "tariff_id",
      "value": "",
      "description": "tariff_id of a TVOD or DTO offer from /video/info"
    },
    {
      "key": "access_token",
      "value": "",
      "description": "Set by /auth/by_partners"
    },
    {
      "key": "rememberme_token",
      "value": "",
      "description": "Set by /auth/by_partners"
    },
    {
      "key": "auth_token",
      "value": "",
      "description": "Calculated by the collection script"
    }
  ],
  "item": [
    {
      "name": "Content API",
      "description": "Signed requests. The collection script adds `sign` to each request.",
      "item": [
        {
          "name": "Configuration",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('result is ok', () => pm.expect(pm.response.json().result).to.eql('ok'));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "description": "Categories, genres and the country (`geo`) of the request.",
            "url": {
              "raw": "{{content_api}}/configuration?lang={{lang}}",
              "host": [
                "{{content_api}}"
              ],
              "path": [
                "configuration"
              ],
              "query": [
                {
                  "key": "lang",
                  "value": "{{lang}}"
                }
              ]
            }
          }
        },
        {
          "name": "Authorize a user (by_partners)",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "// Keep the tokens of the user for the next requests.",
                  "const body = pm.response.json();",
                  "pm.test('result is ok', () => pm.expect(body.result).to.eql('ok'));",
                  "if (body.result === 'ok') {",
                  "    pm.collectionVariables.set('access_token', body.data.tokens.access_token);",
                  "    pm.collectionVariables.set('rememberme_token', body.data.tokens.rememberme_token);",
                  "}"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "description": "Gets an access token for the user. The script saves `access_token` and `rememberme_token`.",
            "url": {
              "raw": "{{content_api}}/auth/by_partners?isdn={{user_id}}&partner_key={{partner_id}}&token={{auth_token}}",
              "host": [
                "{{content_api}}"
              ],
              "path": [
                "auth",
                "by_partners"
              ],
              "query": [
                {
                  "key": "isdn",
                  "value": "{{user_id}}"
                },
                {
                  "key": "partner_key",
                  "value": "{{partner_id}}"
                },
                {
                  "key": "token",
                  "value": "{{auth_token}}"
                }
              ]
            }
          }
        },
        {
          "name": "Refresh the access token",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "// Keep the tokens of the user for the next requests.",
                  "const body = pm.response.json();",
                  "pm.test('result is ok', () => pm.expect(body.result).to.eql('ok'));",
                  "if (body.result === 'ok') {",
                  "    pm.collectionVariables.set('access_token', body.data.tokens.access_token);",
                  "    pm.collectionVariables.set('rememberme_token', body.data.tokens.rememberme_token);",
                  "}"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "description": "Gets a new access token with the remember-me token.",
            "url": {
              "raw": "{{content_api}}/auth/refresh?rememberme_token={{rememberme_token}}",
              "host": [
                "{{content_api}}"
              ],
              "path": [
                "auth",
                "refresh"
              ],
              "query": [
                {
                  "key": "rememberme_token",
                  "value": "{{rememberme_token}}"
                }
              ]
            }
          }
        },
        {
          "name": "Video list",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('result is ok', () => pm.expect(pm.response.json().result).to.eql('ok'));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "description": "Videos of a category.",
            "url": {
              "raw": "{{content_api}}/video?category_id=16&paging_strategy=token&limit=20&lang={{lang}}",
              "host": [
                "{{content_api}}"
              ],
              "path": [
                "video"
              ],
              "query": [
                {
                  "key": "category_id",
                  "value": "16"
                },
                {
                  "key": "paging_strategy",
                  "value": "token"
                },
                {
                  "key": "limit",
                  "value": "20"
                },
                {
                  "key": "lang",
                  "value": "{{lang}}"
                }
              ]
            }
          }
        },
        {
          "name": "Video details",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('result is ok', () => pm.expect(pm.response.json().result).to.eql('ok'));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "description": "Details of a video for the user: `is_available`, `delivery_rules`, `purchase_info`.",
            "url": {
              "raw": "{{content_api}}/video/info?id={{video_id}}&lang={{lang}}&access_token={{access_token}}",
              "host": [
                "{{content_api}}"
              ],
              "path": [
                "video",
                "info"
              ],
              "query": [
                {
                  "key": "id",
                  "value": "{{video_id}}"
                },
                {
                  "key": "lang",
                  "value": "{{lang}}"
                },
                {
                  "key": "access_token",
                  "value": "{{access_token}}"
                }
              ]
            }
          }
        },
        {
          "name": "Seasons and episodes",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('result is ok', () => pm.expect(pm.response.json().result).to.eql('ok'));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "description": "Seasons and episodes of a series.",
            "url": {
              "raw": "{{content_api}}/video/episodes?id={{video_id}}&lang={{lang}}",
              "host": [
                "{{content_api}}"
              ],
              "path": [
                "video",
                "episodes"
              ],
              "query": [
                {
                  "key": "id",
                  "value": "{{video_id}}"
                },
                {
                  "key": "lang",
                  "value": "{{lang}}"
                }
              ]
            }
          }
        },
        {
          "name": "Search",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('result is ok', () => pm.expect(pm.response.json().result).to.eql('ok'));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "description": "Search by text.",
            "url": {
              "raw": "{{content_api}}/search?text=king&limit=20&lang={{lang}}",
              "host": [
                "{{content_api}}"
              ],
              "path": [
                "search"
              ],
              "query": [
                {
                  "key": "text",
                  "value": "king"
                },
                {
                  "key": "limit",
                  "value": "20"
                },
                {
                  "key": "lang",
                  "value": "{{lang}}"
                }
              ]
            }
          }
        },
        {
          "name": "Stream",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('result is ok', () => pm.expect(pm.response.json().result).to.eql('ok'));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "description": "The stream URL (`src`) for the user. Do not keep `src`: request it for each playback.",
            "url": {
              "raw": "{{content_api}}/stream?video_id={{video_id}}&lang={{lang}}&access_token={{access_token}}",
              "host": [
                "{{content_api}}"
              ],
              "path": [
                "stream"
              ],
              "query": [
                {
                  "key": "video_id",
                  "value": "{{video_id}}"
                },
                {
                  "key": "lang",
                  "value": "{{lang}}"
                },
                {
                  "key": "access_token",
                  "value": "{{access_token}}"
                }
              ]
            }
          }
        }
      ]
    },
    {
      "name": "Billing API",
      "description": "Requests without a signature. MEGOGO accepts them only from the IP addresses of your list.",
      "item": [
        {
          "name": "Subscribe",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('successful', () => pm.expect(pm.response.json().successful).to.eql(true));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "description": "Starts the subscription `service_id` for the user.",
            "url": {
              "raw": "{{billing_api}}/partners/{{partner_id}}/subscription/subscribe?userId={{user_id}}&serviceId={{service_id}}",
              "host": [
                "{{billing_api}}"
              ],
              "path": [
                "partners",
                "{{partner_id}}",
                "subscription",
                "subscribe"
              ],
              "query": [
                {
                  "key": "userId",
                  "value": "{{user_id}}"
                },
                {
                  "key": "serviceId",
                  "value": "{{service_id}}"
                }
              ]
            }
          }
        },
        {
          "name": "User services",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('identifier', () => pm.expect(pm.response.json().identifier).to.eql(pm.variables.get('user_id')));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "description": "Subscriptions (`svod`), rents (`tvod`) and purchases (`dto`) of the user.",
            "url": {
              "raw": "{{billing_api}}/partners/{{partner_id}}/user/subscriptions?identifier={{user_id}}",
              "host": [
                "{{billing_api}}"
              ],
              "path": [
                "partners",
                "{{partner_id}}",
                "user",
                "subscriptions"
              ],
              "query": [
                {
                  "key": "identifier",
                  "value": "{{user_id}}"
                }
              ]
            }
          }
        },
        {
          "name": "Suspend",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('successful', () => pm.expect(pm.response.json().successful).to.eql(true));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "description": "Stops the access until `resume`.",
            "url": {
              "raw": "{{billing_api}}/partners/{{partner_id}}/subscription/suspend?userId={{user_id}}&serviceId={{service_id}}",
              "host": [
                "{{billing_api}}"
              ],
              "path": [
                "partners",
                "{{partner_id}}",
                "subscription",
                "suspend"
              ],
              "query": [
                {
                  "key": "userId",
                  "value": "{{user_id}}"
                },
                {
                  "key": "serviceId",
                  "value": "{{service_id}}"
                }
              ]
            }
          }
        },
        {
          "name": "Resume",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('successful', () => pm.expect(pm.response.json().successful).to.eql(true));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "description": "Starts the access again.",
            "url": {
              "raw": "{{billing_api}}/partners/{{partner_id}}/subscription/resume?userId={{user_id}}&serviceId={{service_id}}",
              "host": [
                "{{billing_api}}"
              ],
              "path": [
                "partners",
                "{{partner_id}}",
                "subscription",
                "resume"
              ],
              "query": [
                {
                  "key": "userId",
                  "value": "{{user_id}}"
                },
                {
                  "key": "serviceId",
                  "value": "{{service_id}}"
                }
              ]
            }
          }
        },
        {
          "name": "Unsubscribe",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('successful', () => pm.expect(pm.response.json().successful).to.eql(true));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "description": "Stops the subscription.",
            "url": {
              "raw": "{{billing_api}}/partners/{{partner_id}}/subscription/unsubscribe?userId={{user_id}}&serviceId={{service_id}}",
              "host": [
                "{{billing_api}}"
              ],
              "path": [
                "partners",
                "{{partner_id}}",
                "subscription",
                "unsubscribe"
              ],
              "query": [
                {
                  "key": "userId",
                  "value": "{{user_id}}"
                },
                {
                  "key": "serviceId",
                  "value": "{{service_id}}"
                }
              ]
            }
          }
        },
        {
          "name": "Buy a video (buyMovie)",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('successful', () => pm.expect(pm.response.json().successful).to.eql(true));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "description": "Records a rent (TVOD) or a purchase (DTO). There is no request to cancel it.",
            "url": {
              "raw": "{{billing_api}}/partners/{{partner_id}}/buyMovie?userId={{user_id}}&videoId={{video_id}}&serviceId={{tariff_id}}",
              "host": [
                "{{billing_api}}"
              ],
              "path": [
                "partners",
                "{{partner_id}}",
                "buyMovie"
              ],
              "query": [
                {
                  "key": "userId",
                  "value": "{{user_id}}"
                },
                {
                  "key": "videoId",
                  "value": "{{video_id}}"
                },
                {
                  "key": "serviceId",
                  "value": "{{tariff_id}}"
                }
              ]
            }
          }
        }
      ]
    }
  ]
}
