Showing posts with label Redmine Rest API. Show all posts
Showing posts with label Redmine Rest API. Show all posts

Monday, 29 September 2014

Rest_TimeEntries



Time Entries
Listing time entries
GET /time_entries.xml
Returns time entries.
Showing a time entry
GET /time_entries/[id].xml
Returns the time entry of given id.
Creating a time entry
POST /time_entries.xml
Creates a time entry.
Parameters:
  • time_entry (required): a hash of the time entry attributes, including:
    • issue_id or project_id (only one is required): the issue id or project id to log time on
    • spent_on: the date the time was spent (default to the current date)
    • hours (required): the number of spent hours
    • activity_id: the id of the time activity. This parameter is required unless a default activity is defined in Redmine.
    • comments: short description for the entry (255 characters max)
Response:
  • 201 Created: time entry was created
  • 422 Unprocessable Entity: time entry was not created due to validation failures (response body contains the error messages)
Updating a time entry
PUT /time_entries/[id].xml
Updates the time entry of given id.
Parameters:
  • time_entry (required): a hash of the time entry attributes (same as above)
Response:
  • 200 OK: time entry was updated
  • 422 Unprocessable Entity: time entry was not updated due to validation failures (response body contains the error messages)
Deleting a time entry
DELETE /time_entries/[id].xml
Deletes the time entry of given id.
Insert TimeEntry Code

public string InsertTimeEntries(string host, string key)
 {

  ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, error) => true;

var manager = new RedmineManager(host, key);

 TimeEntry timeent = new TimeEntry();

 timeent.Issue = new IdentifiableName { Id = 3175, Name = "Sample Test Issue" };

 timeent.Project = new IdentifiableName() { Id = 88, Name = "Jacobson - SSRS" };

 timeent.SpentOn = DateTime.Now;

 timeent.Hours = 3;

 timeent.Comments = "Testing of the Code";

 timeent.User = new IdentifiableName { Id = 111, Name = "narendra.kushwaha" };

 timeent.Activity = new IdentifiableName { Id = 14, Name = "Coding" };

 manager.CreateObject(timeent);
  }

Thursday, 25 September 2014

php-redmine-api/Issue.php at master

<?php
namespace Redmine\Api;
/**
* Listing issues, searching, editing and closing your projects issues.
*
* @link http://www.redmine.org/projects/redmine/wiki/Rest_Issues
* @author Kevin Saliou <kevin at saliou dot name>
*/
class Issue extends AbstractApi
{
const PRIO_LOW = 1;
const PRIO_NORMAL = 2;
const PRIO_HIGH = 3;
const PRIO_URGENT = 4;
const PRIO_IMMEDIATE = 5;
/**
* List issues
* @link http://www.redmine.org/projects/redmine/wiki/Rest_Issues
* available $params :
* - offset: skip this number of issues in response (optional)
* - limit: number of issues per page (optional)
* - sort: column to sort with. Append :desc to invert the order.
* - project_id: get issues from the project with the given id, where id is either project id or project identifier
* - tracker_id: get issues from the tracker with the given id
* - status_id: get issues with the given status id only. Possible values: open, closed, * to get open and closed issues, status id
* - assigned_to_id: get issues which are assigned to the given user id
* - cf_x: get issues with the given value for custom field with an ID of x. (Custom field must have 'used as a filter' checked.)
* - query_id : id of the previously saved query
*
* @param array $params the additional parameters (cf avaiable $params above)
* @return array list of issues found
*/
public function all(array $params = array())
{
return $this->retrieveAll('/issues.json', $params);
}
/**
* Get extended information about an issue gitven its id
* @link http://www.redmine.org/projects/redmine/wiki/Rest_Issues#Using-JSON
* available $params :
* include: fetch associated data (optional). Possible values: children, attachments, relations, changesets and journals
*
* @param string $id the issue id
* @param array $params extra associated data
* @return array information about the issue
*/
public function show($id, array $params = array())
{
return $this->get('/issues/'.urlencode($id).'.json?'.http_build_query($params));
}
/**
* Build the XML for an issue
* @param array $params for the new/updated issue data
* @return SimpleXMLElement
*/
private function buildXML(array $params = array())
{
$xml = new SimpleXMLElement('<?xml version="1.0"?><issue></issue>');
foreach ($params as $k => $v) {
if ('custom_fields' === $k && is_array($v)) {
$this->attachCustomFieldXML($xml, $v);
} elseif ('watcher_user_ids' === $k && is_array($v)) {
$watcher_user_ids = $xml->addChild('watcher_user_ids', '');
$watcher_user_ids->addAttribute('type', 'array');
foreach ($v as $watcher) {
$watcher_user_ids->addChild('watcher_user_id', (int) $watcher);
}
} elseif ('uploads' === $k && is_array($v)) {
$uploads_item = $xml->addChild('uploads', '');
$uploads_item->addAttribute('type', 'array');
foreach ($v as $upload) {
$upload_item = $uploads_item->addChild('upload', '');
foreach ($upload as $upload_k => $upload_v) {
$upload_item->addChild($upload_k, $upload_v);
}
}
} else {
$xml->addChild($k, $v);
}
}
return $xml;
}
/**
* Create a new issue given an array of $params
* The issue is assigned to the authenticated user.
* @link http://www.redmine.org/projects/redmine/wiki/Rest_Issues#Creating-an-issue
*
* @param array $params the new issue data
* @return SimpleXMLElement
*/
public function create(array $params = array())
{
$defaults = array(
'subject' => null,
'description' => null,
// 'project' => null,
// 'category' => null,
// 'status' => null,
// 'tracker' => null,
// 'assigned_to' => null,
// 'author' => null,
'project_id' => null,
'category_id' => null,
'priority_id' => null,
'status_id' => null,
'tracker_id' => null,
'assigned_to_id' => null,
'author_id' => null,
'due_date' => null,
'start_date' => null,
'watcher_user_ids' => null,
);
$params = $this->cleanParams($params);
$params = array_filter(array_merge($defaults, $params));
$xml = $this->buildXML($params);
return $this->post('/issues.xml', $xml->asXML());
// $json = json_encode(array('issue' => $params));
// return $this->post('/issues.json', $json);
}
/**
* Update issue information's by username, repo and issue number. Requires authentication.
* @link http://www.redmine.org/projects/redmine/wiki/Rest_Issues#Updating-an-issue
*
* @param string $id the issue number
* @param array $params
* @return SimpleXMLElement
*/
public function update($id, array $params)
{
$defaults = array(
'id' => $id,
'subject' => null,
'notes' => null,
// 'project' => null,
// 'category' => null,
// 'status' => null,
// 'tracker' => null,
// 'assigned_to' => null,
// 'author' => null,
'category_id' => null,
'priority_id' => null,
'status_id' => null,
'tracker_id' => null,
'assigned_to_id' => null,
'due_date' => null,
);
$params = $this->cleanParams($params);
$params = array_filter(array_merge($defaults, $params));
$xml = $this->buildXML($params);
return $this->put('/issues/'.$id.'.xml', $xml->asXML());
}
/**
* @param int $id
* @param string $watcher_user_id
* @return void
*/
public function addWatcher($id, $watcher_user_id)
{
return $this->post('/issues/'.$id.'/watchers.xml', '<user_id>'.$watcher_user_id.'</user_id>');
}
/**
* @param int $id
* @param string $watcher_user_id
* @return void
*/
public function removeWatcher($id, $watcher_user_id)
{
return $this->delete('/issues/'.$id.'/watchers/'.$watcher_user_id.'.xml');
}
/**
* @param int $id
* @param string $status
* @return void
*/
public function setIssueStatus($id, $status)
{
$statusId = $this->client->api('issue_status')->getIdByName($status);
return $this->update($id, array(
'status_id' => $statusId
));
}
/**
* @param int $id
* @param string $note
* @return void
*/
public function addNoteToIssue($id, $note)
{
return $this->update($id, array(
'notes' => $note
));
}
/**
* Transforms literal identifiers to integer ids
* @param array $params
* @return array
*/
private function cleanParams(array $params = array())
{
if (isset($params['project'])) {
$params['project_id'] = $this->client->api('project')->getIdByName($params['project']);
unset($params['project']);
if (isset($params['category'])) {
$params['category_id'] = $this->client->api('issue_category')->getIdByName($params['project_id'], $params['category']);
unset($params['category']);
}
}
if (isset($params['status'])) {
$params['status_id'] = $this->client->api('issue_status')->getIdByName($params['status']);
unset($params['status']);
}
if (isset($params['tracker'])) {
$params['tracker_id'] = $this->client->api('tracker')->getIdByName($params['tracker']);
unset($params['tracker']);
}
if (isset($params['assigned_to'])) {
$params['assigned_to_id'] = $this->client->api('user')->getIdByUsername($params['assigned_to']);
unset($params['assigned_to']);
}
if (isset($params['author'])) {
$params['author_id'] = $this->client->api('user')->getIdByUsername($params['author']);
unset($params['author']);
}
return $params;
}
/**
* Attach a file to an issue issue number. Requires authentication.
* @link http://www.redmine.org/projects/redmine/wiki/Rest_Issues#Updating-an-issue
*
* @param string $id the issue number
* @param array $attachment
* @return bool|string
*/
public function attach($id, array $attachment)
{
$request['issue'] = array(
'id' => $id,
'uploads' => array(
'upload' => $attachment
)
);
return $this->put('/issues/'.$id.'.json', json_encode($request));
}
/**
* Remove a issue by issue number
*
* @param string $id the issue number
*/
public function remove($id)
{
return $this->delete('/issues/'.$id.'.xml');
}
}

Consuming a JSON service with C#



If you like to consume a JSON web service in C# this example might be handy.

You need to know the url (web address) of the web service

You need to understand what the web service will return


You need to know what kind of security the web service accepts (My example uses Basic Authentication)


You need to understand under which context you are consuming the service (My example uses a Proxy server to communicate)


I found a site that automatically generates a class based on the result of the JSON web service at http://json2csharp.com/. This site can generate an class either by you specifying the url to the web service or providing the JSON result from the service you wish to consume.

Lets just assume in the example that my service will return {“variable1″:1,”variable2″:”a return string”}

By definition this means one variable with an int and one with a string.
JSON2CSHARP will return a usable class for me to use in my code;

public class RootObject
{
    public int variable1 { get; set; }
    public string variable2 { get; set; }
}


Lets go ahead and add some more code to consume this now when we have a nice helper class to contain my result.

string url = “http://yourserver/service?someparams=somevalue“;

HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);

string username = “username”;


string password = “********”;


// Use the CredentialCache so we can attach the authentication to the request
CredentialCache mycache = new CredentialCache();


// We want to use Basic Authentication
mycache.Add(new Uri(url), “Basic”, new NetworkCredential(username, password));


wr.Credentials = mycache;
wr.Headers.Add(“Authorization”, “Basic ” + Convert.ToBase64String(new ASCIIEncoding().GetBytes(username + “:” + password)));


// Proxy (if you do not need it – ommit it)
wr.Proxy = new WebProxy(http://proxyserver:8080);


// Get the response from the web service
HttpWebResponse response = (HttpWebResponse)wr.GetResponse();
Stream r_stream = response.GetResponseStream();


//convert it
StreamReader response_stream = new StreamReader(r_stream, System.Text.Encoding.GetEncoding(“utf-8″));

string jSon = response_stream.ReadToEnd();

//clean up your stream
response_stream.Close();

System.Web.Script.Serialization.JavaScriptSerializer jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();

 RootObject result = jsSerializer.Deserialize<RootObject>(jSon);

How do I use the Redmine REST API over https from .net?

 

  Best way to use Redmine REST API over https from .net?

 

  protected void Page_Load(object sender, EventArgs e)
        {

            string host = ConfigurationManager.AppSettings["redmineHost"].ToString();

            string key = ConfigurationManager.AppSettings["redmineKey"].ToString();           

            //  var manager = new RedmineManager(host, user,pass);
            var manager = new RedmineManager(host,
key);
            string str= ReadIssue(host,
key);
            Response.Write(str);
         }

      

    public string ReadIssue (string host,string key)
    {
       ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, error) => true;

            var request = (HttpWebRequest)WebRequest.Create(host+"/issues/1083.json?key="+key+"");
            request.CookieContainer = new CookieContainer();
            request.Method = "GET";
            request.ContentType = "application/json";
           
            using (var response = request.GetResponse()) // Hangs here
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                return reader.ReadToEnd();
            }
   
    }

C# LINQ Joins With SQL

There are  Different Types of SQL Joins  which are used to query data from more than one database tables. In this article, you will learn a...