appkey
see in settings api/front/config.form.php
EAuthorizationType.UserToken
- for authentication from user token. See in the user settings/front/preference.php
in Remote access keysEAuthorizationType.LoginPassInBase64
- for use LDAP and internal auth and other method; Session continue ~ 20 min
await using Client client = new Client("http://localhost:5555/apirest.php/","appkey");
await client.InitSession(
"glpiClientToken",
EAuthorizationType.UserToken,
true);
- I recommend using a user token to avoid leaking user data
await client.InitSession(
Client.ConvertToBase64("log", "pass"),
EAuthorizationType.LoginPassInBase64,
true);
log
andpass
any type auth
- If you need 1 element from the dashboard descendants, then you only need to specify the ID in the parameters
- If you need to make a special collection, then don't use ID
- If you need to get all the elements then use an overload without parameters
Ticket? tic = await Ticket.GetItemAsync(client, new Parameter
{
id = 1
});
IEnumerable<Ticket>? tics = await Ticket.GetItemsAsync(client);
IEnumerable<Ticket>? ticsWithParameters = await Ticket.GetItemsAsync(client, new Parameter
{
order = Parameter.EOrder.DESC,
range = new Range(0, 9)
});
- In the last example, returning the last 10 tickets.
ResponseSearch? searchTickets = await Ticket.GetItemsAsync(client, new[]
{
Criteria.Create<Ticket>((int) EFieldTicket.TicketStatus,
false,
Criteria.ESearchType.equals,
$"{(int) Ticket.EStatus.Await}")
});
- Search for tickets based on a given criterion.
- For each heir, the dashboard has its own listing. For tickets this is
EFieldTicket
. - To initialize the criterion, use the static
Create
method.
Criteria.Create<Ticket>((int) EFieldTicket.TicketStatus,
false,
Criteria.ESearchType.equals,
$"{(int) Ticket.EStatus.Await}")
Criteria.Create((int) EFieldTicket.TicketStatus,
false,
typeof(Ticket),
Criteria.ESearchType.equals,
$"{(int) Ticket.EStatus.Await}")
- Argument 1
int
:- field identifier. It can be obtained from the enumeration I wrote above.
- Argument 2
bool
:- it indicates different types. For example, if you are building a query for computers and want to sort by monitors, then in the meta field you need to indicate false, in other cases true.
- Argument 3
ESearchType
:- for the accuracy of the request, you need to know which parameter is suitable for the field. You can find it (for tickets):
IEnumerable<SearchOption> searchOptions = await Ticket.ListSearchOptionsAsync(client);
- Argument 4
int
and conversion tostring
(forenum
) orstring
:- takes a value to search for; if this is an enumeration, then you need to use the int data type.
responseSearchTickets.Data
.Select(s => s.FirstOrDefault().Value)
.AsParallel()
.Select(p => Ticket.GetItemAsync(client,
new Parameter
{
id = (long) p
}).Result)
.ToList();
responseSearchTickets
was obtained from the previous example.
In s.FirstOrDefault().Value
contains EFieldTicket.TicketStatus
(ticket id). At the same time I download tickets by ID.
ResponseSearch? responseSearch = await Ticket.GetItemsAsync(client,
new Parameter
{
is_deleted = false,
range = new Range(0, 9),
sort = (int)EFieldTicket.TicketId
},
new Criteria[]
{
Criteria.Create<Ticket>
(
(int) EFieldTicket.TicketStatus,
false,
Criteria.ESearchType.equals,
$"{(int)Ticket.EStatus.Await}"
)
});
- In the parameters:
is_deleted = false
(sorting will ignore deleted objects)range: from 0 to 9 (10 objects)
will be taken into account in sortingsorting: the number of the field
by which sorting will be performed.
- In the
criteria
(search
fortickets
whosestatus
isequal
toAwait
) - In the final: loading 10 non-deleted tickets, sorted by their ID, that are in Await status
var ticks = responseSearch?.Data
.Select(s => (long) s.FirstOrDefault().Value)
.AsParallel()
.Select(q => Ticket.GetItemAsync(client, new Parameter
{
id = q
}).Result)
.ToList();
ManifestFileUpload? result = await Document.Upload(client, new ManifestFile("file.png", "newImage.png"));
ManifestFileUpload? result = await Document.Upload(client, new ManifestFile(new MemoryStream(),"fileFromStream.png", "newImageFromStream.png"));
if (!Equals(result, null))
{
var streamFile = Document.Download(client, result.Id);
await using FileStream fs = new FileStream(result.upload_result.Files.FirstOrDefault().Display, FileMode.CreateNew, FileAccess.Write);
await (await streamFile).CopyToAsync(fs);
}
string ticItem = await Ticket.AddItemAsync(client, new Ticket
{
Content = "This is a new ticket"
});
string ticItems = await Ticket.AddItemsAsync(client, new Ticket[]
{
new ()
{
Content = "Help me! From 1 room",
IdLocation = 2
},
new ()
{
Content = "<div><h3>Help me! From 2 room</h3></div>",
IdLocation = 3
}
});
In the tic receives a ticket with ID 2. We update the location ID and ticket status on the closed using the UpdateItemAsync method; the same for several tickets
Ticket? tic = await Ticket.GetItemAsync(client,new Parameter
{
id = 2
});
tic.IdLocation = 4;
tic.Status = Ticket.EStatus.Close;
string updateTic = await Ticket.UpdateItemAsync(client, tic);
var tics = (await Ticket.GetItemsAsync(client, new Parameter
{
is_deleted = true
})).ToList();
tics.ForEach(tic => tic.IsDeleted = false);
string updateTicks = await Ticket.UpdateItemAsync(client, tics);
- Argument 3
bool
:isForcePurge
(defaultfalse
)- If the itemtype have a trashbin, you can force purge (delete finally)
- Argument 4
bool
:isHistory
(defaulttrue
)- Set to false to disable saving of deletion in global history
Ticket? tic = await Ticket.GetItemAsync(client,new Parameter
{
id = 2
});
if(!Equals(tic, null))
await Ticket.DeleteItemAsync(client, tic, true, false);
- If the ticket exists, it can be deleted.
WebHook with plugin the Webhook
const string adr = "http://localhost:5533/";
client.InitListener(new[]
{
new Listening(
string.Join(string.Empty, adr, "createtic"),
"POST",
(result, id) => Task.CompletedTask),
Guid.NewGuid().ToString()
new Listening(
string.Join(string.Empty, adr, "updatetic"),
HttpMethod.Put.Method,
(result, id) => Task.CompletedTask),
new Listening(
string.Join(string.Empty, adr, "deltic"),
HttpMethod.Delete.Method,
ResponseObj,
typeof(Ticket))
});
- Argument 1
string
: http listening address with port if needed - Argument 2
string
: http method. Defaultput
orpost
- Argument 3
Func<string?,string?,Task>
orFunc<objeck?,string?,Task>
: a function whose- 1 argument is a
string
or anobject
; - 2 optional parameter: listener identifier,
- and output is used for
async
.
- 1 argument is a
- Argument 4
string
: optional parameter. Listener ID - If you use an
object
in the 3rd argument, then for deserialization you must specify thetype of the object
, otherwise it willdefault to a string
private Task ResponseObj(object? result, string? id)
{
Ticket? tic = result as Ticket;
return Task.CompletedTask;
}
await client.DisposableListener();
- use
using
await using Client client = new Client("http://localhost:5555/apirest.php/","appkey");
- use
DisposeAsync()
Client client = new Client("http://localhost:5555/apirest.php/","appkey");
await client.DisposeAsync();