I tried using UnityCloudBuild API from Python

I tried using the API of Unity Cloud Build from Python. I think it can be used when uploading a file built in cooperation with other systems.

I tried using Unity Cloud Build

The point that it can be built from the source code of BitBucket (former: Stash) was also high. The setting of the initial build was about 5 minutes with a button, so the explanation is omitted.

The first time is supposed to be built manually and the second and subsequent times are used via API. All the details of the API used this time are described in the official document.

1. Build execution

If the build fails, HTTP Status: 202 will be returned. The URL of the completed file is a separate file

u.py


# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

import requests

API_TOKEN = "****"
ORGID = "****"
PROJECTS = "****"
BUILD_TARGETS = "****"


def get_url(func_name, extend=None):
    auth_base = "orgs/{orgid}/projects/{projectid}/buildtargets/{buildtargetid}".format(orgid=ORGID,
                                                                                        projectid=PROJECTS,
                                                                                        buildtargetid=BUILD_TARGETS)
    if extend:
        url_base = "https://build-api.cloud.unity3d.com/api/v1/{auth}/{func}{extend}"
        return url_base.format(auth=auth_base, func=func_name, extend=extend)

    url_base = "https://build-api.cloud.unity3d.com/api/v1/{auth}/{func}"
    return url_base.format(auth=auth_base, func=func_name)


def get_headers():
    return {
        "Content-Type": "application/json",
        "Authorization": "Basic {token}".format(token=API_TOKEN),
    }


def execute_build():
    """
Build execution

    #Semi-normal system Already building
    [{"buildtargetid":"****","buildTargetName":"****","error":"Cannot start build - already a build pending."}] 202
    """
    response = requests.post(get_url("builds"), headers=get_headers(), data={})
    print(response.text), response.status_code
    return

#Build execution
execute_build()

2. Get build status

The build status is as follows {queued, sentToBuilder, started, restarted, success, failure, canceled, unknown} If there is no charge, the queued time is long. If the build is successful, it will be success

u.py



def get_builds():
    """
You can get the latest 25 build information.
If you want to get older data, per_Modify page and page parameters
    """
    response = requests.get(get_url("builds"), headers=get_headers())
    print(response.text), response.status_code
    return


def get_build(number):
    """
You can get the specified build information

    #Semi-normal system
404 if it does not exist: {"error":"The requested object was not found."}

    :param number: int
    """
    response = requests.get(get_url("builds", extend="/" + str(number)), headers=get_headers())
    print(response.text), response.status_code
    return


def get_latest_build():
    """
You can get the latest 1 build
    """
    build_start_result = requests.get(get_url("builds", extend="?per_page=1&page=1"), headers=get_headers())
    print(build_start_result.text), build_start_result.status_code
    return

#Get a specific build
get_build(6)

#Get the latest build
get_latest_build()

#Get all builds
get_build_status()

3. Build cancel

Is there anything you want to cancel from the API after implementing it? I thought. It is a specification to cancel with an HTML DELETE query. DELETE query is used for the first time this year. The character of the engineer who made the API faithfully to the REST principle was somehow conveyed.

u.py



def cancel_all():
    """
Cancel all builds
    """
    response = requests.delete(get_url("builds"), headers=get_headers())
    print(response.text), response.status_code


def cancel(number):
    """
Cancel a specific build

    #Successful cancellation of normal system
    204

    #Semi-normal system does not exist
    {"error":"The requested object was not found."} 404

    #When you specify a build that has completed a quasi-normal build
    204

    :param number: int
    """
    response = requests.delete(get_url("builds", extend="/" + str(number)), headers=get_headers())
    print(response.text), response.status_code


#Specific build cancellation
cancel(7)

#Cancel all builds
cancel_all()

4. Get download URL

If the build is successful, the file will be uploaded to S3. A function to get the URL to download.

u.py



def get_latest_build_download_urls():
    """
Download URL for the latest successful build

    :return: str
    """
    response = requests.get(get_url("builds", extend="?buildStatus=success&per_page=1&page=1"),
                                      headers=get_headers())
    data = ujson.loads(response.text)
    assert len(data) > 0
    return data[0].get('links').get('download_primary').get('href')

#Download URL for the latest successful build
print get_latest_build_download_urls()

Completed sauce

u.py


# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

import requests
import ujson

API_TOKEN = "****"
ORGID = "****"
PROJECTS = "****"
BUILD_TARGETS = "****"


def get_url(func_name, extend=None):
    auth_base = "orgs/{orgid}/projects/{projectid}/buildtargets/{buildtargetid}".format(orgid=ORGID,
                                                                                        projectid=PROJECTS,
                                                                                        buildtargetid=BUILD_TARGETS)
    if extend:
        url_base = "https://build-api.cloud.unity3d.com/api/v1/{auth}/{func}{extend}"
        return url_base.format(auth=auth_base, func=func_name, extend=extend)

    url_base = "https://build-api.cloud.unity3d.com/api/v1/{auth}/{func}"
    return url_base.format(auth=auth_base, func=func_name)


def get_headers():
    return {
        "Content-Type": "application/json",
        "Authorization": "Basic {token}".format(token=API_TOKEN),
    }


def execute_build():
    """
Build execution

    #Semi-normal system Already building
    [{"buildtargetid":"****","buildTargetName":"****","error":"Cannot start build - already a build pending."}] 202
    """
    response = requests.post(get_url("builds"), headers=get_headers(), data={})
    print(response.text), response.status_code
    return


def get_builds():
    """
You can get the latest 25 build information.
If you want to get older data, per_Modify page and page parameters
    """
    response = requests.get(get_url("builds"), headers=get_headers())
    print(response.text), response.status_code
    return


def get_build(number):
    """
You can get the specified build information

    #Semi-normal system
404 if it does not exist: {"error":"The requested object was not found."}

    :param number: int
    """
    response = requests.get(get_url("builds", extend="/" + str(number)), headers=get_headers())
    print(response.text), response.status_code
    return


def get_latest_build():
    """
You can get the latest 1 build
    """
    build_start_result = requests.get(get_url("builds", extend="?per_page=1&page=1"), headers=get_headers())
    print(build_start_result.text), build_start_result.status_code
    return


def get_latest_build_download_urls():
    """
Download URL for the latest successful build

    :return: str
    """
    response = requests.get(get_url("builds", extend="?buildStatus=success&per_page=1&page=1"),
                                      headers=get_headers())
    data = ujson.loads(response.text)
    assert len(data) > 0
    return data[0].get('links').get('download_primary').get('href')


def cancel_all():
    """
Cancel all builds
    """
    response = requests.delete(get_url("builds"), headers=get_headers())
    print(response.text), response.status_code


def cancel(number):
    """
Cancel a specific build

    #Successful cancellation of normal system
    204

    #Semi-normal system does not exist
    {"error":"The requested object was not found."} 404

    #When you specify a build that has completed a quasi-normal build
    204

    :param number: int
    """
    response = requests.delete(get_url("builds", extend="/" + str(number)), headers=get_headers())
    print(response.text), response.status_code


#Specific build cancellation
# cancel(7)

#Cancel all builds
# cancel_all()

#Get a specific build
# get_build(6)

#Get the latest build
# get_latest_build()

#Get all builds
# get_build_status()

#Build execution
# execute_build()

#Download URL for the latest successful build
print get_latest_build_download_urls()

Recommended Posts

I tried using UnityCloudBuild API from Python
[Python] I tried using OpenPose
I tried using Thonny (Python / IDE)
Run Ansible from Python using API
I tried using the checkio API
[Python] I tried using YOLO v3
I tried to create API list.csv in Python from swagger.yaml
I tried using the Python library from Ruby with PyCall
[Python] I tried collecting data using the API of wikipedia
I tried using Twitter api and Line api
I tried using Bayesian Optimization in Python
I tried using Headless Chrome from Selenium
I tried using the BigQuery Storage API
[Python] I tried to get various information using YouTube Data API!
I tried using parameterized
I tried using argparse
I tried using mimesis
I tried using anytree
I tried using aiomysql
I tried using Summpy
I tried Python> autopep8
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using ngrok
I tried using face_recognition
I tried using Jupyter
I tried using PyCaret
I tried using Heapq
I tried using doctest
I tried Python> decorator
I tried using folium
I tried using jinja2
I tried using folium
I tried using time-window
I tried using AWS Rekognition's Detect Labels API
I tried using Remote API on GAE / J
I tried hitting the Qiita API from go
vprof --I tried using the profiler for Python
I tried web scraping using python and selenium
I tried object detection using Python and OpenCV
I want to email from Gmail using Python.
I tried Python! ] I graduated today from "What is Python! Python!"!
I tried using mecab with python2.7, ruby2.3, php7
I tried debugging from Python via System Console
I tried reading a CSV file using Python
I tried using the Datetime module by Python
Flatten using Python yield from
I tried fp-growth with python
[Python] I immediately tried using Pylance's VS Code extension.
[I tried using Pythonista 3] Introduction
I tried using TradeWave (BitCoin system trading in Python)
I tried using easydict (memo).
Push notifications from Python to Android using Google's API